Skip to content
Myr

command.go

This file

Path

table/command.go

180 lines · 4.2 KiB · Go

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 19a93b0 point at it.

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 1–7why, one step back
1package table
23import (4 "encoding/csv"5 "fmt"6 "os"7

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

line 8why, one step back
8 "github.com/charmbracelet/bubbles/help"

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 9–11why, one step back
9 "github.com/charmbracelet/bubbles/table"
10 tea "github.com/charmbracelet/bubbletea"11 "github.com/charmbracelet/gum/internal/stdin"

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

line 12why, one step back
12 "github.com/charmbracelet/gum/internal/timeout"

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 13why, one step back
13 "github.com/charmbracelet/gum/style"

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

lines 14–15why, one step back
14 "github.com/charmbracelet/lipgloss"
15 ltable "github.com/charmbracelet/lipgloss/table"

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

lines 16–18why, one step back
16 "golang.org/x/text/encoding"
17 "golang.org/x/text/encoding/unicode"18 "golang.org/x/text/transform"

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 19–22why, one step back
19)
2021// Run provides a shell script interface for rendering tabular data (CSV).22func (o Options) Run() error {

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

line 23why, one step back
23 var input *os.File

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 24why, one step back
24 if o.File != "" {

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

lines 25–26why, one step back
25 var err error
26 input, err = os.Open(o.File)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 27why, one step back
27 if err != nil {

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

line 28why, one step back
28 return fmt.Errorf("could not render file: %w", err)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 29–33why, one step back
29 }
30 } else {31 if stdin.IsEmpty() {32 return fmt.Errorf("no data provided")33 }

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

line 34why, one step back
34 input = os.Stdin

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 35why, one step back
35 }

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

line 36why, one step back
36 defer input.Close() //nolint: errcheck

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 37why, one step back
37

fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60

lines 38–39why, one step back
38 transformer := unicode.BOMOverride(encoding.Nop.NewDecoder())
39 reader := csv.NewReader(transform.NewReader(input, transformer))

feat(table): --lazy-quotes and --fields-per-record (#759)Carlos Alexandro Beckerbefore adoptiona8cce1c

lines 40–41why, one step back
40 reader.LazyQuotes = o.LazyQuotes
41 reader.FieldsPerRecord = o.FieldsPerRecord

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 42–81why, one step back
42 separatorRunes := []rune(o.Separator)
43 if len(separatorRunes) != 1 {44 return fmt.Errorf("separator must be single character")45 }46 reader.Comma = separatorRunes[0]4748 writer := csv.NewWriter(os.Stdout)49 writer.Comma = separatorRunes[0]5051 var columnNames []string52 var err error53 // If no columns are provided we'll use the first row of the CSV as the54 // column names.55 if len(o.Columns) <= 0 {56 columnNames, err = reader.Read()57 if err != nil {58 return fmt.Errorf("unable to parse columns")59 }60 } else {61 columnNames = o.Columns62 }6364 data, err := reader.ReadAll()65 if err != nil {66 return fmt.Errorf("invalid data provided")67 }68 columns := make([]table.Column, 0, len(columnNames))6970 for i, title := range columnNames {71 width := lipgloss.Width(title)72 if len(o.Widths) > i {73 width = o.Widths[i]74 }75 columns = append(columns, table.Column{76 Title: title,77 Width: width,78 })79 }8081 defaultStyles := table.DefaultStyles()

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

line 82why, one step back
82 top, right, bottom, left := style.ParsePadding(o.Padding)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 83–90why, one step back
83
84 styles := table.Styles{85 Cell: defaultStyles.Cell.Inherit(o.CellStyle.ToLipgloss()),86 Header: defaultStyles.Header.Inherit(o.HeaderStyle.ToLipgloss()),87 Selected: o.SelectedStyle.ToLipgloss(),88 }8990 rows := make([]table.Row, 0, len(data))

fix(table): grow table rows based on --columns (#760)Carlos Alexandro Beckerbefore adoption8d611cb

lines 91–92why, one step back
91 for row := range data {
92 if len(data[row]) > len(columns) {

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 93–94why, one step back
93 return fmt.Errorf("invalid number of columns")
94 }

fix(table): grow table rows based on --columns (#760)Carlos Alexandro Beckerbefore adoption8d611cb

lines 95–101why, one step back
95
96 // fixes the data in case we have more columns than rows:97 for len(data[row]) < len(columns) {98 data[row] = append(data[row], "")99 }100101 for i, col := range data[row] {

fix(table): set widths (#758)Carlos Alexandro Beckerbefore adoptionbf06fce

lines 102–108why, one step back
102 if len(o.Widths) == 0 {
103 width := lipgloss.Width(col)104 if width > columns[i].Width {105 columns[i].Width = width106 }107 }108 }

fix(table): grow table rows based on --columns (#760)Carlos Alexandro Beckerbefore adoption8d611cb

lines 109–110why, one step back
109
110 rows = append(rows, table.Row(data[row]))

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 111–112why, one step back
111 }
112

Revert "feat(table): set --print if stdout is not a terminal (#762)" (#811)Carlos Alexandro Beckerbefore adoption2846d19

line 113why, one step back
113 if o.Print {

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 114–129why, one step back
114 table := ltable.New().
115 Headers(columnNames...).116 Rows(data...).117 BorderStyle(o.BorderStyle.ToLipgloss()).118 Border(style.Border[o.Border]).119 StyleFunc(func(row, _ int) lipgloss.Style {120 if row == 0 {121 return styles.Header122 }123 return styles.Cell124 })125126 fmt.Println(table.Render())127 return nil128 }129

Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a

line 130why, one step back
130 opts := []table.Option{

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 131–134why, one step back
131 table.WithColumns(columns),
132 table.WithFocused(true),133 table.WithRows(rows),134 table.WithStyles(styles),

Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a

lines 135–136why, one step back
135 }
136 if o.Height > 0 {

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

line 137why, one step back
137 opts = append(opts, table.WithHeight(o.Height-top-bottom))

Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a

line 138why, one step back
138 }

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

line 139why, one step back
139

Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a

line 140why, one step back
140 table := table.New(opts...)

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

line 141why, one step back
141

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

lines 142–144why, one step back
142 ctx, cancel := timeout.Context(o.Timeout)
143 defer cancel()144

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

line 145why, one step back
145 m := model{

rename to count instead (#840)Raphael Amorimbefore adoption5660060

lines 146–150why, one step back
146 table: table,
147 showHelp: o.ShowHelp,148 hideCount: o.HideCount,149 help: help.New(),150 keymap: defaultKeymap(),

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

line 151why, one step back
151 padding: []int{top, right, bottom, left},

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

line 152why, one step back
152 }

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

line 153why, one step back
153 tm, err := tea.NewProgram(

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

line 154why, one step back
154 m,

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

lines 155–157why, one step back
155 tea.WithOutput(os.Stderr),
156 tea.WithContext(ctx),157 ).Run()

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 158–165why, one step back
158 if err != nil {
159 return fmt.Errorf("failed to start tea program: %w", err)160 }161162 if tm == nil {163 return fmt.Errorf("failed to get selection")164 }165

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

line 166why, one step back
166 m = tm.(model)

feat(table): adds --return-column (#415)Dieter Eickstaedtbefore adoption93da22d

lines 167–174why, one step back
167 if o.ReturnColumn > 0 && o.ReturnColumn <= len(m.selected) {
168 if err = writer.Write([]string{m.selected[o.ReturnColumn-1]}); err != nil {169 return fmt.Errorf("failed to write col %d of selected row: %w", o.ReturnColumn, err)170 }171 } else {172 if err = writer.Write([]string(m.selected)); err != nil {173 return fmt.Errorf("failed to write selected row: %w", err)174 }

refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0

lines 175–180why, one step back
175 }
176177 writer.Flush()178179 return nil180}