footer.go

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