-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
103 lines (91 loc) · 2.42 KB
/
builder.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"io"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
type Item string
func (i Item) FilterValue() string { return "" }
type ItemDelegate struct{}
func (d ItemDelegate) Height() int { return 1 }
func (d ItemDelegate) Spacing() int { return 0 }
func (d ItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d ItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(Item)
if !ok {
return
}
str := fmt.Sprintf("%d. %s", index+1, i)
fn := ItemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return SelectedItemStyle.Render("> " + strings.Join(s, " "))
}
}
fmt.Fprint(w, fn(str))
}
func BuildItems(branches []string) []list.Item {
var items []list.Item
for _, branch := range branches {
items = append(items, Item(branch))
}
return items
}
func ListBuilder(items []list.Item) list.Model {
l := list.New(items, ItemDelegate{}, DefaultWidth, ListHeight)
l.Title = TitleDescription
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = TitleStyle
l.Styles.PaginationStyle = PaginationStyle
l.Styles.HelpStyle = HelpStyle
l.KeyMap = defaultKeyMap()
return l
}
func defaultKeyMap() list.KeyMap {
return list.KeyMap{
// Browsing.
CursorUp: key.NewBinding(
key.WithKeys("up", "k", "shift+tab"),
key.WithHelp("↑/k", "up"),
),
CursorDown: key.NewBinding(
key.WithKeys("down", "j", "tab"),
key.WithHelp("↓/j", "down"),
),
PrevPage: key.NewBinding(
key.WithKeys("left", "h", "pgup", "b", "u"),
key.WithHelp("←/h/pgup", "prev page"),
),
NextPage: key.NewBinding(
key.WithKeys("right", "l", "pgdown", "f", "d"),
key.WithHelp("→/l/pgdn", "next page"),
),
GoToStart: key.NewBinding(
key.WithKeys("home", "g"),
key.WithHelp("g/home", "go to start"),
),
GoToEnd: key.NewBinding(
key.WithKeys("end", "G"),
key.WithHelp("G/end", "go to end"),
),
// Toggle help.
ShowFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "more"),
),
CloseFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "close help"),
),
// Quitting.
Quit: key.NewBinding(
key.WithKeys("q", "esc"),
key.WithHelp("q", "quit"),
),
ForceQuit: key.NewBinding(key.WithKeys("ctrl+c")),
}
}