From ffc0c726a4736d3b0b837582fe145bff0e58366d Mon Sep 17 00:00:00 2001 From: eltrac Date: Wed, 4 Mar 2026 10:58:37 +0800 Subject: refactor: rename model -> model_drafts --- model.go | 76 --------------------------------------------------------- model_drafts.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 76 deletions(-) delete mode 100644 model.go create mode 100644 model_drafts.go diff --git a/model.go b/model.go deleted file mode 100644 index c796e98..0000000 --- a/model.go +++ /dev/null @@ -1,76 +0,0 @@ -package main - -import ( - "encoding/csv" - "os" - "os/exec" - "strings" - - tea "charm.land/bubbletea/v2" -) - -type model struct { - choices [][]string - cursor int - selected map[int]struct{} -} - -func initialModel() model { - out, err := exec.Command("hugo", "list", "drafts").Output() - if err != nil { - println("Error fetching Hugo drafts: Is this a Hugo site? Is Hugo installed?") - os.Exit(1) - } - - csvReader := csv.NewReader(strings.NewReader(string(out))) - records, err := csvReader.ReadAll() - if err != nil { - println("Error parsing draft list.") - os.Exit(1) - } - if len(records) <= 1 { - println("No drafts found.") - os.Exit(1) - } - - return model{ - choices: records, - selected: make(map[int]struct{}), - } -} - -func (m model) Init() tea.Cmd { - return nil -} - -func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - case tea.KeyPressMsg: - switch msg.String() { - case "ctrl+c", "q": - return m, tea.Quit - case "up", "k": - if m.cursor > 0 { - m.cursor-- - } - case "down", "j": - if m.cursor < len(m.choices)-1 { - m.cursor++ - } - case "enter": - choice := m.choices[m.cursor] - path := choice[0] - return m, openEditor(path) - case "space": - _, ok := m.selected[m.cursor] - if ok { - delete(m.selected, m.cursor) - } else { - m.selected[m.cursor] = struct{}{} - } - } - case editorFinishedMsg: - return m, tea.Quit - } - return m, nil -} diff --git a/model_drafts.go b/model_drafts.go new file mode 100644 index 0000000..c796e98 --- /dev/null +++ b/model_drafts.go @@ -0,0 +1,76 @@ +package main + +import ( + "encoding/csv" + "os" + "os/exec" + "strings" + + tea "charm.land/bubbletea/v2" +) + +type model struct { + choices [][]string + cursor int + selected map[int]struct{} +} + +func initialModel() model { + out, err := exec.Command("hugo", "list", "drafts").Output() + if err != nil { + println("Error fetching Hugo drafts: Is this a Hugo site? Is Hugo installed?") + os.Exit(1) + } + + csvReader := csv.NewReader(strings.NewReader(string(out))) + records, err := csvReader.ReadAll() + if err != nil { + println("Error parsing draft list.") + os.Exit(1) + } + if len(records) <= 1 { + println("No drafts found.") + os.Exit(1) + } + + return model{ + choices: records, + selected: make(map[int]struct{}), + } +} + +func (m model) Init() tea.Cmd { + return nil +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyPressMsg: + switch msg.String() { + case "ctrl+c", "q": + return m, tea.Quit + case "up", "k": + if m.cursor > 0 { + m.cursor-- + } + case "down", "j": + if m.cursor < len(m.choices)-1 { + m.cursor++ + } + case "enter": + choice := m.choices[m.cursor] + path := choice[0] + return m, openEditor(path) + case "space": + _, ok := m.selected[m.cursor] + if ok { + delete(m.selected, m.cursor) + } else { + m.selected[m.cursor] = struct{}{} + } + } + case editorFinishedMsg: + return m, tea.Quit + } + return m, nil +} -- cgit v1.2.3