-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmdlist.go
72 lines (62 loc) · 2.03 KB
/
cmdlist.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
package boa
import (
"fmt"
"io"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
)
// screen length of the list
const listHeight = 10
// item is the object that will appear in our list
type item struct {
cmd *cobra.Command
}
func (i item) FilterValue() string { return i.cmd.Name() }
// itemDelegate encapsulates the general functionality for all list items
type itemDelegate struct {
styles *Styles
}
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(msg tea.Msg, m *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 := i.cmd.Name() + lipgloss.NewStyle().Bold(true).
PaddingLeft(i.cmd.NamePadding()-len(i.cmd.Name())+1).Render(i.cmd.Short)
fn := d.styles.Item.Render
if index == m.Index() {
fn = func(strs ...string) string {
return d.styles.SelectedItem.Render("> " + fmt.Sprint(strs))
}
}
fmt.Fprint(w, fn(str))
}
// newSubCmdsList returns a new list.Model filled with the values in []list.Items
func newSubCmdsList(styles *Styles, items []list.Item) list.Model {
l := list.New(items, itemDelegate{styles: styles}, 0, listHeight)
l.Styles.TitleBar.Padding(0, 0)
l.Styles.Title = styles.Section
l.Title = "Available Sub Commands:"
l.SetShowHelp(false)
l.SetShowStatusBar(false)
l.SetShowPagination(false)
return l
}
// getSubCommands returns a []list.Item filled with any available sub command from the supplied *cobra.Command.
// This does not follow the command chain past a depth of 1.
func getSubCommands(c *cobra.Command) []list.Item {
subs := make([]list.Item, 0)
if c.HasAvailableSubCommands() {
for _, subcmd := range c.Commands() {
if subcmd.Name() == "help" || subcmd.IsAvailableCommand() {
subs = append(subs, item{cmd: subcmd})
}
}
}
return subs
}