diff --git a/examples/bubbletea/main.go b/examples/bubbletea/main.go index 68231643..32784cad 100644 --- a/examples/bubbletea/main.go +++ b/examples/bubbletea/main.go @@ -206,7 +206,8 @@ func (m Model) View() string { } body := lipgloss.JoinHorizontal(lipgloss.Top, form, status) - footer := m.appBoundaryView(m.form.Help().ShortHelpView(m.form.KeyBinds())) + keys := m.form.KeyBindsHelp(huh.ShortHelp) + footer := m.appBoundaryView(m.form.Help().ShortHelpView(keys)) if len(errors) > 0 { footer = m.appErrorBoundaryView("") } diff --git a/field_confirm.go b/field_confirm.go index 99cffa00..e581e12d 100644 --- a/field_confirm.go +++ b/field_confirm.go @@ -119,11 +119,16 @@ func (c *Confirm) Blur() tea.Cmd { return nil } -// KeyBinds returns the help message for the confirm field. +// KeyBinds returns all keybindings for the confirm field. func (c *Confirm) KeyBinds() []key.Binding { return []key.Binding{c.keymap.Toggle, c.keymap.Prev, c.keymap.Submit, c.keymap.Next} } +// KeyBindsHelp returns the help keybindings for the confirm field. +func (c *Confirm) KeyBindsHelp(HelpFormat) []key.Binding { + return c.KeyBinds() +} + // Init initializes the confirm field. func (c *Confirm) Init() tea.Cmd { return nil diff --git a/field_input.go b/field_input.go index 5e3e868d..c361148f 100644 --- a/field_input.go +++ b/field_input.go @@ -152,7 +152,7 @@ func (i *Input) Blur() tea.Cmd { return nil } -// KeyBinds returns the help message for the input field. +// KeyBinds returns all keybindings for the input field. func (i *Input) KeyBinds() []key.Binding { if i.textinput.ShowSuggestions { return []key.Binding{i.keymap.AcceptSuggestion, i.keymap.Prev, i.keymap.Submit, i.keymap.Next} @@ -160,6 +160,11 @@ func (i *Input) KeyBinds() []key.Binding { return []key.Binding{i.keymap.Prev, i.keymap.Submit, i.keymap.Next} } +// KeyBindsHelp returns the help keybindings for the input field. +func (i *Input) KeyBindsHelp(HelpFormat) []key.Binding { + return i.KeyBinds() +} + // Init initializes the input field. func (i *Input) Init() tea.Cmd { i.textinput.Blur() diff --git a/field_multiselect.go b/field_multiselect.go index 0c10a484..16932c8a 100644 --- a/field_multiselect.go +++ b/field_multiselect.go @@ -161,11 +161,16 @@ func (m *MultiSelect[T]) Blur() tea.Cmd { return nil } -// KeyBinds returns the help message for the multi-select field. +// KeyBinds returns all keybindings for the multi-select field. func (m *MultiSelect[T]) KeyBinds() []key.Binding { return []key.Binding{m.keymap.Toggle, m.keymap.Up, m.keymap.Down, m.keymap.Filter, m.keymap.SetFilter, m.keymap.ClearFilter, m.keymap.Prev, m.keymap.Submit, m.keymap.Next} } +// KeyBindsHelp returns the help keybindings for the multi-select field. +func (m *MultiSelect[T]) KeyBindsHelp(HelpFormat) []key.Binding { + return m.KeyBinds() +} + // Init initializes the multi-select field. func (m *MultiSelect[T]) Init() tea.Cmd { return nil @@ -386,9 +391,7 @@ func (m *MultiSelect[T]) View() string { } func (m *MultiSelect[T]) printOptions() { - var ( - sb strings.Builder - ) + var sb strings.Builder sb.WriteString(m.theme.Focused.Title.Render(m.title)) sb.WriteString("\n") diff --git a/field_note.go b/field_note.go index 79a287b8..c14a30f7 100644 --- a/field_note.go +++ b/field_note.go @@ -76,11 +76,16 @@ func (n *Note) Skip() bool { return n.skip } -// KeyBinds returns the help message for the note field. +// KeyBinds returns all keybindings for the note field. func (n *Note) KeyBinds() []key.Binding { return []key.Binding{n.keymap.Prev, n.keymap.Submit, n.keymap.Next} } +// KeyBindsHelp returns the help keybindings for the note field. +func (n *Note) KeyBindsHelp(HelpFormat) []key.Binding { + return n.KeyBinds() +} + // Init initializes the note field. func (n *Note) Init() tea.Cmd { return nil diff --git a/field_select.go b/field_select.go index de88685c..a6f4f28a 100644 --- a/field_select.go +++ b/field_select.go @@ -148,11 +148,16 @@ func (s *Select[T]) Blur() tea.Cmd { return nil } -// KeyBinds returns the help keybindings for the select field. +// KeyBinds returns all keybindings for the select field. func (s *Select[T]) KeyBinds() []key.Binding { return []key.Binding{s.keymap.Up, s.keymap.Down, s.keymap.Filter, s.keymap.SetFilter, s.keymap.ClearFilter, s.keymap.Prev, s.keymap.Next, s.keymap.Submit} } +// KeyBindsHelp returns the help keybindings for the select field. +func (s *Select[T]) KeyBindsHelp(HelpFormat) []key.Binding { + return s.KeyBinds() +} + // Init initializes the select field. func (s *Select[T]) Init() tea.Cmd { return nil diff --git a/field_text.go b/field_text.go index b9c45bc8..61876e82 100644 --- a/field_text.go +++ b/field_text.go @@ -175,11 +175,16 @@ func (t *Text) Blur() tea.Cmd { return nil } -// KeyBinds returns the help message for the text field. +// KeyBinds returns all keybindings for the text field. func (t *Text) KeyBinds() []key.Binding { return []key.Binding{t.keymap.NewLine, t.keymap.Editor, t.keymap.Prev, t.keymap.Submit, t.keymap.Next} } +// KeyBindsHelp returns the help keybindings for the text field. +func (t *Text) KeyBindsHelp(HelpFormat) []key.Binding { + return t.KeyBinds() +} + type updateValueMsg []byte // Init initializes the text field. diff --git a/form.go b/form.go index 99c1ae16..95585303 100644 --- a/form.go +++ b/form.go @@ -116,9 +116,12 @@ type Field interface { // Skip returns whether this input should be skipped or not. Skip() bool - // KeyBinds returns help keybindings. + // KeyBinds returns all keybindings. KeyBinds() []key.Binding + // KeyBindsHelp returns the help keybindings. + KeyBindsHelp(HelpFormat) []key.Binding + // WithTheme sets the theme on a field. WithTheme(*Theme) Field @@ -144,6 +147,17 @@ type Field interface { GetValue() any } +// HelpFormat is the way in which help is presented. +type HelpFormat int + +const ( + // FullHelp is the full-size help format. + FullHelp HelpFormat = iota + + // ShortHelp is the one-line summary help format. + ShortHelp +) + // FieldPosition is positional information about the given field and form. type FieldPosition struct { Group int @@ -324,6 +338,12 @@ func (f *Form) KeyBinds() []key.Binding { return group.fields[group.paginator.Page].KeyBinds() } +// KeyBindsHelp returns the current fields' help keybinds. +func (f *Form) KeyBindsHelp(format HelpFormat) []key.Binding { + group := f.groups[f.paginator.Page] + return group.fields[group.paginator.Page].KeyBindsHelp(format) +} + // Get returns a result from the form. func (f *Form) Get(key string) any { return f.results[key] diff --git a/group.go b/group.go index 131af167..da711cad 100644 --- a/group.go +++ b/group.go @@ -287,7 +287,8 @@ func (g *Group) View() string { g.viewport.SetContent(fields.String() + "\n") if g.showHelp && len(errors) <= 0 { - view.WriteString(g.help.ShortHelpView(g.fields[g.paginator.Page].KeyBinds())) + keys := g.fields[g.paginator.Page].KeyBindsHelp(ShortHelp) + view.WriteString(g.help.ShortHelpView(keys)) } if !g.showErrors {