Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 6837ed2 point at it.

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 1–17why, one step back
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 (

feat: show indicator on help keybindings (opt-in) (#839)Raphael Amorimbefore adoption984c84f

line 18why, one step back
18 "fmt"

fix(table): padding on item indicator (#841)Carlos Alexandro Beckerbefore adoptiond795b8a

line 19why, one step back
19 "strconv"

feat: show indicator on help keybindings (opt-in) (#839)Raphael Amorimbefore adoption984c84f

line 20why, one step back
20

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

lines 21–25why, one step back
21 "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"

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 26–27why, one step back
26)
27

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

lines 28–50why, one step back
28type 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"),

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

line 51why, one step back
51 key.WithHelp("↓↑", "navigate"),

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

lines 52–67why, one step back
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}67

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 68why, one step back
68type model struct {

rename to count instead (#840)Raphael Amorimbefore adoption5660060

lines 69–75why, one step back
69 table table.Model
70 selected table.Row71 quitting bool72 showHelp bool73 hideCount bool74 help help.Model75 keymap keymap

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 76why, one step back
76 padding []int

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 77–78why, one step back
77}
78

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 79why, one step back
79func (m model) Init() tea.Cmd { return nil }

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 80why, one step back
80

rename to count instead (#840)Raphael Amorimbefore adoption5660060

lines 81–82why, one step back
81func (m model) countView() string {
82 if m.hideCount {

feat: show indicator on help keybindings (opt-in) (#839)Raphael Amorimbefore adoption984c84f

lines 83–85why, one step back
83 return ""
84 }85

fix(table): padding on item indicator (#841)Carlos Alexandro Beckerbefore adoptiond795b8a

lines 86–92why, one step back
86 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 ))

feat: show indicator on help keybindings (opt-in) (#839)Raphael Amorimbefore adoption984c84f

lines 93–94why, one step back
93}
94

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 95–98why, one step back
95func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
96 var cmd tea.Cmd9798 switch msg := msg.(type) {

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 99why, one step back
99 case tea.KeyPressMsg:

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

lines 100–102why, one step back
100 km := m.keymap
101 switch {102 case key.Matches(msg, km.Select):

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 103–105why, one step back
103 m.selected = m.table.SelectedRow()
104 m.quitting = true105 return m, tea.Quit

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

line 106why, one step back
106 case key.Matches(msg, km.Quit):

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 107–108why, one step back
107 m.quitting = true
108 return m, tea.Quit

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

line 109why, one step back
109 case key.Matches(msg, km.Abort):

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

lines 110–111why, one step back
110 m.quitting = true
111 return m, tea.Interrupt

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 112–118why, one step back
112 }
113 }114115 m.table, cmd = m.table.Update(msg)116 return m, cmd117}118

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 119why, one step back
119func (m model) View() tea.View {

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 120why, one step back
120 if m.quitting {

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 121why, one step back
121 return tea.NewView("")

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 122why, one step back
122 }

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

lines 123–124why, one step back
123 s := m.table.View()
124 if m.showHelp {

rename to count instead (#840)Raphael Amorimbefore adoption5660060

line 125why, one step back
125 s += "\n" + m.countView() + m.help.View(m.keymap)

feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8

line 126why, one step back
126 }

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 127why, one step back
127 return tea.NewView(lipgloss.NewStyle().

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 128why, one step back
128 Padding(m.padding...).

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 129why, one step back
129 Render(s))

fix(table): padding on item indicator (#841)Carlos Alexandro Beckerbefore adoptiond795b8a

lines 130–141why, one step back
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 count

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 142why, one step back
142}