Merge branch 'v2-exp' into v2-table

This commit is contained in:
Andrey Nering
2025-05-21 14:08:35 -03:00
26 changed files with 805 additions and 61 deletions
+517 -31
View File
@@ -3,39 +3,41 @@ package table
import (
"fmt"
"image/color"
"reflect"
"testing"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/lipgloss/v2/table"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
)
// Reusable inputs
var niceMargins = lipgloss.NewStyle().Padding(0, 1)
var headers = []string{"Rank", "City", "Country", "Population"}
var rows = [][]string{
{"1", "Tokyo", "Japan", "37,274,000"},
{"2", "Delhi", "India", "32,065,760"},
{"3", "Shanghai", "China", "28,516,904"},
{"4", "Dhaka", "Bangladesh", "22,478,116"},
{"5", "São Paulo", "Brazil", "22,429,800"},
{"6", "Mexico City", "Mexico", "22,085,140"},
{"7", "Cairo", "Egypt", "21,750,020"},
{"8", "Beijing", "China", "21,333,332"},
{"9", "Mumbai", "India", "20,961,472"},
{"10", "Osaka", "Japan", "19,059,856"},
{"11", "Chongqing", "China", "16,874,740"},
{"12", "Karachi", "Pakistan", "16,839,950"},
{"13", "Istanbul", "Turkey", "15,636,243"},
{"14", "Kinshasa", "DR Congo", "15,628,085"},
{"15", "Lagos", "Nigeria", "15,387,639"},
{"16", "Buenos Aires", "Argentina", "15,369,919"},
}
var (
niceMargins = lipgloss.NewStyle().Padding(0, 1)
headers = []string{"Rank", "City", "Country", "Population"}
rows = [][]string{
{"1", "Tokyo", "Japan", "37,274,000"},
{"2", "Delhi", "India", "32,065,760"},
{"3", "Shanghai", "China", "28,516,904"},
{"4", "Dhaka", "Bangladesh", "22,478,116"},
{"5", "São Paulo", "Brazil", "22,429,800"},
{"6", "Mexico City", "Mexico", "22,085,140"},
{"7", "Cairo", "Egypt", "21,750,020"},
{"8", "Beijing", "China", "21,333,332"},
{"9", "Mumbai", "India", "20,961,472"},
{"10", "Osaka", "Japan", "19,059,856"},
{"11", "Chongqing", "China", "16,874,740"},
{"12", "Karachi", "Pakistan", "16,839,950"},
{"13", "Istanbul", "Turkey", "15,636,243"},
{"14", "Kinshasa", "DR Congo", "15,628,085"},
{"15", "Lagos", "Nigeria", "15,387,639"},
{"16", "Buenos Aires", "Argentina", "15,369,919"},
}
)
// Tests
func TestNew(t *testing.T) {
func TestNewBash(t *testing.T) {
headers := []string{"Rank", "City", "Country", "Population"}
rows := [][]string{
{"1", "Tokyo", "Japan", "37,274,000"},
@@ -63,6 +65,63 @@ func TestNew(t *testing.T) {
})
}
func TestModel_FromValues(t *testing.T) {
table := New(
WithHeaders("Foo", "Bar"),
WithRows(
[]string{"foo1", "bar1"},
[]string{"foo2", "bar2"},
[]string{"foo3", "bar3"},
))
if len(table.rows) != 3 {
t.Fatalf("expect table to have 3 rows but it has %d", len(table.rows))
}
expect := [][]string{
{"foo1", "bar1"},
{"foo2", "bar2"},
{"foo3", "bar3"},
}
if !reflect.DeepEqual(table.rows, expect) {
t.Fatalf("\n\nwant %v\n\ngot %v", expect, table.rows)
}
}
func TestModel_FromValues_WithTabSeparator(t *testing.T) {
table := New(
WithHeaders("Foo", "Bar"),
WithRows(
[]string{"foo1.", "bar1"},
[]string{"foo,bar,baz", "bar,2"},
),
)
if len(table.rows) != 2 {
t.Fatalf("expect table to have 2 rows but it has %d", len(table.rows))
}
expect := [][]string{
{"foo1.", "bar1"},
{"foo,bar,baz", "bar,2"},
}
if !reflect.DeepEqual(table.rows, expect) {
t.Fatalf("\n\nwant %v\n\ngot %v", expect, table.rows)
}
t.Run("new with options", func(t *testing.T) {
tb := New(
WithHeaders(headers...),
WithRows(rows...),
WithHeight(10),
)
tb.View()
})
t.Run("new, no options", func(t *testing.T) {
tb := New().SetHeaders(headers...).SetRows(rows...)
tb.View()
})
}
func TestTableAlignment(t *testing.T) {
headers := []string{
"Name",
@@ -109,7 +168,6 @@ func TestOverwriteStyles(t *testing.T) {
name string
styles Styles
}{
{"clear styles", Styles{
Selected: lipgloss.NewStyle(),
Header: lipgloss.NewStyle(),
@@ -121,7 +179,6 @@ func TestOverwriteStyles(t *testing.T) {
Cell: niceMargins,
}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tb := New(
@@ -136,6 +193,51 @@ func TestOverwriteStyles(t *testing.T) {
}
}
func TestModel_RenderRow(t *testing.T) {
tests := []struct {
name string
table *Model
expected string
}{
{
name: "simple row",
table: New(
WithRows([]string{"Foooooo", "Baaaaar", "Baaaaaz"}),
WithHeaders("col1", "col2", "col3"),
WithStyles(Styles{Cell: lipgloss.NewStyle()}),
),
expected: "Foooooo Baaaaar Baaaaaz ",
},
{
name: "simple row with truncations",
table: New(
WithRows([]string{"Foooooooooo", "Baaaaaaaaar", "Quuuuuuuuux"}),
WithHeaders("col1", "col2", "col3"),
WithStyles(Styles{Cell: lipgloss.NewStyle()}),
),
expected: "Foooooooo…Baaaaaaaa…Quuuuuuuu…",
},
{
name: "simple row avoiding truncations",
table: New(
WithRows([]string{"Fooooooooo", "Baaaaaaaar", "Quuuuuuuux"}),
WithHeaders("col1", "col2", "col3"),
WithStyles(Styles{Cell: lipgloss.NewStyle()}),
),
expected: "FoooooooooBaaaaaaaarQuuuuuuuux",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actual := tc.table.table.Render()
if actual != tc.expected {
t.Fatalf("\n\nWant: \n%s\n\nGot: \n%s\n", tc.expected, actual)
}
})
}
}
func TestSetStyles(t *testing.T) {
tests := []struct {
name string
@@ -338,9 +440,9 @@ func TestOverwriteStylesFromLipgloss(t *testing.T) {
// Examples
func ExampleOption() {
var niceMargins = lipgloss.NewStyle().Padding(0, 1)
var headers = []string{"Rank", "City", "Country", "Population"}
var rows = [][]string{
niceMargins := lipgloss.NewStyle().Padding(0, 1)
headers := []string{"Rank", "City", "Country", "Population"}
rows := [][]string{
{"1", "Tokyo", "Japan", "37,274,000"},
{"2", "Delhi", "India", "32,065,760"},
{"3", "Shanghai", "China", "28,516,904"},
@@ -377,9 +479,9 @@ func ExampleOption() {
}
func ExampleModel_SetRows() {
var niceMargins = lipgloss.NewStyle().Padding(0, 1)
var headers = []string{"Rank", "City", "Country", "Population"}
var rows = [][]string{
niceMargins := lipgloss.NewStyle().Padding(0, 1)
headers := []string{"Rank", "City", "Country", "Population"}
rows := [][]string{
{"1", "Tokyo", "Japan", "37,274,000"},
{"2", "Delhi", "India", "32,065,760"},
{"3", "Shanghai", "China", "28,516,904"},
@@ -413,3 +515,387 @@ func ExampleModel_SetRows() {
//│ …   …   …   …  │
//└───────────────────────────────────────────┘
}
func TestCursorNavigation(t *testing.T) {
tests := map[string]struct {
rows [][]string
action func(*Model)
want int
}{
"New": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
},
action: func(_ *Model) {},
want: 0,
},
"MoveDown": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.MoveDown(2)
},
want: 2,
},
"MoveUp": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.cursor = 3
t.MoveUp(2)
},
want: 1,
},
"GotoBottom": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.GotoBottom()
},
want: 3,
},
"GotoTop": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.cursor = 3
t.GotoTop()
},
want: 0,
},
"SetCursor": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.SetCursor(2)
},
want: 2,
},
"MoveDown with overflow": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.MoveDown(5)
},
want: 3,
},
"MoveUp with overflow": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.cursor = 3
t.MoveUp(5)
},
want: 0,
},
"Blur does not stop movement": {
rows: [][]string{
{"r1"},
{"r2"},
{"r3"},
{"r4"},
},
action: func(t *Model) {
t.Blur()
t.MoveDown(2)
},
want: 2,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
table := New(
WithHeaders("col1", "col2", "col3"),
WithRows(tc.rows...),
)
tc.action(table)
if table.Cursor() != tc.want {
t.Errorf("want %d, got %d", tc.want, table.Cursor())
}
})
}
}
func TestModel_SetRows(t *testing.T) {
table := New(WithHeaders("col1", "col2", "col3"))
if len(table.rows) != 0 {
t.Fatalf("want 0, got %d", len(table.rows))
}
table.SetRows([]string{"r1", "r2"})
if len(table.rows) != 2 {
t.Fatalf("want 2, got %d", len(table.rows))
}
want := []string{"r1", "r2"}
if !reflect.DeepEqual(table.rows, want) {
t.Fatalf("\n\nwant %v\n\ngot %v", want, table.rows)
}
}
func TestModel_SetHeaders(t *testing.T) {
table := New()
if len(table.headers) != 0 {
t.Fatalf("want 0, got %d", len(table.headers))
}
table.SetHeaders("Foo", "Bar")
if len(table.headers) != 2 {
t.Fatalf("want 2, got %d", len(table.headers))
}
want := []string{"Foo", "Bar"}
if !reflect.DeepEqual(table.headers, want) {
t.Fatalf("\n\nwant %v\n\ngot %v", want, table.headers)
}
}
func TestModel_View(t *testing.T) {
tests := map[string]struct {
modelFunc func() *Model
skip bool
}{
// TODO(?): should the view/output of empty tables use the same default height? (this has height 21)
"Empty": {
modelFunc: func() *Model {
return New()
},
},
"Single row and column": {
modelFunc: func() *Model {
return New(
WithHeaders("Name"),
WithRows(
[]string{"Chocolate Digestives"},
),
)
},
},
"Multiple rows and columns": {
modelFunc: func() *Model {
return New(
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
},
},
// TODO(fix): since the table height is tied to the viewport height, adding vertical padding to the headers' height directly increases the table height.
"Extra padding": {
modelFunc: func() *Model {
s := DefaultStyles()
s.Header = lipgloss.NewStyle().Padding(2, 2)
s.Cell = lipgloss.NewStyle().Padding(2, 2)
return New(
WithHeight(10),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
WithStyles(s),
)
},
},
"No padding": {
modelFunc: func() *Model {
s := DefaultStyles()
s.Header = lipgloss.NewStyle()
s.Cell = lipgloss.NewStyle()
return New(
WithHeight(10),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
WithStyles(s),
)
},
},
// TODO(?): the total height is modified with borderd headers, however not with bordered cells. Is this expected/desired?
"Bordered headers": {
modelFunc: func() *Model {
return New(
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
WithStyles(Styles{
Header: lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()),
}),
)
},
},
// TODO(fix): Headers are not horizontally aligned with cells due to the border adding width to the cells.
"Bordered cells": {
modelFunc: func() *Model {
return New(
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
WithStyles(Styles{
Cell: lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()),
}),
)
},
},
"Manual height greater than rows": {
modelFunc: func() *Model {
return New(
WithHeight(6),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
},
},
"Manual height less than rows": {
modelFunc: func() *Model {
return New(
WithHeight(2),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
},
},
// TODO(fix): spaces are added to the right of the viewport to fill the width, but the headers end as though they are not aware of the width.
"Manual width greater than columns": {
modelFunc: func() *Model {
return New(
WithWidth(80),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
},
},
// TODO(fix): Setting the table width does not affect the total headers' width. Cells are wrapped.
// Headers are not affected. Truncation/resizing should match lipgloss.table functionality.
"Manual width less than columns": {
modelFunc: func() *Model {
return New(
WithWidth(30),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
},
skip: true,
},
"Modified viewport height": {
modelFunc: func() *Model {
m := New(
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
return m
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
if tc.skip {
t.Skip()
}
table := tc.modelFunc()
got := ansi.Strip(table.View())
golden.RequireEqual(t, []byte(got))
})
}
}
// TODO: Fix table to make this test will pass.
func TestModel_View_CenteredInABox(t *testing.T) {
t.Skip()
boxStyle := lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
Align(lipgloss.Center)
table := New(
WithHeight(6),
WithWidth(80),
WithHeaders("Name", "Country of Origin", "Dunk-able"),
WithRows(
[]string{"Chocolate Digestives", "UK", "Yes"},
[]string{"Tim Tams", "Australia", "No"},
[]string{"Hobnobs", "UK", "Yes"},
),
)
tableView := ansi.Strip(table.View())
got := boxStyle.Render(tableView)
golden.RequireEqual(t, []byte(got))
}
+21
View File
@@ -0,0 +1,21 @@
Name Country of Orig…Dunk-able
┌─────────────────────────┐┌────────────────┐┌────────────┐
│Chocolate Digestives ││UK ││Yes │
└─────────────────────────┘└────────────────┘└────────────┘
┌─────────────────────────┐┌────────────────┐┌────────────┐
│Tim Tams ││Australia ││No │
└─────────────────────────┘└────────────────┘└────────────┘
┌─────────────────────────┐┌────────────────┐┌────────────┐
│Hobnobs ││UK ││Yes │
└─────────────────────────┘└────────────────┘└────────────┘
+23
View File
@@ -0,0 +1,23 @@
┌─────────────────────────┐┌────────────────┐┌────────────┐
│Name ││Country of Orig…││Dunk-able │
└─────────────────────────┘└────────────────┘└────────────┘
Chocolate Digestives UK Yes
Tim Tams Australia No
Hobnobs UK Yes
+20
View File
@@ -0,0 +1,20 @@
+14
View File
@@ -0,0 +1,14 @@
  Name     Country of Orig…    Dunk-able   
  Chocolate Digestives     UK     Yes   
  Tim Tams     Australia     No   
@@ -0,0 +1,6 @@
 Name   Country of Orig…  Dunk-able  
 Chocolate Digestives   UK   Yes  
 Tim Tams   Australia   No  
 Hobnobs   UK   Yes  
@@ -0,0 +1,2 @@
 Name   Country of Orig…  Dunk-able  
 Chocolate Digestives   UK   Yes  
@@ -0,0 +1,21 @@
 Name   Country of Orig…  Dunk-able  
 Chocolate Digestives   UK   Yes  
 Tim Tams   Australia   No  
 Hobnobs   UK   Yes  
@@ -0,0 +1,21 @@
Name Country of Origin Dunk-able
Chocolate Digestives UK Yes
Tim Tams Australia No
Hobnobs UK Yes
@@ -0,0 +1,3 @@
 Name   Country of Orig…  Dunk-able  
 Chocolate Digestives   UK   Yes  
 Tim Tams   Australia   No  
@@ -0,0 +1,21 @@
 Name   Country of Orig…  Dunk-able  
 Chocolate Digestives   UK   Yes  
 Tim Tams   Australia   No  
 Hobnobs   UK   Yes  
+10
View File
@@ -0,0 +1,10 @@
Name Country of Orig…Dunk-able
Chocolate Digestives UK Yes
Tim Tams Australia No
Hobnobs UK Yes
@@ -0,0 +1,21 @@
 Name  
 Chocolate Digestives  
+8
View File
@@ -0,0 +1,8 @@
┌────────────────────────────────────────────────────────────────────────────────┐
│ Name Country of Orig… Dunk-able │
│ Chocolate Digestives UK Yes │
│ Tim Tams Australia No │
│ Hobnobs UK Yes │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────┘