split cursors

This commit is contained in:
Travis Shears 2025-10-02 10:36:19 +02:00
parent eb2191045e
commit f74f50c148
3 changed files with 58 additions and 32 deletions

View file

@ -17,7 +17,6 @@ func genBasicAuthHeader(user, password string) string {
} }
func listGemLogs(config *Config) ([]GemlogListEntry, error) { func listGemLogs(config *Config) ([]GemlogListEntry, error) {
slog.Info("Listing gemlogs from couchdb")
url := fmt.Sprintf("%s:%d/gemlog/_design/gemlog-cli/_view/list", config.CouchDB.Host, config.CouchDB.Port) url := fmt.Sprintf("%s:%d/gemlog/_design/gemlog-cli/_view/list", config.CouchDB.Host, config.CouchDB.Port)
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {

View file

@ -1,15 +1,12 @@
package gemlog package gemlog
import ( import (
"log/slog"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
func LoadGemlogCMD(config *Config) tea.Cmd { func LoadGemlogCMD(config *Config) tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
logs, err := listGemLogs(config) logs, err := listGemLogs(config)
slog.Info("Loaded gemlogs", "count", len(logs))
if err != nil { if err != nil {
return ErrorMsg{err} return ErrorMsg{err}
} }

64
main.go
View file

@ -34,16 +34,21 @@ const (
type entryListPageModel struct { type entryListPageModel struct {
entries []gemlog.GemlogListEntry entries []gemlog.GemlogListEntry
action Action actionToTake Action
cursor int
}
type actionListPageModel struct {
cursor int
} }
type uiState struct { type uiState struct {
notification string notification string
cursor int
errorTxt string errorTxt string
page Page page Page
entryListPage entryListPageModel entryListPage entryListPageModel
actionListPage actionListPageModel
} }
type context struct { type context struct {
@ -60,8 +65,12 @@ func initialModel(config *gemlog.Config) model {
ui: uiState{ ui: uiState{
page: ActionList, page: ActionList,
entryListPage: entryListPageModel{ entryListPage: entryListPageModel{
cursor: 0,
entries: []gemlog.GemlogListEntry{}, entries: []gemlog.GemlogListEntry{},
}, },
actionListPage: actionListPageModel{
cursor: 0,
},
}, },
context: &context{ context: &context{
config: config, config: config,
@ -81,31 +90,54 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ui.notification = fmt.Sprintf("%s\n\n", string(msg)) m.ui.notification = fmt.Sprintf("%s\n\n", string(msg))
case gemlog.GemLogsLoaded: case gemlog.GemLogsLoaded:
m.ui.entryListPage.entries = msg.Logs m.ui.entryListPage.entries = msg.Logs
m.ui.cursor = 0 m.ui.entryListPage.cursor = 0
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "q": case "ctrl+c", "q":
return m, tea.Quit return m, tea.Quit
case "up", "k": case "up", "k":
if m.ui.cursor > 0 { var cursor *int
m.ui.cursor-- switch m.ui.page {
case EntryList:
cursor = &m.ui.entryListPage.cursor
case ActionList:
cursor = &m.ui.actionListPage.cursor
}
if cursor != nil && *cursor > 0 {
(*cursor)--
} }
case "down", "j": case "down", "j":
if m.ui.cursor < len(actions)-1 { var cursor *int
m.ui.cursor++ if m.ui.page == EntryList && m.ui.entryListPage.cursor < len(m.ui.entryListPage.entries)-1 {
cursor = &m.ui.entryListPage.cursor
} else if m.ui.page == ActionList && m.ui.actionListPage.cursor < len(actions)-1 {
cursor = &m.ui.actionListPage.cursor
}
if cursor != nil {
(*cursor)++
} }
case "enter", " ": case "enter", " ":
action := actions[m.ui.cursor] if m.ui.page == ActionList {
action := actions[m.ui.actionListPage.cursor]
switch action { switch action {
case Write: case Write:
return m, gemlog.WritePostCMD(m.context.config) return m, gemlog.WritePostCMD(m.context.config)
case Read: case Read:
m.ui.page = EntryList m.ui.page = EntryList
m.ui.entryListPage.cursor = 0
m.ui.entryListPage.actionToTake = Read
return m, gemlog.LoadGemlogCMD(m.context.config) return m, gemlog.LoadGemlogCMD(m.context.config)
case Edit: case Edit:
return m, TODOCmd m.ui.page = EntryList
m.ui.entryListPage.cursor = 0
m.ui.entryListPage.actionToTake = Edit
return m, gemlog.LoadGemlogCMD(m.context.config)
case Delete: case Delete:
return m, TODOCmd m.ui.page = EntryList
m.ui.entryListPage.cursor = 0
m.ui.entryListPage.actionToTake = Delete
return m, gemlog.LoadGemlogCMD(m.context.config)
}
} }
} }
} }
@ -120,14 +152,13 @@ func (m model) View() string {
s := "" s := ""
if m.ui.notification != "" { if m.ui.notification != "" {
s += m.ui.notification s += m.ui.notification
} else {
s += "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
} }
if m.ui.page == ActionList { if m.ui.page == ActionList {
s += "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
for i, action := range actions { for i, action := range actions {
cursor := " " cursor := " "
if m.ui.cursor == i { if m.ui.actionListPage.cursor == i {
cursor = ">" cursor = ">"
} }
s += fmt.Sprintf("%s %s\n", cursor, action) s += fmt.Sprintf("%s %s\n", cursor, action)
@ -135,11 +166,10 @@ func (m model) View() string {
} }
if m.ui.page == EntryList { if m.ui.page == EntryList {
slog.Info("rendering entry list", "count", len(m.ui.entryListPage.entries)) s += fmt.Sprintf("Which entry would you like to %s\n\n", m.ui.entryListPage.actionToTake)
for i, entry := range m.ui.entryListPage.entries { for i, entry := range m.ui.entryListPage.entries {
slog.Info("entry", "value", entry)
cursor := " " cursor := " "
if m.ui.cursor == i { if m.ui.entryListPage.cursor == i {
cursor = ">" cursor = ">"
} }
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug) s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
@ -160,7 +190,7 @@ func main() {
defer f.Close() defer f.Close()
slog.Info("Starting gemlog cli") slog.Info("Starting gemlog cli")
config, err := gemlog.LoadConfig() config, err := gemlog.LoadConfig()
// gemlog.CheckDBConnection(config) gemlog.CheckDBConnection(config)
if err != nil { if err != nil {
fmt.Printf("Error loading config: %v", err) fmt.Printf("Error loading config: %v", err)
os.Exit(1) os.Exit(1)