Skip to content

Commit

Permalink
Fix truncate of table cells containing ANSI
Browse files Browse the repository at this point in the history
Table cells (including the header) may contain ANSI codes. When
determining the width of these cells, ANSI was not excluded.

The `reflow` package has the same truncate function that is aware of
ANSI. This improves cell width calculation and correctly truncates.

Signed-off-by: Michael Lorant <[email protected]>
  • Loading branch information
mikelorant committed Jan 31, 2024
1 parent 92946d3 commit 14d73ee
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
"github.com/muesli/reflow/truncate"
)

// StyleFunc is the style function that determines the style of a Cell.
Expand Down Expand Up @@ -437,7 +437,7 @@ func (t *Table) constructHeaders() string {
MaxHeight(1).
Width(t.widths[i]).
MaxWidth(t.widths[i]).
Render(runewidth.Truncate(header, t.widths[i], "…")))
Render(truncate.StringWithTail(header, uint(t.widths[i]), "…")))
if i < len(t.headers)-1 && t.borderColumn {
s.WriteString(t.borderStyle.Render(t.border.Left))
}
Expand Down Expand Up @@ -488,7 +488,7 @@ func (t *Table) constructRow(index int) string {
MaxHeight(height).
Width(t.widths[c]).
MaxWidth(t.widths[c]).
Render(runewidth.Truncate(cell, t.widths[c]*height, "…")))
Render(truncate.StringWithTail(cell, uint(t.widths[c]*height), "…")))

if c < t.data.Columns()-1 && t.borderColumn {
cells = append(cells, left)
Expand Down

0 comments on commit 14d73ee

Please sign in to comment.