-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist.go
More file actions
79 lines (66 loc) · 1.46 KB
/
list.go
File metadata and controls
79 lines (66 loc) · 1.46 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
package tui
import (
"fmt"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"os"
)
type ListModel struct {
}
type Item struct {
Ititle, Desc string
}
func (i Item) Title() string { return i.Ititle }
func (i Item) Description() string { return i.Desc }
func (i Item) FilterValue() string { return i.Ititle }
type listModel struct {
list list.Model
}
func (m listModel) Init() tea.Cmd {
return nil
}
func (m listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc, tea.KeyCtrlQ:
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := DocStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m listModel) View() string {
m.list.SetShowHelp(false)
return DocStyle.Render(m.list.View())
}
func Newlist(items []list.Item) *listModel {
return &listModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
}
}
func (m listModel) SetSpacing(i int) *listModel {
d := list.NewDefaultDelegate()
d.SetSpacing(i)
m.list.SetDelegate(d)
return &m
}
func (m listModel) SetHeight(i int) *listModel {
m.list.SetHeight(i)
return &m
}
func (m listModel) Run() error {
p := tea.NewProgram(m)
_, err := p.Run()
if err != nil {
return err
}
fmt.Printf(HelpStyle("<Press enter to exit>\n"))
os.Stdin.Write([]byte("\n"))
ClearLines(1)
return nil
}