feat: add HelpView to table as a convenience

This commit is contained in:
Christian Rocha
2024-08-20 15:22:27 -04:00
parent c1d9f7dd6b
commit 235075af49
+11 -1
View File
@@ -3,6 +3,7 @@ package table
import ( import (
"strings" "strings"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -13,6 +14,7 @@ import (
// Model defines a state for the table widget. // Model defines a state for the table widget.
type Model struct { type Model struct {
KeyMap KeyMap KeyMap KeyMap
Help help.Model
cols []Column cols []Column
rows []Row rows []Row
@@ -35,7 +37,7 @@ type Column struct {
} }
// KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which // KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which
// is used to render the menu. // is used to render the help menu.
type KeyMap struct { type KeyMap struct {
LineUp key.Binding LineUp key.Binding
LineDown key.Binding LineDown key.Binding
@@ -134,6 +136,7 @@ func New(opts ...Option) Model {
viewport: viewport.New(0, 20), viewport: viewport.New(0, 20),
KeyMap: DefaultKeyMap(), KeyMap: DefaultKeyMap(),
Help: help.New(),
styles: DefaultStyles(), styles: DefaultStyles(),
} }
@@ -251,6 +254,13 @@ func (m Model) View() string {
return m.headersView() + "\n" + m.viewport.View() return m.headersView() + "\n" + m.viewport.View()
} }
// HelpView is a helper method for rendering the help menu from the keymap.
// 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)
}
// UpdateViewport updates the list content based on the previously defined // UpdateViewport updates the list content based on the previously defined
// columns and rows. // columns and rows.
func (m *Model) UpdateViewport() { func (m *Model) UpdateViewport() {