feat(table): add help (#756)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Carlos Alexandro Becker · committed by GitHub · parent cc71f60 · Browse files · 3 files·+65 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Diff stats
3 files+65 added−8 removed
- other:+65 added−8 removed
Provenance
- Commit
2b090e8- Tree
eb04db1- Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
cc71f60
3 files changed+65 added−8 removed
modified:table/command.go+9 added-3 removed
3 unmodified lines4 "encoding/csv"5 "fmt"6 "os"7 8+ "github.com/charmbracelet/bubbles/help"9 "github.com/charmbracelet/bubbles/table"10 tea "github.com/charmbracelet/bubbletea"11 "github.com/charmbracelet/gum/internal/stdin"12 "github.com/charmbracelet/gum/internal/timeout"110 unmodified lines123 124 ctx, cancel := timeout.Context(o.Timeout)125 defer cancel()126 127+ m := model{128+ table: table,129+ showHelp: o.ShowHelp,130+ help: help.New(),131+ keymap: defaultKeymap(),132+ }133 tm, err := tea.NewProgram(127- model{table: table},134+ m,135 tea.WithOutput(os.Stderr),136 tea.WithContext(ctx),137 ).Run()138 if err != nil {3 unmodified lines142 if tm == nil {143 return fmt.Errorf("failed to get selection")144 }145 139- m := tm.(model)140-146+ m = tm.(model)147 if o.ReturnColumn > 0 && o.ReturnColumn <= len(m.selected) {148 if err = writer.Write([]string{m.selected[o.ReturnColumn-1]}); err != nil {149 return fmt.Errorf("failed to write col %d of selected row: %w", o.ReturnColumn, err)150 }10 unmodified linesmodified:table/options.go+1 added
13 unmodified lines14 Height int `help:"Table height" default:"0"`15 Print bool `short:"p" help:"static print" default:"false"`16 File string `short:"f" help:"file path" default:""`17 Border string `short:"b" help:"border style" default:"rounded" enum:"rounded,thick,normal,hidden,double,none"`18+ ShowHelp bool `help:"Show help keybinds" default:"true" negatable:"" env:"GUM_TABLE_SHOW_HELP"`19 20 BorderStyle style.Styles `embed:"" prefix:"border." envprefix:"GUM_TABLE_BORDER_"`21 CellStyle style.Styles `embed:"" prefix:"cell." envprefix:"GUM_TABLE_CELL_"`22 HeaderStyle style.Styles `embed:"" prefix:"header." envprefix:"GUM_TABLE_HEADER_"`4 unmodified linesmodified:table/table.go+55 added-5 removed
13 unmodified lines14 // Cherry $0.7515 package table16 17 import (18+ "github.com/charmbracelet/bubbles/help"19+ "github.com/charmbracelet/bubbles/key"20 "github.com/charmbracelet/bubbles/table"21 tea "github.com/charmbracelet/bubbletea"22 )23 24+type keymap struct {25+ Navigate,26+ Select,27+ Quit,28+ Abort key.Binding29+}30+31+// FullHelp implements help.KeyMap.32+func (k keymap) FullHelp() [][]key.Binding { return nil }33+34+// ShortHelp implements help.KeyMap.35+func (k keymap) ShortHelp() []key.Binding {36+ return []key.Binding{37+ k.Navigate,38+ k.Select,39+ k.Quit,40+ }41+}42+43+func defaultKeymap() keymap {44+ return keymap{45+ Navigate: key.NewBinding(46+ key.WithKeys("up", "down"),47+ key.WithHelp("↑↓", "navigate"),48+ ),49+ Select: key.NewBinding(50+ key.WithKeys("enter"),51+ key.WithHelp("enter", "select"),52+ ),53+ Quit: key.NewBinding(54+ key.WithKeys("esc", "ctrl+q", "q"),55+ key.WithHelp("esc", "quit"),56+ ),57+ Abort: key.NewBinding(58+ key.WithKeys("ctrl+c"),59+ key.WithHelp("ctrl+c", "abort"),60+ ),61+ }62+}63+64 type model struct {65 table table.Model66 selected table.Row67 quitting bool68+ showHelp bool69+ help help.Model70+ keymap keymap71 }72 73 func (m model) Init() tea.Cmd { return nil }74 1 unmodified line76 var cmd tea.Cmd77 78 switch msg := msg.(type) {79 case tea.KeyMsg:35- switch msg.String() {36- case "enter":80+ km := m.keymap81+ switch {82+ case key.Matches(msg, km.Select):83 m.selected = m.table.SelectedRow()84 m.quitting = true85 return m, tea.Quit40- case "q", "esc":86+ case key.Matches(msg, km.Quit):87 m.quitting = true88 return m, tea.Quit43- case "ctrl+c":89+ case key.Matches(msg, km.Abort):90 m.quitting = true91 return m, tea.Interrupt92 }93 }5 unmodified lines99 func (m model) View() string {100 if m.quitting {101 return ""102 }57- return m.table.View()103+ s := m.table.View()104+ if m.showHelp {105+ s += "\n" + m.help.View(m.keymap)106+ }107+ return s108 }