93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
gemlog "git.travisshears.com/travisshears/gemlog-cli/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 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(m.entries)-1 {
|
|
m.cursor++
|
|
}
|
|
case "left", "h":
|
|
cmd := func() tea.Msg {
|
|
return SwitchPages{Page: ActionList}
|
|
}
|
|
return m, cmd
|
|
|
|
case "enter", " ", "l":
|
|
id := m.entries[m.cursor].ID
|
|
rev := m.entries[m.cursor].Rev
|
|
switch m.actionToTake {
|
|
case Edit:
|
|
editCmd := EditPostCMD(ctx.config, id, rev)
|
|
navCmd := func() tea.Msg {
|
|
return SwitchPages{Page: ActionList}
|
|
}
|
|
return m, tea.Sequence(editCmd, navCmd)
|
|
case Read:
|
|
loadCmd := LoadGemlogCMD(ctx.config, id)
|
|
navCmd := func() tea.Msg {
|
|
return SwitchPages{Page: Entry}
|
|
}
|
|
return m, tea.Sequence(loadCmd, navCmd)
|
|
case Delete:
|
|
delCmd := DeleteGemlogCMD(ctx.config, id, rev)
|
|
loadCmd := LoadGemlogsCMD(ctx.config)
|
|
navCmd := func() tea.Msg {
|
|
return SwitchPages{Page: ActionList}
|
|
}
|
|
return m, tea.Sequence(delCmd, loadCmd, navCmd)
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|