add page for reading a single gemlog entry
This commit is contained in:
parent
8414414f98
commit
02ed95a612
5 changed files with 78 additions and 17 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
45
entry.go
45
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
|
||||
}
|
||||
12
entryList.go
12
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
gemlog/db.go
15
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
|
||||
}
|
||||
|
||||
|
|
|
|||
18
main.go
18
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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue