-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.go
246 lines (222 loc) · 7.42 KB
/
model.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package boa
import (
"bytes"
"strings"
"unicode"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
)
const (
spacebar = " "
)
// cmdModel Implements tea.Model. It provides an interactive
// help and usage tui component for bubbletea programs.
type cmdModel struct {
styles *Styles
list list.Model
viewport *viewport.Model
cmd *cobra.Command
subCmds []list.Item
print bool
cmdChain string
// Store window height to adjust viewport on command selection changes
windowHeight int
// Store full height of content for given view, updated on command change
contentHeight int
errorWriter *bytes.Buffer
}
// newCmdModel initializes a based on values supplied from cmd *cobra.Command
func newCmdModel(options *options, cmd *cobra.Command) *cmdModel {
subCmds := getSubCommands(cmd)
l := newSubCmdsList(options.styles, subCmds)
vp := viewport.New(0, 0)
vp.KeyMap = viewPortKeyMap()
m := &cmdModel{
styles: options.styles,
cmd: cmd,
subCmds: subCmds,
list: l,
viewport: &vp,
errorWriter: options.errorWriter,
}
m.contentHeight = lipgloss.Height(m.usage())
return m
}
// Init is the initial cmd to be executed which is nil for this component.
func (m *cmdModel) Init() tea.Cmd {
return nil
}
// Update is called when a message is received.
func (m *cmdModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var listCmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.windowHeight = msg.Height
m.viewport.Width = msg.Width
m.viewport.Height = m.windowHeight - lipgloss.Height(m.footer())
if m.viewport.Height > m.contentHeight {
m.viewport.Height = m.contentHeight
}
// Scroll viewport back to top for new screen
m.viewport.SetYOffset(0)
return m, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "q", "ctrl+c":
return m, tea.Quit
case "enter":
i, ok := m.list.SelectedItem().(item)
if ok {
m.cmd = i.cmd
subCmds := getSubCommands(m.cmd)
m.list = newSubCmdsList(m.styles, subCmds)
m.viewport.Height = m.windowHeight - lipgloss.Height(m.footer())
// Update new content height and check viewport size
m.contentHeight = lipgloss.Height(m.usage())
if m.viewport.Height > m.contentHeight {
m.viewport.Height = m.contentHeight
}
// Scroll viewport back to top for new screen
m.viewport.SetYOffset(0)
}
return m, nil
case "b":
if m.cmd.HasParent() {
m.cmd = m.cmd.Parent()
subCmds := getSubCommands(m.cmd)
m.list = newSubCmdsList(m.styles, subCmds)
m.viewport.Height = m.windowHeight - lipgloss.Height(m.footer())
// Update new content height and check viewport size
m.contentHeight = lipgloss.Height(m.usage())
if m.viewport.Height > m.contentHeight {
m.viewport.Height = m.contentHeight
}
// Scroll viewport back to top for new screen
m.viewport.SetYOffset(0)
}
return m, nil
case "p":
m.print = true
m.cmdChain = print("", m.cmd)
return m, tea.Quit
}
}
m.list, listCmd = m.list.Update(msg)
newViewport, viewPortCmd := m.viewport.Update(msg)
// point to new viewport
m.viewport = &newViewport
m.viewport.KeyMap = viewPortKeyMap()
if m.viewport.Height > m.contentHeight {
m.viewport.Height = m.contentHeight
}
cmds = append(cmds, listCmd, viewPortCmd)
return m, tea.Batch(cmds...)
}
// View renders the program's UI, which is just a string.
func (m *cmdModel) View() string {
m.viewport.SetContent(m.usage())
return lipgloss.JoinVertical(lipgloss.Top, m.viewport.View(), m.footer())
}
// usage builds the usage body from a cobra command
func (m *cmdModel) usage() string {
usageText := strings.Builder{}
if m.errorWriter != nil && m.errorWriter.Len() > 0 {
usageText.WriteString(m.styles.ErrorText.Render(m.errorWriter.String() + "\n"))
}
cmdTitle := ""
cmdName := m.cmd.Name()
if m.cmd.Version != "" {
cmdName += " " + m.cmd.Version
}
cmdName = m.styles.Section.Render(cmdName)
cmdLong := m.styles.SubTitle.Render(m.cmd.Long)
cmdTitle = m.styles.Title.Foreground(lipgloss.AdaptiveColor{Light: darkGrey, Dark: white}).
Render(lipgloss.JoinVertical(lipgloss.Center, cmdName, cmdLong))
usageText.WriteString(cmdTitle + "\n")
cmdSection := m.styles.Section.Render("Cmd Description:")
short := m.styles.Text.Render(m.cmd.Short)
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, cmdSection, short) + "\n")
if m.cmd.Runnable() {
usage := m.styles.Section.Render("Usage:")
useLine := m.styles.Text.Render(m.cmd.UseLine())
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, usage, useLine) + "\n")
if m.cmd.HasAvailableSubCommands() {
commandPath := m.styles.Text.Render(m.cmd.CommandPath() + " [command]")
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, commandPath) + "\n")
}
}
if len(m.cmd.Aliases) > 0 {
aliases := m.styles.Section.Render("Aliases:")
nameAndAlias := m.styles.Text.Render(m.cmd.NameAndAliases())
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, aliases, nameAndAlias) + "\n")
}
if m.cmd.HasAvailableLocalFlags() {
localFlags := m.styles.Section.Render("Flags:")
flagUsage := m.styles.Text.Render(strings.TrimRightFunc(m.cmd.LocalFlags().FlagUsages(), unicode.IsSpace))
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, localFlags, flagUsage) + "\n")
}
if m.cmd.HasAvailableInheritedFlags() {
globalFlags := m.styles.Section.Render("Global Flags:")
flagUsage := m.styles.Text.Render(strings.TrimRightFunc(m.cmd.InheritedFlags().FlagUsages(), unicode.IsSpace))
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, globalFlags, flagUsage) + "\n")
}
if m.cmd.HasExample() {
examples := m.styles.Section.Render("Examples:")
example := m.styles.Text.Render(m.cmd.Example)
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, examples, example) + "\n")
}
if m.cmd.HasAvailableSubCommands() {
usageText.WriteString(lipgloss.JoinVertical(lipgloss.Left, m.list.View()))
}
return m.styles.Border.Render(usageText.String() + "\n")
}
// footer outputs the footer of the viewport and contains help text.
func (m *cmdModel) footer() string {
var help, scroll string
help = m.styles.Info.Render("↑/k up • ↓/j down • / to filter • p to print • b to go back • enter to select • q, ctrl+c to quit")
// If content is larger than the window minus the size of the necessary footer then it will be in a scrollable viewport
if m.contentHeight > m.windowHeight-2 {
scroll = m.styles.Info.Render("ctrl+k up • ctrl+j down • mouse to scroll")
}
return lipgloss.JoinVertical(lipgloss.Left, help, scroll)
}
// print outputs the command chain for a given cobra command.
func print(v string, cmd *cobra.Command) string {
if cmd != nil {
v = cmd.Name() + " " + v
if !cmd.HasParent() {
// final result
return "Command: " + v
}
// recursively walk cmd chain
return print(v, cmd.Parent())
}
return v
}
func viewPortKeyMap() viewport.KeyMap {
return viewport.KeyMap{
PageDown: key.NewBinding(
key.WithKeys("pgdown", spacebar, "f"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup", "v"),
),
HalfPageUp: key.NewBinding(
key.WithKeys("u", "ctrl+u"),
),
HalfPageDown: key.NewBinding(
key.WithKeys("d", "ctrl+d"),
),
Up: key.NewBinding(
key.WithKeys("ctrl+k"),
),
Down: key.NewBinding(
key.WithKeys("ctrl+j"),
),
}
}