table.go
This file
Path
table/table.go
142 lines · 2.7 KiB · Go
Last changed by
chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
History of this fileTake it
1// Package table provides a shell script interface for the table bubble.2// https://github.com/charmbracelet/bubbles/tree/master/table3//4// It is useful to render tabular (CSV) data in a terminal and allows5// the user to select a row from the table.6//7// Let's render a table of gum flavors:8//9// $ gum table <<< "Flavor,Price\nStrawberry,$0.50\nBanana,$0.99\nCherry,$0.75"10//11// Flavor Price12// Strawberry $0.5013// Banana $0.9914// Cherry $0.7515package table1617import (18 "fmt"19 "strconv"2021 "charm.land/bubbles/v2/help"22 "charm.land/bubbles/v2/key"23 "charm.land/bubbles/v2/table"24 tea "charm.land/bubbletea/v2"25 "charm.land/lipgloss/v2"26)2728type keymap struct {29 Navigate,30 Select,31 Quit,32 Abort key.Binding33}3435// FullHelp implements help.KeyMap.36func (k keymap) FullHelp() [][]key.Binding { return nil }3738// ShortHelp implements help.KeyMap.39func (k keymap) ShortHelp() []key.Binding {40 return []key.Binding{41 k.Navigate,42 k.Select,43 k.Quit,44 }45}4647func defaultKeymap() keymap {48 return keymap{49 Navigate: key.NewBinding(50 key.WithKeys("up", "down"),51 key.WithHelp("↓↑", "navigate"),52 ),53 Select: key.NewBinding(54 key.WithKeys("enter"),55 key.WithHelp("enter", "select"),56 ),57 Quit: key.NewBinding(58 key.WithKeys("esc", "ctrl+q", "q"),59 key.WithHelp("esc", "quit"),60 ),61 Abort: key.NewBinding(62 key.WithKeys("ctrl+c"),63 key.WithHelp("ctrl+c", "abort"),64 ),65 }66}6768type model struct {69 table table.Model70 selected table.Row71 quitting bool72 showHelp bool73 hideCount bool74 help help.Model75 keymap keymap76 padding []int77}7879func (m model) Init() tea.Cmd { return nil }8081func (m model) countView() string {82 if m.hideCount {83 return ""84 }8586 padding := strconv.Itoa(numLen(len(m.table.Rows())))87 return m.help.Styles.FullDesc.Render(fmt.Sprintf(88 "%"+padding+"d/%d%s",89 m.table.Cursor()+1,90 len(m.table.Rows()),91 m.help.ShortSeparator,92 ))93}9495func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {96 var cmd tea.Cmd9798 switch msg := msg.(type) {99 case tea.KeyPressMsg:100 km := m.keymap101 switch {102 case key.Matches(msg, km.Select):103 m.selected = m.table.SelectedRow()104 m.quitting = true105 return m, tea.Quit106 case key.Matches(msg, km.Quit):107 m.quitting = true108 return m, tea.Quit109 case key.Matches(msg, km.Abort):110 m.quitting = true111 return m, tea.Interrupt112 }113 }114115 m.table, cmd = m.table.Update(msg)116 return m, cmd117}118119func (m model) View() tea.View {120 if m.quitting {121 return tea.NewView("")122 }123 s := m.table.View()124 if m.showHelp {125 s += "\n" + m.countView() + m.help.View(m.keymap)126 }127 return tea.NewView(lipgloss.NewStyle().128 Padding(m.padding...).129 Render(s))130}131132func numLen(i int) int {133 if i == 0 {134 return 1135 }136 count := 0137 for i != 0 {138 i /= 10139 count++140 }141 return count142}