diff --git a/actionsList.go b/actionsList.go index c1df8e1..430021c 100644 --- a/actionsList.go +++ b/actionsList.go @@ -49,11 +49,12 @@ func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageMo switch action { case Write: return m, gemlog.WritePostCMD(ctx.config) - // case Read: - // m.ui.page = EntryList - // m.ui.entryListPage.cursor = 0 - // m.ui.entryListPage.actionToTake = Read - // return m, gemlog.LoadGemlogCMD(ctx.config) + case Read: + switchPageCmd := func() tea.Msg { + return SwitchPages{Page: EntryList} + } + loadGemLogsCmd := gemlog.LoadGemlogCMD(ctx.config) + return m, tea.Batch(switchPageCmd, loadGemLogsCmd) // case Edit: // m.ui.page = EntryList // m.ui.entryListPage.cursor = 0 diff --git a/entryList.go b/entryList.go new file mode 100644 index 0000000..af9e8c3 --- /dev/null +++ b/entryList.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "gemini_site/gemlog" + + tea "github.com/charmbracelet/bubbletea" +) + +type EntryListPageModel struct { + entries []gemlog.GemlogListEntry + actionToTake Action + cursor int +} + +func InitialEntryListPageModel() EntryListPageModel { + return EntryListPageModel{ + cursor: 0, + actionToTake: Read, + } +} + +func (m EntryListPageModel) InitEntryListPage() tea.Cmd { + return nil +} + +func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageModel, tea.Cmd) { + switch msg := msg.(type) { + case gemlog.GemLogsLoaded: + m.entries = msg.Logs + return m, nil + case tea.KeyMsg: + switch msg.String() { + case "up", "k": + if m.cursor > 0 { + m.cursor-- + } + case "down", "j": + if m.cursor < len(actions)-1 { + m.cursor++ + } + // case "enter", " ": + + // action := actions[m.cursor] + // switch action { + // case Write: + // return m, gemlog.WritePostCMD(ctx.config) + // case Read: + // m.ui.page = EntryList + // m.ui.entryListPage.cursor = 0 + // m.ui.entryListPage.actionToTake = Read + // return m, gemlog.LoadGemlogCMD(ctx.config) + // case Edit: + // m.ui.page = EntryList + // m.ui.entryListPage.cursor = 0 + // m.ui.entryListPage.actionToTake = Edit + // return m, gemlog.LoadGemlogCMD(ctx.config) + // case Delete: + // m.ui.page = EntryList + // m.ui.entryListPage.cursor = 0 + // m.ui.entryListPage.actionToTake = Delete + // return m, gemlog.LoadGemlogCMD(m.context.config) + // } + } + } + + return m, nil +} + +func (m EntryListPageModel) View() string { + s := fmt.Sprintf("Which entry would you like to %s\n\n", m.actionToTake) + for i, entry := range m.entries { + cursor := " " + if m.cursor == i { + cursor = ">" + } + s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug) + } + return s +} diff --git a/gemlog/list.go b/gemlog/list.go index 90c7226..ce51dfc 100644 --- a/gemlog/list.go +++ b/gemlog/list.go @@ -10,6 +10,6 @@ func LoadGemlogCMD(config *Config) tea.Cmd { if err != nil { return ErrorMsg{err} } - return GemLogsLoaded{logs} + return GemLogsLoaded{Logs: logs} } } diff --git a/main.go b/main.go index d6520ec..6b2d5e8 100644 --- a/main.go +++ b/main.go @@ -13,22 +13,18 @@ type Page string const ( ActionList Page = "actionList" - // EntryList Page = "entryList" + EntryList Page = "entryList" // Entry Page = "entry" ) -// type entryListPageModel struct { -// entries []gemlog.GemlogListEntry -// actionToTake Action -// cursor int -// } +type SwitchPages struct{ Page Page } type uiState struct { notification string errorTxt string - page Page - // entryListPage entryListPageModel + page Page + entryListPage EntryListPageModel actionListPage ActionListPageModel } @@ -67,6 +63,8 @@ func (m model) Init() tea.Cmd { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds := make([]tea.Cmd, 0) switch msg := msg.(type) { + case SwitchPages: + m.ui.page = msg.Page case gemlog.ErrorMsg: m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg)) case gemlog.Notification: @@ -78,13 +76,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } - // Delegate to the active page - switch m.ui.page { - case ActionList: - m2, cmd := m.ui.actionListPage.Update(msg, m.context) - m.ui.actionListPage = m2 - cmds = append(cmds, cmd) - } + actionListM, cmd := m.ui.actionListPage.Update(msg, m.context) + m.ui.actionListPage = actionListM + cmds = append(cmds, cmd) + + entryListM, cmd := m.ui.entryListPage.Update(msg, m.context) + m.ui.entryListPage = entryListM + cmds = append(cmds, cmd) return m, tea.Batch(cmds...) } @@ -100,19 +98,10 @@ func (m model) View() string { if m.ui.page == ActionList { s += m.ui.actionListPage.View() + } else if m.ui.page == EntryList { + s += m.ui.entryListPage.View() } - // if m.ui.page == EntryList { - // s += fmt.Sprintf("Which entry would you like to %s\n\n", m.ui.entryListPage.actionToTake) - // for i, entry := range m.ui.entryListPage.entries { - // cursor := " " - // if m.ui.entryListPage.cursor == i { - // cursor = ">" - // } - // s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug) - // } - // } - s += "\nPress q to quit.\n" return s