restructure project following best practices
https://go.dev/doc/modules/layout This also enables us to later import just the db interaction part say to the gemini capsule backend go project.
This commit is contained in:
parent
928c82536f
commit
360fedbebe
11 changed files with 202 additions and 169 deletions
88
internal/ui/entryList.go
Normal file
88
internal/ui/entryList.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
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(actions)-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 {
|
||||
// TODO: handle edit
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue