command.go
This file
Path
table/command.go
181 lines · 4.2 KiB · Go
Last changed by
chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06
History of this fileTake 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–7why, one step back1package table23import (4 "encoding/csv"5 "fmt"6 "os"7chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 8–10why, one step back8 "charm.land/bubbles/v2/help"9 "charm.land/bubbles/v2/table"10 tea "charm.land/bubbletea/v2"chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06
lines 11–14why, one step back11 "charm.land/gum/v2/internal/stdin"12 "charm.land/gum/v2/internal/timeout"13 "charm.land/gum/v2/internal/tty"14 "charm.land/gum/v2/style"chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 15–16why, one step back15 "charm.land/lipgloss/v2"16 ltable "charm.land/lipgloss/v2/table"fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
lines 17–19why, one step back17 "golang.org/x/text/encoding"18 "golang.org/x/text/encoding/unicode"19 "golang.org/x/text/transform"feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 20–23why, one step back20)2122// Run provides a shell script interface for rendering tabular data (CSV).23func (o Options) Run() error {fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
line 24why, one step back24 var input *os.Filefeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 25why, one step back25 if o.File != "" {fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
lines 26–27why, one step back26 var err error27 input, err = os.Open(o.File)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 28why, one step back28 if err != nil {fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
line 29why, one step back29 return fmt.Errorf("could not render file: %w", err)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 30–34why, one step back30 }31 } else {32 if stdin.IsEmpty() {33 return fmt.Errorf("no data provided")34 }fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
line 35why, one step back35 input = os.Stdinfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 36why, one step back36 }fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
line 37why, one step back37 defer input.Close() //nolint: errcheckfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 38why, one step back38fix(table): ignore BOM (#757)Carlos Alexandro Beckerbefore adoptioncc71f60
lines 39–40why, one step back39 transformer := unicode.BOMOverride(encoding.Nop.NewDecoder())40 reader := csv.NewReader(transform.NewReader(input, transformer))feat(table): --lazy-quotes and --fields-per-record (#759)Carlos Alexandro Beckerbefore adoptiona8cce1c
lines 41–42why, one step back41 reader.LazyQuotes = o.LazyQuotes42 reader.FieldsPerRecord = o.FieldsPerRecordfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 43–82why, one step back43 separatorRunes := []rune(o.Separator)44 if len(separatorRunes) != 1 {45 return fmt.Errorf("separator must be single character")46 }47 reader.Comma = separatorRunes[0]4849 writer := csv.NewWriter(os.Stdout)50 writer.Comma = separatorRunes[0]5152 var columnNames []string53 var err error54 // If no columns are provided we'll use the first row of the CSV as the55 // column names.56 if len(o.Columns) <= 0 {57 columnNames, err = reader.Read()58 if err != nil {59 return fmt.Errorf("unable to parse columns")60 }61 } else {62 columnNames = o.Columns63 }6465 data, err := reader.ReadAll()66 if err != nil {67 return fmt.Errorf("invalid data provided")68 }69 columns := make([]table.Column, 0, len(columnNames))7071 for i, title := range columnNames {72 width := lipgloss.Width(title)73 if len(o.Widths) > i {74 width = o.Widths[i]75 }76 columns = append(columns, table.Column{77 Title: title,78 Width: width,79 })80 }8182 defaultStyles := table.DefaultStyles()feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 83why, one step back83 top, right, bottom, left := style.ParsePadding(o.Padding)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 84–91why, one step back8485 styles := table.Styles{86 Cell: defaultStyles.Cell.Inherit(o.CellStyle.ToLipgloss()),87 Header: defaultStyles.Header.Inherit(o.HeaderStyle.ToLipgloss()),88 Selected: o.SelectedStyle.ToLipgloss(),89 }9091 rows := make([]table.Row, 0, len(data))fix(table): grow table rows based on --columns (#760)Carlos Alexandro Beckerbefore adoption8d611cb
lines 92–93why, one step back92 for row := range data {93 if len(data[row]) > len(columns) {feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 94–95why, one step back94 return fmt.Errorf("invalid number of columns")95 }fix(table): grow table rows based on --columns (#760)Carlos Alexandro Beckerbefore adoption8d611cb
lines 96–102why, one step back9697 // fixes the data in case we have more columns than rows:98 for len(data[row]) < len(columns) {99 data[row] = append(data[row], "")100 }101102 for i, col := range data[row] {fix(table): set widths (#758)Carlos Alexandro Beckerbefore adoptionbf06fce
lines 103–109why, one step back103 if len(o.Widths) == 0 {104 width := lipgloss.Width(col)105 if width > columns[i].Width {106 columns[i].Width = width107 }108 }109 }fix(table): grow table rows based on --columns (#760)Carlos Alexandro Beckerbefore adoption8d611cb
lines 110–111why, one step back110111 rows = append(rows, table.Row(data[row]))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 112–113why, one step back112 }113Revert "feat(table): set --print if stdout is not a terminal (#762)" (#811)Carlos Alexandro Beckerbefore adoption2846d19
line 114why, one step back114 if o.Print {feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 115–126why, one step back115 table := ltable.New().116 Headers(columnNames...).117 Rows(data...).118 BorderStyle(o.BorderStyle.ToLipgloss()).119 Border(style.Border[o.Border]).120 StyleFunc(func(row, _ int) lipgloss.Style {121 if row == 0 {122 return styles.Header123 }124 return styles.Cell125 })126chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 127why, one step back127 tty.Println(table.Render())feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 128–130why, one step back128 return nil129 }130Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a
line 131why, one step back131 opts := []table.Option{feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 132–135why, one step back132 table.WithColumns(columns),133 table.WithFocused(true),134 table.WithRows(rows),135 table.WithStyles(styles),Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a
lines 136–137why, one step back136 }137 if o.Height > 0 {feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 138why, one step back138 opts = append(opts, table.WithHeight(o.Height-top-bottom))Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a
line 139why, one step back139 }feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 140why, one step back140Merge remote-tracking branch 'origin/main'Carlos Alexandro Beckerbefore adoption20a381a
line 141why, one step back141 table := table.New(opts...)feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 142why, one step back142feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 143–145why, one step back143 ctx, cancel := timeout.Context(o.Timeout)144 defer cancel()145feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8
line 146why, one step back146 m := model{rename to count instead (#840)Raphael Amorimbefore adoption5660060
lines 147–151why, one step back147 table: table,148 showHelp: o.ShowHelp,149 hideCount: o.HideCount,150 help: help.New(),151 keymap: defaultKeymap(),feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 152why, one step back152 padding: []int{top, right, bottom, left},feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8
line 153why, one step back153 }feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 154why, one step back154 tm, err := tea.NewProgram(feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8
line 155why, one step back155 m,feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 156–158why, one step back156 tea.WithOutput(os.Stderr),157 tea.WithContext(ctx),158 ).Run()feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 159–166why, one step back159 if err != nil {160 return fmt.Errorf("failed to start tea program: %w", err)161 }162163 if tm == nil {164 return fmt.Errorf("failed to get selection")165 }166feat(table): add help (#756)Carlos Alexandro Beckerbefore adoption2b090e8
line 167why, one step back167 m = tm.(model)feat(table): adds --return-column (#415)Dieter Eickstaedtbefore adoption93da22d
lines 168–175why, one step back168 if o.ReturnColumn > 0 && o.ReturnColumn <= len(m.selected) {169 if err = writer.Write([]string{m.selected[o.ReturnColumn-1]}); err != nil {170 return fmt.Errorf("failed to write col %d of selected row: %w", o.ReturnColumn, err)171 }172 } else {173 if err = writer.Write([]string(m.selected)); err != nil {174 return fmt.Errorf("failed to write selected row: %w", err)175 }feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 176–181why, one step back176 }177178 writer.Flush()179180 return nil181}