fix(table): use ansi.Truncate instead of runewidth.Truncate (#884)

`runewidth.Truncate` does not consider terminal escape characters - this
means that if a table cell contains funky characters, then it will be
incorrectly truncated.

Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
Justin Chadwell
2026-02-13 09:45:48 -03:00
committed by GitHub
parent cc76d4176d
commit d004225e8c
2 changed files with 18 additions and 3 deletions
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"charm.land/bubbles/v2/viewport"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/mattn/go-runewidth"
"github.com/charmbracelet/x/ansi"
)
// Model defines a state for the table widget.
@@ -422,7 +422,7 @@ func (m Model) headersView() string {
continue
}
style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
renderedCell := style.Render(ansi.Truncate(col.Title, col.Width, "…"))
s = append(s, m.styles.Header.Render(renderedCell))
}
return lipgloss.JoinHorizontal(lipgloss.Top, s...)
@@ -435,7 +435,7 @@ func (m *Model) renderRow(r int) string {
continue
}
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
renderedCell := m.styles.Cell.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
renderedCell := m.styles.Cell.Render(style.Render(ansi.Truncate(value, m.cols[i].Width, "…")))
s = append(s, renderedCell)
}