From 4ef2a2ee8214ebbe5889fb33c4aeb5a24f4366b6 Mon Sep 17 00:00:00 2001 From: Sergio Date: Wed, 22 Nov 2023 15:19:42 +0100 Subject: [PATCH 1/3] fix: get width of printable chars of column --- table/table.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/table/table.go b/table/table.go index 36549e3b..53173f0f 100644 --- a/table/table.go +++ b/table/table.go @@ -2,6 +2,7 @@ package table import ( "strings" + "unicode" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/viewport" @@ -392,8 +393,18 @@ func (m Model) headersView() string { func (m *Model) renderRow(rowID int) string { var s = make([]string, 0, len(m.cols)) for i, value := range m.rows[rowID] { + printableValue := strings.Map(func(r rune) rune { + if unicode.IsGraphic(r) { + return r + } + return -1 + }, value) 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( + strings.Replace(value, printableValue, runewidth.Truncate(printableValue, m.cols[i].Width, "…"), 0), + ), + ) s = append(s, renderedCell) } From 1863c0f5fb22e99fbed5c670bfbceb66ee2d4802 Mon Sep 17 00:00:00 2001 From: Sergio Date: Thu, 23 Nov 2023 16:36:58 +0100 Subject: [PATCH 2/3] fix: highlight row with colors would cut highlighting --- table/table.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/table/table.go b/table/table.go index 53173f0f..acf4a094 100644 --- a/table/table.go +++ b/table/table.go @@ -400,20 +400,17 @@ func (m *Model) renderRow(rowID int) string { return -1 }, value) style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true) - renderedCell := m.styles.Cell.Render( - style.Render( - strings.Replace(value, printableValue, runewidth.Truncate(printableValue, m.cols[i].Width, "…"), 0), - ), - ) - s = append(s, renderedCell) - } - - row := lipgloss.JoinHorizontal(lipgloss.Left, s...) + renderedCell := m.styles.Cell.Render(style.Render(strings.Replace(value, printableValue, runewidth.Truncate(printableValue, m.cols[i].Width, "…"), 1))) + if rowID == m.cursor { + renderedCell = strings.ReplaceAll(renderedCell, "\033[0m", "") + s = append(s, m.styles.Selected.Render(renderedCell)) + } else { + s = append(s, renderedCell) + } - if rowID == m.cursor { - return m.styles.Selected.Render(row) } + row := lipgloss.JoinHorizontal(lipgloss.Left, s...) return row } From c65808be7079fc07d7da07b47e3ae1d0ac2ad434 Mon Sep 17 00:00:00 2001 From: Sergio Date: Thu, 23 Nov 2023 16:38:41 +0100 Subject: [PATCH 3/3] remove custom min and max fn in favor of using golang's builtin methods --- table/table.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/table/table.go b/table/table.go index acf4a094..4338b38c 100644 --- a/table/table.go +++ b/table/table.go @@ -414,22 +414,6 @@ func (m *Model) renderRow(rowID int) string { return row } -func max(a, b int) int { - if a > b { - return a - } - - return b -} - -func min(a, b int) int { - if a < b { - return a - } - - return b -} - func clamp(v, low, high int) int { return min(max(v, low), high) }