From 02ed95a6125b318d9802941016cee59481a13f96 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Fri, 3 Oct 2025 12:07:48 +0200 Subject: [PATCH] add page for reading a single gemlog entry --- actionsList.go | 5 ++++- entry.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ entryList.go | 12 ++++++++---- gemlog/db.go | 15 ++++++++------- main.go | 18 +++++++++++++----- 5 files changed, 78 insertions(+), 17 deletions(-) diff --git a/actionsList.go b/actionsList.go index 674cc5f..a6d068f 100644 --- a/actionsList.go +++ b/actionsList.go @@ -32,9 +32,12 @@ func (m ActionListPageModel) InitActionListPage() tea.Cmd { return nil } -func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageModel, tea.Cmd) { +func (m ActionListPageModel) Update(msg tea.Msg, active bool, ctx *context) (ActionListPageModel, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: + if !active { + return m, nil + } switch msg.String() { case "up", "k": if m.cursor > 0 { diff --git a/entry.go b/entry.go index e69de29..d9f25ad 100644 --- a/entry.go +++ b/entry.go @@ -0,0 +1,45 @@ +package main + +import ( + "gemini_site/gemlog" + + tea "github.com/charmbracelet/bubbletea" +) + +type EntryPageModel struct { + entry gemlog.GemlogEntry +} + +func initialEntryPageModel() EntryPageModel { + return EntryPageModel{} +} + +func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPageModel, tea.Cmd) { + switch msg := msg.(type) { + case gemlog.GemLogLoaded: + m.entry = msg.Log + case tea.KeyMsg: + if !active { + return m, nil + } + switch msg.String() { + case "left", "h": + cmd := func() tea.Msg { + return SwitchPages{Page: EntryList} + } + return m, cmd + } + } + + return m, nil + +} + +func (m EntryPageModel) View() string { + s := m.entry.Slug + s += "\n\n" + s += m.entry.Gemtxt + s += "\n\n-------------------------\n" + s += "\n\nPress h or left arrow to go back" + return s +} diff --git a/entryList.go b/entryList.go index ce76bc0..b70bc5f 100644 --- a/entryList.go +++ b/entryList.go @@ -48,20 +48,24 @@ func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (Entr } return m, cmd - case "enter", " ": + case "enter", " ", "l": id := m.entries[m.cursor].ID rev := m.entries[m.cursor].Rev switch m.actionToTake { + // TODO: handle edit + case Read: + loadCmd := gemlog.LoadGemlogCMD(ctx.config, id) + navCmd := func() tea.Msg { + return SwitchPages{Page: Entry} + } + return m, tea.Sequence(loadCmd, navCmd) case Delete: delCmd := gemlog.DeleteGemlogCMD(ctx.config, id, rev) loadCmd := gemlog.LoadGemlogsCMD(ctx.config) navCmd := func() tea.Msg { return SwitchPages{Page: ActionList} } - return m, tea.Sequence(delCmd, loadCmd, navCmd) - case Read: - return m, gemlog.LoadGemlogCMD(ctx.config, id) } } } diff --git a/gemlog/db.go b/gemlog/db.go index f8bcc8c..356a466 100644 --- a/gemlog/db.go +++ b/gemlog/db.go @@ -104,10 +104,10 @@ func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) { } var rawData struct { - ID int `json:"_id"` - Rev int `json:"_rev"` + // ID int `json:"_id"` + // Rev int `json:"_rev"` Title string `json:"title"` - GemText string `json:"gemtext"` + GemText string `json:"gemtxt"` Slug string `json:"slug"` Date time.Time `json:"date"` } @@ -117,10 +117,11 @@ func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) { } return GemlogEntry{ - Title: rawData.Title, - Slug: rawData.Slug, - Date: rawData.Date, - Tags: make([]string, 0), + Title: rawData.Title, + Slug: rawData.Slug, + Date: rawData.Date, + Gemtxt: rawData.GemText, + Tags: make([]string, 0), }, nil } diff --git a/main.go b/main.go index 953e0c8..cc2d5b3 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ type uiState struct { page Page entryListPage EntryListPageModel + entryPage EntryPageModel actionListPage ActionListPageModel } @@ -43,7 +44,7 @@ func initialModel(config *gemlog.Config) model { page: ActionList, actionListPage: initialActionListPageModel(), entryListPage: initialEntryListPageModel(), - // entryPage: initialEntryPageModel(), + entryPage: initialEntryPageModel(), }, context: &context{ config: config, @@ -59,7 +60,7 @@ func (m model) Init() tea.Cmd { } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - cmds := make([]tea.Cmd, 0) + cmds := make([]tea.Cmd, 3) switch msg := msg.(type) { case SwitchPages: m.ui.page = msg.Page @@ -74,7 +75,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } - actionListM, cmd := m.ui.actionListPage.Update(msg, m.context) + actionListM, cmd := m.ui.actionListPage.Update(msg, m.ui.page == ActionList, m.context) m.ui.actionListPage = actionListM cmds = append(cmds, cmd) @@ -82,6 +83,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.ui.entryListPage = entryListM cmds = append(cmds, cmd) + entrytM, cmd := m.ui.entryPage.Update(msg, m.ui.page == Entry, m.context) + m.ui.entryPage = entrytM + cmds = append(cmds, cmd) + return m, tea.Batch(cmds...) } @@ -94,10 +99,13 @@ func (m model) View() string { s += m.ui.notification } - if m.ui.page == ActionList { + switch m.ui.page { + case ActionList: s += m.ui.actionListPage.View() - } else if m.ui.page == EntryList { + case EntryList: s += m.ui.entryListPage.View() + case Entry: + s += m.ui.entryPage.View() } s += "\nPress q to quit.\n"