-
Notifications
You must be signed in to change notification settings - Fork 4
/
sl.go
80 lines (71 loc) · 1.73 KB
/
sl.go
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
package sl
import (
"fmt"
"os"
h "github.com/beetcb/ghdl/helper"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type model struct {
choices []string // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected int // which to-do items are selected
}
func initialModel(choices *[]string) model {
return model{
choices: *choices,
selected: -1,
}
}
func (m model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return nil
}
func (m *model) 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.choices)-1 {
m.cursor++
}
case "enter":
m.selected = m.cursor
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
blue, printWidth := lipgloss.Color("14"), 60
paddingS := lipgloss.NewStyle().PaddingLeft(2).Width(printWidth)
colorS := paddingS.Copy().
Foreground(blue).BorderLeft(true).BorderForeground(blue)
s := h.Sprint("multiple options after filtering, please select asset manually", h.SprintOptions{PrintWidth: 80}) + "\n"
if m.selected == -1 {
for i, choice := range m.choices {
if m.cursor == i {
s += colorS.Render(choice) + "\n"
} else {
s += paddingS.Render(choice) + "\n"
}
}
// Send the UI for rendering
return s + "\n"
} else {
return s
}
}
func Select(choices *[]string) int {
state := initialModel(choices)
p := tea.NewProgram(&state)
if err := p.Start(); err != nil {
h.Println(fmt.Sprintf("Alas, there's been an error: %v", err), h.PrintModeErr)
os.Exit(1)
}
return state.selected
}