Skip to content

Commit

Permalink
fix(table): include margins in cell width (#401)
Browse files Browse the repository at this point in the history
* test(table): modify test to show bug

* fix(table): account for margin in cell height and width

* test(table): add sizing tests for margins and padding

* test(table): move TestSizing to another branch

* chore: tidy + force truecolor in test

* chore(lint): remove nil check on []string
  • Loading branch information
bashbunni authored Oct 21, 2024
1 parent 80b4221 commit d858132
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 30 deletions.
8 changes: 5 additions & 3 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,12 @@ func (t *Table) constructRow(index int, isOverflow bool) string {
cell = t.data.At(index, c)
}

cells = append(cells, t.style(index, c).
Height(height).
cellStyle := t.style(index, c)
cells = append(cells, cellStyle.
// Account for the margins in the cell sizing.
Height(height-cellStyle.GetVerticalMargins()).
MaxHeight(height).
Width(t.widths[c]).
Width(t.widths[c]-cellStyle.GetHorizontalMargins()).
MaxWidth(t.widths[c]).
Render(ansi.Truncate(cell, cellWidth*height, "…")))

Expand Down
75 changes: 52 additions & 23 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
"github.com/muesli/termenv"
)

var TableStyle = func(row, col int) lipgloss.Style {
Expand Down Expand Up @@ -1142,30 +1143,58 @@ func TestTableHeightWithOffset(t *testing.T) {
}

func TestStyleFunc(t *testing.T) {
TestStyle := func(row, col int) lipgloss.Style {
switch {
// this is the header
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
// this is the first row of data
case row == 0:
return lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Right)
default:
return lipgloss.NewStyle().Padding(0, 1)
}
lipgloss.SetColorProfile(termenv.TrueColor)
tests := []struct {
name string
style StyleFunc
}{
{
"right-aligned text with margins",
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
default:
return lipgloss.NewStyle().Margin(0, 1).Align(lipgloss.Right)
}
},
},
{
"margin and padding set",
// this test case uses background colors to differentiate margins
// and padding.
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
default:
return lipgloss.NewStyle().
Padding(1).
Margin(1).
// keeping right-aligned text as it's the most likely to
// be broken when truncated.
Align(lipgloss.Right).
Background(lipgloss.Color("#874bfc"))
}
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(tc.style).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにけは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "ΒΏQuΓ© tal?")

golden.RequireEqual(t, []byte(table.String()))
})
}

table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TestStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにけは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "ΒΏQuΓ© tal?")

golden.RequireEqual(t, []byte(table.String()))
}

func TestClearRows(t *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions table/testdata/TestStyleFunc/margin_and_padding_set.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMAL β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”‚ β”‚ β”‚
β”‚   β”‚   β”‚   β”‚
β”‚   Chinese  β”‚   Nǐn hǎo  β”‚   Nǐ hǎo  β”‚
β”‚   β”‚   β”‚   β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚
β”‚   β”‚   β”‚   β”‚
β”‚   French  β”‚   Bonjour  β”‚   Salut  β”‚
β”‚   β”‚   β”‚   β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚
β”‚   β”‚   β”‚   β”‚
β”‚  Japanese  β”‚   こんにけは  β”‚   やあ  β”‚
β”‚   β”‚   β”‚   β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚
β”‚   β”‚   β”‚   β”‚
β”‚   Russian  β”‚  Zdravstvuyte  β”‚   Privet  β”‚
β”‚   β”‚   β”‚   β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚
β”‚   β”‚   β”‚   β”‚
β”‚   Spanish  β”‚   Hola  β”‚  ΒΏQuΓ© tal?  β”‚
β”‚   β”‚   β”‚   β”‚
β”‚ β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMAL β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Chinese β”‚ Nǐn hǎo β”‚ Nǐ hǎo β”‚
β”‚ French β”‚ Bonjour β”‚ Salut β”‚
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
β”‚ Russian β”‚ Zdravstvuyte β”‚ Privet β”‚
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? β”‚
β”‚ French β”‚ Bonjour β”‚ Salut β”‚
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
β”‚ Russian β”‚ Zdravstvuyte β”‚ Privet β”‚
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

0 comments on commit d858132

Please sign in to comment.