Skip to content
Myr

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

3 files+65 added−8 removed

modified:table/command.go+9 added-3 removed
3 unmodified lines
4 "encoding/csv"5 "fmt"6 "os"78 "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 lines
123124 ctx, cancel := timeout.Context(o.Timeout)125 defer cancel()126127 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 lines
142 if tm == nil {143 return fmt.Errorf("failed to get selection")144 }145139 m := tm.(model)140146 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 lines
modified:table/options.go+1 added
13 unmodified lines
14 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"`1920 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 lines
modified:table/table.go+55 added-5 removed
13 unmodified lines
14// Cherry $0.7515package table1617import (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)2324type keymap struct {25 Navigate,26 Select,27 Quit,28 Abort key.Binding29}3031// FullHelp implements help.KeyMap.32func (k keymap) FullHelp() [][]key.Binding { return nil }3334// ShortHelp implements help.KeyMap.35func (k keymap) ShortHelp() []key.Binding {36 return []key.Binding{37 k.Navigate,38 k.Select,39 k.Quit,40 }41}4243func 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}6364type model struct {65 table table.Model66 selected table.Row67 quitting bool68 showHelp bool69 help help.Model70 keymap keymap71}7273func (m model) Init() tea.Cmd { return nil }74
1 unmodified line
76 var cmd tea.Cmd7778 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 lines
99func (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}