Skip to content

Commit 7445f97

Browse files
authored
refactor(viewport): softwrap; improve perf; various bug fixes (#823)
1 parent 50038eb commit 7445f97

27 files changed

+604
-220
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/charmbracelet/harmonica v0.2.0
1010
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1
1111
github.com/charmbracelet/x/ansi v0.8.0
12-
github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a
12+
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f
1313
github.com/dustin/go-humanize v1.0.1
1414
github.com/lucasb-eyer/go-colorful v1.2.0
1515
github.com/mattn/go-runewidth v0.0.16
@@ -18,7 +18,7 @@ require (
1818
)
1919

2020
require (
21-
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
21+
github.com/aymanbagabas/go-udiff v0.3.1 // indirect
2222
github.com/charmbracelet/colorprofile v0.3.0 // indirect
2323
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
2424
github.com/charmbracelet/x/input v0.3.4 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z
44
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
55
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
66
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
7+
github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY=
8+
github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E=
79
github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.1 h1:RvpXiXuPAuaKCHPCsE/lK5+zztnNDTSCa0CpeeIKdDU=
810
github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.1/go.mod h1:qbcZLI5z8R49v9xBdU5V5Dh5D2uccx8wSwBqxQyErqc=
911
github.com/charmbracelet/colorprofile v0.3.0 h1:KtLh9uuu1RCt+Hml4s6Hz+kB1PfV3wi++1h5ia65yKQ=
@@ -18,6 +20,8 @@ github.com/charmbracelet/x/cellbuf v0.0.13 h1:/KBBKHuVRbq1lYx5BzEHBAFBP8VcQzJejZ
1820
github.com/charmbracelet/x/cellbuf v0.0.13/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
1921
github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a h1:FsHEJ52OC4VuTzU8t+n5frMjLvpYWEznSr/u8tnkCYw=
2022
github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
23+
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA=
24+
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f/go.mod h1:IfZAMTHB6XkZSeXUqriemErjAWCCzT0LwjKFYCZyw0I=
2125
github.com/charmbracelet/x/input v0.3.4 h1:Mujmnv/4DaitU0p+kIsrlfZl/UlmeLKw1wAP3e1fMN0=
2226
github.com/charmbracelet/x/input v0.3.4/go.mod h1:JI8RcvdZWQIhn09VzeK3hdp4lTz7+yhiEdpEQtZN+2c=
2327
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=

table/table_test.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ func TestModel_RenderRow(t *testing.T) {
291291
func TestTableAlignment(t *testing.T) {
292292
t.Run("No border", func(t *testing.T) {
293293
biscuits := New(
294+
WithWidth(59),
294295
WithHeight(5),
295296
WithColumns([]Column{
296297
{Title: "Name", Width: 25},
@@ -319,6 +320,7 @@ func TestTableAlignment(t *testing.T) {
319320
Bold(false)
320321

321322
biscuits := New(
323+
WithWidth(59),
322324
WithHeight(5),
323325
WithColumns([]Column{
324326
{Title: "Name", Width: 25},
@@ -515,15 +517,19 @@ func TestModel_View(t *testing.T) {
515517
modelFunc func() Model
516518
skip bool
517519
}{
518-
// TODO(?): should the view/output of empty tables use the same default height? (this has height 21)
519520
"Empty": {
520521
modelFunc: func() Model {
521-
return New()
522+
return New(
523+
WithWidth(60),
524+
WithHeight(21),
525+
)
522526
},
523527
},
524528
"Single row and column": {
525529
modelFunc: func() Model {
526530
return New(
531+
WithWidth(27),
532+
WithHeight(21),
527533
WithColumns([]Column{
528534
{Title: "Name", Width: 25},
529535
}),
@@ -536,6 +542,8 @@ func TestModel_View(t *testing.T) {
536542
"Multiple rows and columns": {
537543
modelFunc: func() Model {
538544
return New(
545+
WithWidth(59),
546+
WithHeight(21),
539547
WithColumns([]Column{
540548
{Title: "Name", Width: 25},
541549
{Title: "Country of Origin", Width: 16},
@@ -557,6 +565,7 @@ func TestModel_View(t *testing.T) {
557565
s.Cell = lipgloss.NewStyle().Padding(2, 2)
558566

559567
return New(
568+
WithWidth(60),
560569
WithHeight(10),
561570
WithColumns([]Column{
562571
{Title: "Name", Width: 25},
@@ -579,6 +588,7 @@ func TestModel_View(t *testing.T) {
579588
s.Cell = lipgloss.NewStyle()
580589

581590
return New(
591+
WithWidth(53),
582592
WithHeight(10),
583593
WithColumns([]Column{
584594
{Title: "Name", Width: 25},
@@ -594,10 +604,12 @@ func TestModel_View(t *testing.T) {
594604
)
595605
},
596606
},
597-
// TODO(?): the total height is modified with borderd headers, however not with bordered cells. Is this expected/desired?
607+
// TODO(?): the total height is modified with bordered headers, however not with bordered cells. Is this expected/desired?
598608
"Bordered headers": {
599609
modelFunc: func() Model {
600610
return New(
611+
WithWidth(59),
612+
WithHeight(23),
601613
WithColumns([]Column{
602614
{Title: "Name", Width: 25},
603615
{Title: "Country of Origin", Width: 16},
@@ -618,6 +630,8 @@ func TestModel_View(t *testing.T) {
618630
"Bordered cells": {
619631
modelFunc: func() Model {
620632
return New(
633+
WithWidth(59),
634+
WithHeight(21),
621635
WithColumns([]Column{
622636
{Title: "Name", Width: 25},
623637
{Title: "Country of Origin", Width: 16},
@@ -634,9 +648,10 @@ func TestModel_View(t *testing.T) {
634648
)
635649
},
636650
},
637-
"Manual height greater than rows": {
651+
"Height greater than rows": {
638652
modelFunc: func() Model {
639653
return New(
654+
WithWidth(59),
640655
WithHeight(6),
641656
WithColumns([]Column{
642657
{Title: "Name", Width: 25},
@@ -651,9 +666,10 @@ func TestModel_View(t *testing.T) {
651666
)
652667
},
653668
},
654-
"Manual height less than rows": {
669+
"Height less than rows": {
655670
modelFunc: func() Model {
656671
return New(
672+
WithWidth(59),
657673
WithHeight(2),
658674
WithColumns([]Column{
659675
{Title: "Name", Width: 25},
@@ -669,10 +685,11 @@ func TestModel_View(t *testing.T) {
669685
},
670686
},
671687
// 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.
672-
"Manual width greater than columns": {
688+
"Width greater than columns": {
673689
modelFunc: func() Model {
674690
return New(
675691
WithWidth(80),
692+
WithHeight(21),
676693
WithColumns([]Column{
677694
{Title: "Name", Width: 25},
678695
{Title: "Country of Origin", Width: 16},
@@ -688,10 +705,11 @@ func TestModel_View(t *testing.T) {
688705
},
689706
// TODO(fix): Setting the table width does not affect the total headers' width. Cells are wrapped.
690707
// Headers are not affected. Truncation/resizing should match lipgloss.table functionality.
691-
"Manual width less than columns": {
708+
"Width less than columns": {
692709
modelFunc: func() Model {
693710
return New(
694711
WithWidth(30),
712+
WithHeight(15),
695713
WithColumns([]Column{
696714
{Title: "Name", Width: 25},
697715
{Title: "Country of Origin", Width: 16},
@@ -709,6 +727,8 @@ func TestModel_View(t *testing.T) {
709727
"Modified viewport height": {
710728
modelFunc: func() Model {
711729
m := New(
730+
WithWidth(59),
731+
WithHeight(15),
712732
WithColumns([]Column{
713733
{Title: "Name", Width: 25},
714734
{Title: "Country of Origin", Width: 16},
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
┌─────────────────────────┐┌────────────────┐┌────────────┐
22
│Name ││Country of Orig…││Dunk-able │
33
└─────────────────────────┘└────────────────┘└────────────┘
4-
Chocolate Digestives UK Yes
5-
Tim Tams Australia No
6-
Hobnobs UK Yes
7-
8-
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
4+
Chocolate Digestives UK Yes
5+
Tim Tams Australia No
6+
Hobnobs UK Yes
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11

2-
3-
4-
5-
6-
7-
8-
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+

table/testdata/TestModel_View/Extra_padding.golden

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
  Name     Country of Orig…    Dunk-able   
44

55

6-
7-
8-
  Chocolate Digestives     UK     Yes   
9-
10-
11-
12-
13-
  Tim Tams     Australia     No   
14-
6+
7+
8+
  Chocolate Digestives     UK     Yes
9+
10+
11+
12+
13+
  Tim Tams     Australia     No
14+

table/testdata/TestModel_View/Manual_height_greater_than_rows.golden renamed to table/testdata/TestModel_View/Height_greater_than_rows.golden

File renamed without changes.

table/testdata/TestModel_View/Manual_height_less_than_rows.golden renamed to table/testdata/TestModel_View/Height_less_than_rows.golden

File renamed without changes.

table/testdata/TestModel_View/Manual_width_greater_than_columns.golden renamed to table/testdata/TestModel_View/Width_greater_than_columns.golden

File renamed without changes.

table/testdata/TestModel_View/Manual_width_less_than_columns.golden renamed to table/testdata/TestModel_View/Width_less_than_columns.golden

File renamed without changes.

0 commit comments

Comments
 (0)