Skip to content

Commit d858132

Browse files
authored
fix(table): include margins in cell width (#401)
* 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
1 parent 80b4221 commit d858132

File tree

4 files changed

+90
-30
lines changed

4 files changed

+90
-30
lines changed

β€Žtable/table.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,12 @@ func (t *Table) constructRow(index int, isOverflow bool) string {
552552
cell = t.data.At(index, c)
553553
}
554554

555-
cells = append(cells, t.style(index, c).
556-
Height(height).
555+
cellStyle := t.style(index, c)
556+
cells = append(cells, cellStyle.
557+
// Account for the margins in the cell sizing.
558+
Height(height-cellStyle.GetVerticalMargins()).
557559
MaxHeight(height).
558-
Width(t.widths[c]).
560+
Width(t.widths[c]-cellStyle.GetHorizontalMargins()).
559561
MaxWidth(t.widths[c]).
560562
Render(ansi.Truncate(cell, cellWidth*height, "…")))
561563

β€Žtable/table_test.go

+52-23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/charmbracelet/lipgloss"
99
"github.com/charmbracelet/x/ansi"
1010
"github.com/charmbracelet/x/exp/golden"
11+
"github.com/muesli/termenv"
1112
)
1213

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

11441145
func TestStyleFunc(t *testing.T) {
1145-
TestStyle := func(row, col int) lipgloss.Style {
1146-
switch {
1147-
// this is the header
1148-
case row == HeaderRow:
1149-
return lipgloss.NewStyle().Align(lipgloss.Center)
1150-
// this is the first row of data
1151-
case row == 0:
1152-
return lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Right)
1153-
default:
1154-
return lipgloss.NewStyle().Padding(0, 1)
1155-
}
1146+
lipgloss.SetColorProfile(termenv.TrueColor)
1147+
tests := []struct {
1148+
name string
1149+
style StyleFunc
1150+
}{
1151+
{
1152+
"right-aligned text with margins",
1153+
func(row, col int) lipgloss.Style {
1154+
switch {
1155+
case row == HeaderRow:
1156+
return lipgloss.NewStyle().Align(lipgloss.Center)
1157+
default:
1158+
return lipgloss.NewStyle().Margin(0, 1).Align(lipgloss.Right)
1159+
}
1160+
},
1161+
},
1162+
{
1163+
"margin and padding set",
1164+
// this test case uses background colors to differentiate margins
1165+
// and padding.
1166+
func(row, col int) lipgloss.Style {
1167+
switch {
1168+
case row == HeaderRow:
1169+
return lipgloss.NewStyle().Align(lipgloss.Center)
1170+
default:
1171+
return lipgloss.NewStyle().
1172+
Padding(1).
1173+
Margin(1).
1174+
// keeping right-aligned text as it's the most likely to
1175+
// be broken when truncated.
1176+
Align(lipgloss.Right).
1177+
Background(lipgloss.Color("#874bfc"))
1178+
}
1179+
},
1180+
},
1181+
}
1182+
1183+
for _, tc := range tests {
1184+
t.Run(tc.name, func(t *testing.T) {
1185+
table := New().
1186+
Border(lipgloss.NormalBorder()).
1187+
StyleFunc(tc.style).
1188+
Headers("LANGUAGE", "FORMAL", "INFORMAL").
1189+
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
1190+
Row("French", "Bonjour", "Salut").
1191+
Row("Japanese", "こんにけは", "やあ").
1192+
Row("Russian", "Zdravstvuyte", "Privet").
1193+
Row("Spanish", "Hola", "ΒΏQuΓ© tal?")
1194+
1195+
golden.RequireEqual(t, []byte(table.String()))
1196+
})
11561197
}
1157-
1158-
table := New().
1159-
Border(lipgloss.NormalBorder()).
1160-
StyleFunc(TestStyle).
1161-
Headers("LANGUAGE", "FORMAL", "INFORMAL").
1162-
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
1163-
Row("French", "Bonjour", "Salut").
1164-
Row("Japanese", "こんにけは", "やあ").
1165-
Row("Russian", "Zdravstvuyte", "Privet").
1166-
Row("Spanish", "Hola", "ΒΏQuΓ© tal?")
1167-
1168-
golden.RequireEqual(t, []byte(table.String()))
11691198
}
11701199

11711200
func TestClearRows(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
2+
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMAL β”‚
3+
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
4+
β”‚ β”‚ β”‚ β”‚
5+
β”‚   β”‚   β”‚   β”‚
6+
β”‚   Chinese  β”‚   Nǐn hǎo  β”‚   Nǐ hǎo  β”‚
7+
β”‚   β”‚   β”‚   β”‚
8+
β”‚ β”‚ β”‚ β”‚
9+
β”‚ β”‚ β”‚ β”‚
10+
β”‚   β”‚   β”‚   β”‚
11+
β”‚   French  β”‚   Bonjour  β”‚   Salut  β”‚
12+
β”‚   β”‚   β”‚   β”‚
13+
β”‚ β”‚ β”‚ β”‚
14+
β”‚ β”‚ β”‚ β”‚
15+
β”‚   β”‚   β”‚   β”‚
16+
β”‚  Japanese  β”‚   こんにけは  β”‚   やあ  β”‚
17+
β”‚   β”‚   β”‚   β”‚
18+
β”‚ β”‚ β”‚ β”‚
19+
β”‚ β”‚ β”‚ β”‚
20+
β”‚   β”‚   β”‚   β”‚
21+
β”‚   Russian  β”‚  Zdravstvuyte  β”‚   Privet  β”‚
22+
β”‚   β”‚   β”‚   β”‚
23+
β”‚ β”‚ β”‚ β”‚
24+
β”‚ β”‚ β”‚ β”‚
25+
β”‚   β”‚   β”‚   β”‚
26+
β”‚   Spanish  β”‚   Hola  β”‚  ΒΏQuΓ© tal?  β”‚
27+
β”‚   β”‚   β”‚   β”‚
28+
β”‚ β”‚ β”‚ β”‚
29+
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β€Žtable/testdata/TestStyleFunc.golden renamed to β€Žtable/testdata/TestStyleFunc/right-aligned_text_with_margins.golden

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMAL β”‚
33
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
44
β”‚ Chinese β”‚ Nǐn hǎo β”‚ Nǐ hǎo β”‚
5-
β”‚ French β”‚ Bonjour β”‚ Salut β”‚
6-
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
7-
β”‚ Russian β”‚ Zdravstvuyte β”‚ Privet β”‚
8-
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? β”‚
5+
β”‚ French β”‚ Bonjour β”‚ Salut β”‚
6+
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
7+
β”‚ Russian β”‚ Zdravstvuyte β”‚ Privet β”‚
8+
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? β”‚
99
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

0 commit comments

Comments
Β (0)