refine navigation and loading enteries
This commit is contained in:
parent
99548e67b7
commit
8414414f98
10 changed files with 148 additions and 41 deletions
|
|
@ -44,7 +44,7 @@ func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageMo
|
||||||
if m.cursor < len(actions)-1 {
|
if m.cursor < len(actions)-1 {
|
||||||
m.cursor++
|
m.cursor++
|
||||||
}
|
}
|
||||||
case "enter", " ":
|
case "enter", " ", "l":
|
||||||
action := actions[m.cursor]
|
action := actions[m.cursor]
|
||||||
switch action {
|
switch action {
|
||||||
case Write:
|
case Write:
|
||||||
|
|
@ -56,7 +56,7 @@ func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageMo
|
||||||
actionToTakeCmd := func() tea.Msg {
|
actionToTakeCmd := func() tea.Msg {
|
||||||
return SelectActionToTake{ActionToTake: action}
|
return SelectActionToTake{ActionToTake: action}
|
||||||
}
|
}
|
||||||
loadGemLogsCmd := gemlog.LoadGemlogCMD(ctx.config)
|
loadGemLogsCmd := gemlog.LoadGemlogsCMD(ctx.config)
|
||||||
return m, tea.Batch(switchPageCmd, loadGemLogsCmd, actionToTakeCmd)
|
return m, tea.Batch(switchPageCmd, loadGemLogsCmd, actionToTakeCmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
bruno/Gemlog/Get Gemlog.bru
Normal file
20
bruno/Gemlog/Get Gemlog.bru
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
meta {
|
||||||
|
name: Get Gemlog
|
||||||
|
type: http
|
||||||
|
seq: 5
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: http://eisenhorn:5023/gemlog/d198245cbc0d67891f3d3d6dc301c242
|
||||||
|
body: json
|
||||||
|
auth: basic
|
||||||
|
}
|
||||||
|
|
||||||
|
auth:basic {
|
||||||
|
username: gemlog-cli
|
||||||
|
password: {{pw}}
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
}
|
||||||
33
bruno/Gemlog/Update Gemlog.bru
Normal file
33
bruno/Gemlog/Update Gemlog.bru
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
meta {
|
||||||
|
name: Update Gemlog
|
||||||
|
type: http
|
||||||
|
seq: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
put {
|
||||||
|
url: http://eisenhorn:5023/gemlog/d198245cbc0d67891f3d3d6dc301c242?rev=2-e997af4bb2b1b18ce224ebcb46d145dc
|
||||||
|
body: json
|
||||||
|
auth: basic
|
||||||
|
}
|
||||||
|
|
||||||
|
params:query {
|
||||||
|
rev: 2-e997af4bb2b1b18ce224ebcb46d145dc
|
||||||
|
}
|
||||||
|
|
||||||
|
auth:basic {
|
||||||
|
username: gemlog-cli
|
||||||
|
password: {{pw}}
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"title": "Bruno Test Updated",
|
||||||
|
"slug": "bruno-test",
|
||||||
|
"date": "2025-09-30T21:04:45.092Z",
|
||||||
|
"gemtxt": "this is a test\n\nfrom bruno\nwith someupdated data"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
}
|
||||||
0
entry.go
Normal file
0
entry.go
Normal file
49
entryList.go
49
entryList.go
|
|
@ -15,18 +15,14 @@ type EntryListPageModel struct {
|
||||||
|
|
||||||
type SelectActionToTake struct{ ActionToTake Action }
|
type SelectActionToTake struct{ ActionToTake Action }
|
||||||
|
|
||||||
func InitialEntryListPageModel() EntryListPageModel {
|
func initialEntryListPageModel() EntryListPageModel {
|
||||||
return EntryListPageModel{
|
return EntryListPageModel{
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
actionToTake: Read,
|
actionToTake: Read,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m EntryListPageModel) InitEntryListPage() tea.Cmd {
|
func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryListPageModel, tea.Cmd) {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageModel, tea.Cmd) {
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case SelectActionToTake:
|
case SelectActionToTake:
|
||||||
m.actionToTake = msg.ActionToTake
|
m.actionToTake = msg.ActionToTake
|
||||||
|
|
@ -34,6 +30,9 @@ func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageMode
|
||||||
m.entries = msg.Logs
|
m.entries = msg.Logs
|
||||||
return m, nil
|
return m, nil
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
|
if !active {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "up", "k":
|
case "up", "k":
|
||||||
if m.cursor > 0 {
|
if m.cursor > 0 {
|
||||||
|
|
@ -43,33 +42,27 @@ func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageMode
|
||||||
if m.cursor < len(actions)-1 {
|
if m.cursor < len(actions)-1 {
|
||||||
m.cursor++
|
m.cursor++
|
||||||
}
|
}
|
||||||
|
case "left", "h":
|
||||||
|
cmd := func() tea.Msg {
|
||||||
|
return SwitchPages{Page: ActionList}
|
||||||
|
}
|
||||||
|
return m, cmd
|
||||||
|
|
||||||
case "enter", " ":
|
case "enter", " ":
|
||||||
id := m.entries[m.cursor].ID
|
id := m.entries[m.cursor].ID
|
||||||
|
rev := m.entries[m.cursor].Rev
|
||||||
switch m.actionToTake {
|
switch m.actionToTake {
|
||||||
case Delete:
|
case Delete:
|
||||||
return m, gemlog.DeleteGemlogCMD(ctx.config, id)
|
delCmd := gemlog.DeleteGemlogCMD(ctx.config, id, rev)
|
||||||
|
loadCmd := gemlog.LoadGemlogsCMD(ctx.config)
|
||||||
|
navCmd := func() tea.Msg {
|
||||||
|
return SwitchPages{Page: ActionList}
|
||||||
}
|
}
|
||||||
|
|
||||||
// action := actions[m.cursor]
|
return m, tea.Sequence(delCmd, loadCmd, navCmd)
|
||||||
// switch action {
|
case Read:
|
||||||
// case Write:
|
return m, gemlog.LoadGemlogCMD(ctx.config, id)
|
||||||
// return m, gemlog.WritePostCMD(ctx.config)
|
}
|
||||||
// case Read:
|
|
||||||
// m.ui.page = EntryList
|
|
||||||
// m.ui.entryListPage.cursor = 0
|
|
||||||
// m.ui.entryListPage.actionToTake = Read
|
|
||||||
// return m, gemlog.LoadGemlogCMD(ctx.config)
|
|
||||||
// case Edit:
|
|
||||||
// m.ui.page = EntryList
|
|
||||||
// m.ui.entryListPage.cursor = 0
|
|
||||||
// m.ui.entryListPage.actionToTake = Edit
|
|
||||||
// return m, gemlog.LoadGemlogCMD(ctx.config)
|
|
||||||
// case Delete:
|
|
||||||
// m.ui.page = EntryList
|
|
||||||
// m.ui.entryListPage.cursor = 0
|
|
||||||
// m.ui.entryListPage.actionToTake = Delete
|
|
||||||
// return m, gemlog.LoadGemlogCMD(m.context.config)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,5 +78,7 @@ func (m EntryListPageModel) View() string {
|
||||||
}
|
}
|
||||||
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
|
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s += "\n\n Press h or left arrow to go back"
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,4 @@ package gemlog
|
||||||
type Notification string
|
type Notification string
|
||||||
type ErrorMsg struct{ err error }
|
type ErrorMsg struct{ err error }
|
||||||
type GemLogsLoaded struct{ Logs []GemlogListEntry }
|
type GemLogsLoaded struct{ Logs []GemlogListEntry }
|
||||||
|
type GemLogLoaded struct{ Log GemlogEntry }
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ type GemlogListEntry struct {
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
Rev string `json:"rev"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUUID generates a new UUID v4 using crypto/rand
|
// NewUUID generates a new UUID v4 using crypto/rand
|
||||||
|
|
|
||||||
50
gemlog/db.go
50
gemlog/db.go
|
|
@ -51,6 +51,7 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
|
Rev string `json:"rev"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
} `json:"value"`
|
} `json:"value"`
|
||||||
} `json:"rows"`
|
} `json:"rows"`
|
||||||
|
|
@ -68,6 +69,7 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) {
|
||||||
Title: row.Value.Title,
|
Title: row.Value.Title,
|
||||||
Slug: row.Value.Slug,
|
Slug: row.Value.Slug,
|
||||||
Date: row.Value.Date,
|
Date: row.Value.Date,
|
||||||
|
Rev: row.Value.Rev,
|
||||||
}
|
}
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
}
|
}
|
||||||
|
|
@ -76,8 +78,54 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) {
|
||||||
return entries, nil
|
return entries, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteGemlogEntry(config *Config, id string) error {
|
func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) {
|
||||||
url := fmt.Sprintf("%s:%d/gemlog/%s", config.CouchDB.Host, config.CouchDB.Port, id)
|
url := fmt.Sprintf("%s:%d/gemlog/%s", config.CouchDB.Host, config.CouchDB.Port, id)
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return GemlogEntry{}, fmt.Errorf("failed to create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password))
|
||||||
|
req.Header.Add("content-type", "application/json")
|
||||||
|
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return GemlogEntry{}, fmt.Errorf("failed to send request: %w", err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return GemlogEntry{}, fmt.Errorf("failed to read response body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||||
|
return GemlogEntry{}, fmt.Errorf("unexpected status code %d: %s", res.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawData struct {
|
||||||
|
ID int `json:"_id"`
|
||||||
|
Rev int `json:"_rev"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
GemText string `json:"gemtext"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
|
Date time.Time `json:"date"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(body, &rawData); err != nil {
|
||||||
|
return GemlogEntry{}, fmt.Errorf("failed to parse response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return GemlogEntry{
|
||||||
|
Title: rawData.Title,
|
||||||
|
Slug: rawData.Slug,
|
||||||
|
Date: rawData.Date,
|
||||||
|
Tags: make([]string, 0),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteGemlogEntry(config *Config, id string, rev string) error {
|
||||||
|
url := fmt.Sprintf("%s:%d/gemlog/%s?rev=%s", config.CouchDB.Host, config.CouchDB.Port, id, rev)
|
||||||
req, err := http.NewRequest("DELETE", url, nil)
|
req, err := http.NewRequest("DELETE", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create request: %w", err)
|
return fmt.Errorf("failed to create request: %w", err)
|
||||||
|
|
|
||||||
|
|
@ -6,17 +6,28 @@ import (
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeleteGemlogCMD(config *Config, id string) tea.Cmd {
|
func DeleteGemlogCMD(config *Config, id string, rev string) tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
err := DeleteGemlogEntry(config, id)
|
err := DeleteGemlogEntry(config, id, rev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrorMsg{err}
|
return ErrorMsg{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Notification(fmt.Sprintf("Gemlog with id: %s deleted", id))
|
return Notification(fmt.Sprintf("Gemlog with id: %s deleted", id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadGemlogCMD(config *Config) tea.Cmd {
|
func LoadGemlogCMD(config *Config, id string) tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
log, err := ReadGemlogEntry(config, id)
|
||||||
|
if err != nil {
|
||||||
|
return ErrorMsg{err}
|
||||||
|
}
|
||||||
|
return GemLogLoaded{Log: log}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadGemlogsCMD(config *Config) tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
logs, err := listGemLogs(config)
|
logs, err := listGemLogs(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
10
main.go
10
main.go
|
|
@ -14,7 +14,7 @@ type Page string
|
||||||
const (
|
const (
|
||||||
ActionList Page = "actionList"
|
ActionList Page = "actionList"
|
||||||
EntryList Page = "entryList"
|
EntryList Page = "entryList"
|
||||||
// Entry Page = "entry"
|
Entry Page = "entry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SwitchPages struct{ Page Page }
|
type SwitchPages struct{ Page Page }
|
||||||
|
|
@ -41,11 +41,9 @@ func initialModel(config *gemlog.Config) model {
|
||||||
return model{
|
return model{
|
||||||
ui: uiState{
|
ui: uiState{
|
||||||
page: ActionList,
|
page: ActionList,
|
||||||
// entryListPage: entryListPageModel{
|
|
||||||
// cursor: 0,
|
|
||||||
// entries: []gemlog.GemlogListEntry{},
|
|
||||||
// },
|
|
||||||
actionListPage: initialActionListPageModel(),
|
actionListPage: initialActionListPageModel(),
|
||||||
|
entryListPage: initialEntryListPageModel(),
|
||||||
|
// entryPage: initialEntryPageModel(),
|
||||||
},
|
},
|
||||||
context: &context{
|
context: &context{
|
||||||
config: config,
|
config: config,
|
||||||
|
|
@ -80,7 +78,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
m.ui.actionListPage = actionListM
|
m.ui.actionListPage = actionListM
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
|
|
||||||
entryListM, cmd := m.ui.entryListPage.Update(msg, m.context)
|
entryListM, cmd := m.ui.entryListPage.Update(msg, m.ui.page == EntryList, m.context)
|
||||||
m.ui.entryListPage = entryListM
|
m.ui.entryListPage = entryListM
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue