-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathpicker.go
More file actions
101 lines (85 loc) · 2.65 KB
/
Copy pathpicker.go
File metadata and controls
101 lines (85 loc) · 2.65 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
package cli
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// ─── Styles ─────────────────────────────────────────────────────────
var (
titleStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12"))
itemStyle = lipgloss.NewStyle().PaddingLeft(2)
selectedStyle = lipgloss.NewStyle().PaddingLeft(0).Foreground(lipgloss.Color("10")).Bold(true)
cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
subtleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
)
// ─── Picker model ───────────────────────────────────────────────────
type pickerModel struct {
title string
items []string
cursor int
choice string
quitting bool
}
func newPickerModel(title string, items []string) pickerModel {
return pickerModel{
title: title,
items: items,
}
}
func (m pickerModel) Init() tea.Cmd { return nil }
func (m pickerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
m.quitting = true
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.items)-1 {
m.cursor++
}
case "enter":
m.choice = m.items[m.cursor]
return m, tea.Quit
}
}
return m, nil
}
func (m pickerModel) View() string {
if m.quitting && m.choice == "" {
return subtleStyle.Render("Cancelled.") + "\n"
}
if m.choice != "" {
return ""
}
s := titleStyle.Render(m.title) + "\n\n"
for i, item := range m.items {
if i == m.cursor {
s += cursorStyle.Render("▸ ") + selectedStyle.Render(item) + "\n"
} else {
s += itemStyle.Render(" "+item) + "\n"
}
}
s += "\n" + subtleStyle.Render("↑/↓ navigate • enter select • q quit") + "\n"
return s
}
// ─── Public API ─────────────────────────────────────────────────────
// RunPicker launches an interactive Bubble Tea list picker and returns the
// selected item, or an empty string if the user cancelled.
func RunPicker(title string, items []string) (string, error) {
if len(items) == 0 {
return "", fmt.Errorf("no items to pick from")
}
m := newPickerModel(title, items)
p := tea.NewProgram(m)
result, err := p.Run()
if err != nil {
return "", err
}
final := result.(pickerModel)
return final.choice, nil
}