Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): additional help keys #720

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}

}