footer.go

 1package footer
 2
 3import (
 4	"strings"
 5
 6	"github.com/charmbracelet/bubbles/help"
 7	"github.com/charmbracelet/bubbles/key"
 8	tea "github.com/charmbracelet/bubbletea"
 9	"github.com/charmbracelet/soft-serve/ui/common"
10)
11
12// Footer is a Bubble Tea model that displays help and other info.
13type Footer struct {
14	common common.Common
15	help   help.Model
16	keymap help.KeyMap
17}
18
19// New creates a new Footer.
20func New(c common.Common, keymap help.KeyMap) *Footer {
21	h := help.New()
22	h.Styles.ShortKey = c.Styles.HelpKey
23	h.Styles.ShortDesc = c.Styles.HelpValue
24	h.Styles.FullKey = c.Styles.HelpKey
25	h.Styles.FullDesc = c.Styles.HelpValue
26	f := &Footer{
27		common: c,
28		help:   h,
29		keymap: keymap,
30	}
31	f.SetSize(c.Width, c.Height)
32	return f
33}
34
35// SetSize implements common.Component.
36func (f *Footer) SetSize(width, height int) {
37	f.common.SetSize(width, height)
38	f.help.Width = width
39}
40
41// Init implements tea.Model.
42func (f *Footer) Init() tea.Cmd {
43	return nil
44}
45
46// Update implements tea.Model.
47func (f *Footer) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
48	return f, nil
49}
50
51// View implements tea.Model.
52func (f *Footer) View() string {
53	if f.keymap == nil {
54		return ""
55	}
56	s := f.common.Styles.Footer.Copy().Width(f.common.Width)
57	helpView := f.help.View(f.keymap)
58	return s.Render(helpView)
59}
60
61// ShortHelp returns the short help key bindings.
62func (f *Footer) ShortHelp() []key.Binding {
63	return f.keymap.ShortHelp()
64}
65
66// FullHelp returns the full help key bindings.
67func (f *Footer) FullHelp() [][]key.Binding {
68	return f.keymap.FullHelp()
69}
70
71// ShowAll returns whether the full help is shown.
72func (f *Footer) ShowAll() bool {
73	return f.help.ShowAll
74}
75
76// SetShowAll sets whether the full help is shown.
77func (f *Footer) SetShowAll(show bool) {
78	f.help.ShowAll = show
79}
80
81// Height returns the height of the footer.
82func (f *Footer) Height() int {
83	return len(strings.Split(f.View(), "\n"))
84}