|
8 | 8 | "github.com/charmbracelet/lipgloss"
|
9 | 9 | "github.com/charmbracelet/x/ansi"
|
10 | 10 | "github.com/charmbracelet/x/exp/golden"
|
| 11 | + "github.com/muesli/termenv" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | var TableStyle = func(row, col int) lipgloss.Style {
|
@@ -1142,30 +1143,58 @@ func TestTableHeightWithOffset(t *testing.T) {
|
1142 | 1143 | }
|
1143 | 1144 |
|
1144 | 1145 | 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 | + }) |
1156 | 1197 | }
|
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())) |
1169 | 1198 | }
|
1170 | 1199 |
|
1171 | 1200 | func TestClearRows(t *testing.T) {
|
|
0 commit comments