diff options
| author | eltrac <hi@eltr.ac> | 2026-03-04 11:17:06 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-03-04 11:17:06 +0800 |
| commit | 1941516829e25b513f3be173c54fa563c534b2d5 (patch) | |
| tree | eca427bf6dfff448389142ff0d3fcafff5cd01ac | |
| parent | 4d2843cbdc04098fef4141179f00fdacdb63bcc4 (diff) | |
refactor: move View() to model_list
I didn't quite understand the architecture of Bubble Tea before, now I
know I need to put Init, Update and View in the same model
| -rw-r--r-- | model_list.go | 31 | ||||
| -rw-r--r-- | view.go | 37 |
2 files changed, 31 insertions, 37 deletions
diff --git a/model_list.go b/model_list.go index 12f2238..8ffdb49 100644 --- a/model_list.go +++ b/model_list.go @@ -2,9 +2,11 @@ package main import ( "encoding/csv" + "fmt" "os" "os/exec" "strings" + "time" tea "charm.land/bubbletea/v2" ) @@ -76,3 +78,32 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m, nil } + +func (m model) View() tea.View { + s := "Unpublished drafts of this Hugo site:\n\n" + + for i, choice := range m.choices { + cursor := " " // no cursor + if m.cursor == i { + cursor = ">" + } + + checked := " " // not checked + if _, ok := m.selected[i]; ok { + checked = "x" + } + + title := choice[2] + t, err := time.Parse(time.RFC3339, choice[3]) + if err != nil { + + } + datetime := t.Format("2006-01-02 15:04") + + s += fmt.Sprintf("%s [%s] %s \t %s\n", cursor, checked, datetime, title) + } + + s += "\n[q] quit | [Enter] edit | [SPACE] select | [↑/k] up | [↓/j] down\n" + + return tea.NewView(s) +} diff --git a/view.go b/view.go deleted file mode 100644 index 4ee430e..0000000 --- a/view.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "fmt" - "time" - - tea "charm.land/bubbletea/v2" -) - -func (m model) View() tea.View { - s := "Unpublished drafts of this Hugo site:\n\n" - - for i, choice := range m.choices { - cursor := " " // no cursor - if m.cursor == i { - cursor = ">" - } - - checked := " " // not checked - if _, ok := m.selected[i]; ok { - checked = "x" - } - - title := choice[2] - t, err := time.Parse(time.RFC3339, choice[3]) - if err != nil { - - } - datetime := t.Format("2006-01-02 15:04") - - s += fmt.Sprintf("%s [%s] %s \t %s\n", cursor, checked, datetime, title) - } - - s += "\n[q] quit | [Enter] edit | [SPACE] select | [↑/k] up | [↓/j] down\n" - - return tea.NewView(s) -} |
