footer.go

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