1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package main
import (
"encoding/csv"
"fmt"
"os"
"os/exec"
"strings"
"time"
tea "charm.land/bubbletea/v2"
)
type model struct {
choices [][]string
cursor int
selected map[int]struct{}
}
func listModel(arg string) model {
out, err := exec.Command("hugo", "list", arg).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)
}
// remove the table head row
records = records[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
}
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)
}
|