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) {
slog.Info("Listing gemlogs from couchdb")
url := fmt.Sprintf("%s:%d/gemlog/_design/gemlog-cli/_view/list", config.CouchDB.Host, config.CouchDB.Port)
req, err := http.NewRequest("GET", url, nil)
if err != nil {

View file

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

86
main.go
View file

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