Skip to content

Commit

Permalink
fix(table): set widths (#758)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
caarlos0 authored Dec 11, 2024
1 parent 2b090e8 commit bf06fce
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions table/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down

0 comments on commit bf06fce

Please sign in to comment.