From bf06fce1c9b2f3b3bb913856387fe26241369539 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 10 Dec 2024 21:17:53 -0300 Subject: [PATCH 1/2] fix(table): set widths (#758) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm not sure about this, as it might be slow in big tables... This will go through all rows, calculate their widths, and set it as the column width if none was provided with `-w`. ```console $ echo -e "a,b\naaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbb" | gum table a b … … $ echo -e "a,b\naaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbb" | gum table a b aaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbb $ echo -e "a,b\naaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbb" | gum table -w 5,5 a b aaaa… bbbb… ``` closes #285 --- table/command.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/table/command.go b/table/command.go index 064ee8115..c48cfe5dc 100644 --- a/table/command.go +++ b/table/command.go @@ -89,6 +89,14 @@ func (o Options) Run() error { if len(row) > len(columns) { return fmt.Errorf("invalid number of columns") } + for i, col := range row { + if len(o.Widths) == 0 { + width := lipgloss.Width(col) + if width > columns[i].Width { + columns[i].Width = width + } + } + } rows = append(rows, table.Row(row)) } From a8cce1cad945972d2a066b5646776243b3080476 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 10 Dec 2024 21:18:13 -0300 Subject: [PATCH 2/2] feat(table): --lazy-quotes and --fields-per-record (#759) As per [csv.Reader]. closes #345 --- table/command.go | 2 ++ table/options.go | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/table/command.go b/table/command.go index c48cfe5dc..08dc725ab 100644 --- a/table/command.go +++ b/table/command.go @@ -37,6 +37,8 @@ func (o Options) Run() error { transformer := unicode.BOMOverride(encoding.Nop.NewDecoder()) reader := csv.NewReader(transform.NewReader(input, transformer)) + reader.LazyQuotes = o.LazyQuotes + reader.FieldsPerRecord = o.FieldsPerRecord separatorRunes := []rune(o.Separator) if len(separatorRunes) != 1 { return fmt.Errorf("separator must be single character") diff --git a/table/options.go b/table/options.go index 1b1844a95..5949c92c4 100644 --- a/table/options.go +++ b/table/options.go @@ -8,14 +8,16 @@ import ( // Options is the customization options for the table command. type Options struct { - Separator string `short:"s" help:"Row separator" default:","` - Columns []string `short:"c" help:"Column names"` - Widths []int `short:"w" help:"Column widths"` - Height int `help:"Table height" default:"0"` - Print bool `short:"p" help:"static print" default:"false"` - File string `short:"f" help:"file path" default:""` - Border string `short:"b" help:"border style" default:"rounded" enum:"rounded,thick,normal,hidden,double,none"` - ShowHelp bool `help:"Show help keybinds" default:"true" negatable:"" env:"GUM_TABLE_SHOW_HELP"` + Separator string `short:"s" help:"Row separator" default:","` + Columns []string `short:"c" help:"Column names"` + Widths []int `short:"w" help:"Column widths"` + Height int `help:"Table height" default:"0"` + Print bool `short:"p" help:"static print" default:"false"` + File string `short:"f" help:"file path" default:""` + Border string `short:"b" help:"border style" default:"rounded" enum:"rounded,thick,normal,hidden,double,none"` + ShowHelp bool `help:"Show help keybinds" default:"true" negatable:"" env:"GUM_TABLE_SHOW_HELP"` + LazyQuotes bool `help:"If LazyQuotes is true, a quote may appear in an unquoted field and a non-doubled quote may appear in a quoted field" default:"false" env:"GUM_TABLE_LAZY_QUOTES"` + FieldsPerRecord int `help:"Sets the number of expected fields per record" default:"0" env:"GUM_TABLE_FIELDS_PER_RECORD"` BorderStyle style.Styles `embed:"" prefix:"border." envprefix:"GUM_TABLE_BORDER_"` CellStyle style.Styles `embed:"" prefix:"cell." envprefix:"GUM_TABLE_CELL_"`