-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpick.go
More file actions
105 lines (89 loc) · 1.93 KB
/
pick.go
File metadata and controls
105 lines (89 loc) · 1.93 KB
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
package main
import (
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type pickModel struct {
entries []ChangelogEntry
displayName string
cursor int
selected int
quitting bool
}
func (m pickModel) Init() tea.Cmd {
return nil
}
func (m pickModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.entries)-1 {
m.cursor++
}
case "enter":
m.selected = m.cursor
m.quitting = true
return m, tea.Quit
case "q", "esc", "ctrl+c":
m.selected = -1
m.quitting = true
return m, tea.Quit
}
}
return m, nil
}
func (m pickModel) View() string {
if m.quitting {
return ""
}
bold := lipgloss.NewStyle().Bold(true)
dim := lipgloss.NewStyle().Faint(true)
var b strings.Builder
for i, entry := range m.entries {
date := "-"
ago := "-"
if !entry.ReleasedAt.IsZero() {
date = entry.ReleasedAt.Format("2006-01-02")
ago = formatRelativeTime(entry.ReleasedAt)
}
pointer := " "
if i == m.cursor {
pointer = "> "
}
line := fmt.Sprintf("%s%-12s %-12s %s", pointer, entry.Version, date, ago)
if i == m.cursor {
line = bold.Render(line)
}
b.WriteString(line)
b.WriteString("\n")
}
b.WriteString("\n")
b.WriteString(dim.Render(" ↑/↓ navigate • enter select • q cancel"))
b.WriteString("\n")
return b.String()
}
func runPickCommand(displayName string, entries []ChangelogEntry) {
model := pickModel{
entries: entries,
displayName: displayName,
selected: -1,
}
p := tea.NewProgram(model)
result, err := p.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
final := result.(pickModel)
if final.selected >= 0 && final.selected < len(entries) {
outputRendered(displayName, &entries[final.selected])
}
}