package main import ( "fmt" "gemini_site/gemlog" "log/slog" "os" tea "github.com/charmbracelet/bubbletea" ) type Action string const ( Write Action = "write" Read Action = "read" Edit Action = "edit" Delete Action = "delete" ) var actions = []Action{Write, Read, Edit, Delete} func TODOCmd() tea.Msg { return gemlog.Notification("This action has not been implemented yet. Try another.") } type Page string const ( ActionList Page = "actionList" EntryList Page = "entryList" Entry Page = "entry" ) type entryListPageModel struct { entries []gemlog.GemlogListEntry actionToTake Action cursor int } type actionListPageModel struct { cursor int } type uiState struct { notification string errorTxt string page Page entryListPage entryListPageModel actionListPage actionListPageModel } type context struct { config *gemlog.Config } type model struct { ui uiState context *context } func initialModel(config *gemlog.Config) model { return model{ ui: uiState{ page: ActionList, entryListPage: entryListPageModel{ cursor: 0, entries: []gemlog.GemlogListEntry{}, }, actionListPage: actionListPageModel{ cursor: 0, }, }, context: &context{ config: config, }, } } func (m model) Init() tea.Cmd { return tea.SetWindowTitle("Gemlog CLI") } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case gemlog.ErrorMsg: m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg)) case gemlog.Notification: m.ui.notification = fmt.Sprintf("%s\n\n", string(msg)) case gemlog.GemLogsLoaded: m.ui.entryListPage.entries = msg.Logs m.ui.entryListPage.cursor = 0 case tea.KeyMsg: switch msg.String() { case "ctrl+c", "q": return m, tea.Quit case "up", "k": 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": 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", " ": 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) } } } } return m, nil } func (m model) View() string { if len(m.ui.errorTxt) > 0 { return m.ui.errorTxt } s := "" if m.ui.notification != "" { s += m.ui.notification } 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.actionListPage.cursor == i { cursor = ">" } s += fmt.Sprintf("%s %s\n", cursor, action) } } if m.ui.page == EntryList { s += fmt.Sprintf("Which entry would you like to %s\n\n", m.ui.entryListPage.actionToTake) for i, entry := range m.ui.entryListPage.entries { cursor := " " if m.ui.entryListPage.cursor == i { cursor = ">" } s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug) } } s += "\nPress q to quit.\n" return s } func main() { f, err := tea.LogToFile("debug.log", "debug") if err != nil { fmt.Println("fatal:", err) os.Exit(1) } defer f.Close() slog.Info("Starting gemlog cli") config, err := gemlog.LoadConfig() gemlog.CheckDBConnection(config) if err != nil { fmt.Printf("Error loading config: %v", err) os.Exit(1) } // TODO: check if we can reach db before starting program p := tea.NewProgram(initialModel(config)) if _, err := p.Run(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) os.Exit(1) } }