Skip to content

Commit

Permalink
add (Field).KeyBindsHelp
Browse files Browse the repository at this point in the history
  • Loading branch information
ardnew committed Jan 26, 2024
1 parent 382fbd1 commit 788d2c4
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 12 deletions.
3 changes: 2 additions & 1 deletion examples/bubbletea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
}
Expand Down
7 changes: 6 additions & 1 deletion field_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,19 @@ 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}
}
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()
Expand Down
11 changes: 7 additions & 4 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion field_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion field_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 21 additions & 1 deletion form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 788d2c4

Please sign in to comment.