Skip to content

Commit

Permalink
feat(table): additional help keys
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalkaoz committed Jan 29, 2025
1 parent 0f9e38c commit 336d71f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
36 changes: 33 additions & 3 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (

// Model defines a state for the table widget.
type Model struct {
KeyMap KeyMap
Help help.Model
KeyMap KeyMap
additionalShortHelpKeys []key.Binding
additionalFullHelpKeys [][]key.Binding

Help help.Model

cols []Column
rows []Row
Expand Down Expand Up @@ -198,6 +201,22 @@ func WithKeyMap(km KeyMap) Option {
}
}

// WithAdditionalShortHelpKeys adds additional keys to the short help.
// you need to handle the keys itself!
func WithAdditionalShortHelpKeys(kbs []key.Binding) Option {
return func(m *Model) {
m.additionalShortHelpKeys = kbs
}
}

// WithAdditionalFullHelpKeys adds additional keys to the full help.
// you need to handle the keys itself!
func WithAdditionalFullHelpKeys(kbs [][]key.Binding) Option {
return func(m *Model) {
m.additionalFullHelpKeys = kbs
}
}

// Update is the Bubble Tea update loop.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
Expand Down Expand Up @@ -256,7 +275,18 @@ func (m Model) View() string {
// Note that this view is not rendered by default and you must call it
// manually in your application, where applicable.
func (m Model) HelpView() string {
return m.Help.View(m.KeyMap)
if m.Help.ShowAll {
var kbs [][]key.Binding
kbs = append(kbs, m.KeyMap.FullHelp()...)
kbs = append(kbs, m.additionalFullHelpKeys...)
return m.Help.FullHelpView(kbs)
}

var kbs []key.Binding
kbs = append(kbs, m.KeyMap.ShortHelp()...)
kbs = append(kbs, m.additionalShortHelpKeys...)

return m.Help.ShortHelpView(kbs)
}

// UpdateViewport updates the list content based on the previously defined
Expand Down
45 changes: 45 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package table

import (
"github.com/charmbracelet/bubbles/key"
"strings"
"testing"

"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -157,3 +159,46 @@ func TestTableAlignment(t *testing.T) {
golden.RequireEqual(t, []byte(got))
})
}

func TestAdditionalKeys(t *testing.T) {
biscuits := New(
WithHeight(5),
WithColumns([]Column{
{Title: "Name", Width: 25},
{Title: "Country of Origin", Width: 16},
{Title: "Dunk-able", Width: 12},
}),
WithRows([]Row{
{"Chocolate Digestives", "UK", "Yes"},
{"Tim Tams", "Australia", "No"},
{"Hobnobs", "UK", "Yes"},
}),
WithAdditionalShortHelpKeys([]key.Binding{
key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "shortkey")),
}),
WithAdditionalFullHelpKeys([][]key.Binding{
{
key.NewBinding(key.WithKeys("f"), key.WithHelp("s", "fullkey")),
},
}),
)

// short help
if !strings.Contains(biscuits.HelpView(), "shortkey") {
t.Fatalf("Error: short help view to contain '%s'", "shortkey")
}
if strings.Contains(biscuits.HelpView(), "fullkey") {
t.Fatalf("Error: short help should not contain '%s'", "fullkey")
}

// full help
biscuits.Help.ShowAll = true
if strings.Contains(biscuits.HelpView(), "shortkey") {
t.Fatalf("Error: full help should not contain '%s'", "shortkey")
}

if !strings.Contains(biscuits.HelpView(), "fullkey") {
t.Fatalf("Error: short help view to contain '%s'", "fullkey")
}

}

0 comments on commit 336d71f

Please sign in to comment.