package main import ( "fmt" "gemini_site/gemlog" tea "github.com/charmbracelet/bubbletea" ) type EntryListPageModel struct { entries []gemlog.GemlogListEntry actionToTake Action cursor int } type SelectActionToTake struct{ ActionToTake Action } func initialEntryListPageModel() EntryListPageModel { return EntryListPageModel{ cursor: 0, actionToTake: Read, } } func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryListPageModel, tea.Cmd) { switch msg := msg.(type) { case SelectActionToTake: m.actionToTake = msg.ActionToTake case gemlog.GemLogsLoaded: m.entries = msg.Logs return m, nil case tea.KeyMsg: if !active { return m, nil } switch msg.String() { case "up", "k": if m.cursor > 0 { m.cursor-- } case "down", "j": if m.cursor < len(actions)-1 { m.cursor++ } case "left", "h": cmd := func() tea.Msg { return SwitchPages{Page: ActionList} } return m, cmd case "enter", " ": id := m.entries[m.cursor].ID rev := m.entries[m.cursor].Rev switch m.actionToTake { case Delete: delCmd := gemlog.DeleteGemlogCMD(ctx.config, id, rev) loadCmd := gemlog.LoadGemlogsCMD(ctx.config) navCmd := func() tea.Msg { return SwitchPages{Page: ActionList} } return m, tea.Sequence(delCmd, loadCmd, navCmd) case Read: return m, gemlog.LoadGemlogCMD(ctx.config, id) } } } 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) } s += "\n\n Press h or left arrow to go back" return s }