Skip to content

Commit

Permalink
feat: ShowHelp optional
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 24, 2023
1 parent 573a004 commit 2ac0fbb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
8 changes: 7 additions & 1 deletion field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (m *MultiSelect[T]) Focus() tea.Cmd {
// Blur blurs the multi-select field.
func (m *MultiSelect[T]) Blur() tea.Cmd {
m.focused = false
m.err = m.validate(*m.value)
return nil
}

Expand Down Expand Up @@ -143,9 +142,15 @@ func (m *MultiSelect[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.selected[m.cursor] = !m.selected[m.cursor]
case key.Matches(msg, m.keymap.Prev):
m.finalize()
if m.err != nil {
return m, nil
}
return m, prevField
case key.Matches(msg, m.keymap.Next):
m.finalize()
if m.err != nil {
return m, nil
}
return m, nextField
}
}
Expand All @@ -170,6 +175,7 @@ func (m *MultiSelect[T]) finalize() {
*m.value = append(*m.value, option.Value)
}
}
m.err = m.validate(*m.value)
}

// View renders the multi-select field.
Expand Down
9 changes: 9 additions & 0 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Form struct {
groups []*Group
paginator paginator.Model
accessible bool
showHelp bool
quitting bool
theme *Theme
keymap *KeyMap
Expand Down Expand Up @@ -91,6 +92,14 @@ func (f *Form) KeyMap(keymap *KeyMap) *Form {
return f
}

// ShowHelp sets whether to show help on a form.
func (f *Form) ShowHelp(v bool) *Form {
for _, group := range f.groups {
group.ShowHelp(v)
}
return f
}

// Init initializes the form.
func (f *Form) Init() tea.Cmd {
var cmds []tea.Cmd
Expand Down
24 changes: 17 additions & 7 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Group struct {
description string
current int

help help.Model
showHelp bool
help help.Model

theme *Theme
keymap *KeyMap
Expand All @@ -24,9 +25,10 @@ type Group struct {
// NewGroup creates a new group with the given fields.
func NewGroup(fields ...Field) *Group {
return &Group{
fields: fields,
current: 0,
help: help.New(),
fields: fields,
current: 0,
help: help.New(),
showHelp: true,
}
}

Expand All @@ -42,6 +44,12 @@ func (g *Group) Description(description string) *Group {
return g
}

// ShowHelp sets whether or not the group's help should be shown.
func (g *Group) ShowHelp(showHelp bool) *Group {
g.showHelp = showHelp
return g
}

// Theme sets the theme on a group.
func (g *Group) Theme(t *Theme) *Group {
g.theme = t
Expand Down Expand Up @@ -154,9 +162,11 @@ func (g *Group) View() string {
}
}

s.WriteString(gap)
s.WriteString(g.theme.Focused.Help.Render(g.help.ShortHelpView(g.fields[g.current].KeyBinds())))
s.WriteString("\n")
if g.showHelp {
s.WriteString(gap)
s.WriteString(g.theme.Focused.Help.Render(g.help.ShortHelpView(g.fields[g.current].KeyBinds())))
s.WriteString("\n")
}

for _, err := range g.Errors() {
s.WriteString("\n")
Expand Down

0 comments on commit 2ac0fbb

Please sign in to comment.