Skip to content

Commit

Permalink
fix: table expand math error
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 3, 2023
1 parent 33a15e3 commit d28947e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
1 change: 1 addition & 0 deletions examples/table/pokemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
Border(lipgloss.NormalBorder()).
BorderStyle(re.NewStyle().Foreground(lipgloss.Color("238"))).
Headers(CapitalizeHeaders(headers)...).
Width(90).
Rows(data...).
StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
Expand Down
16 changes: 11 additions & 5 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
// })
type StyleFunc func(row, col int) lipgloss.Style

// NoTableStyle is a TableStyleFunc that returns a new Style with no attributes.
func NoTableStyle(_, _ int) lipgloss.Style {
// DefaultStyles is a TableStyleFunc that returns a new Style with no attributes.
func DefaultStyles(_, _ int) lipgloss.Style {
return lipgloss.NewStyle()
}

Expand Down Expand Up @@ -70,8 +70,8 @@ type Table struct {
// By default, a table has no border, no styling, and no rows.
func New() *Table {
return &Table{
styleFunc: NoTableStyle,
border: lipgloss.HiddenBorder(),
styleFunc: DefaultStyles,
border: lipgloss.RoundedBorder(),
borderBottom: true,
borderColumn: true,
borderHeader: true,
Expand Down Expand Up @@ -282,6 +282,9 @@ func (t *Table) String() string {
for i := range t.widths {
t.widths[i]++
tableWidth++
if tableWidth >= t.width {
break
}
}
}
} else if tableWidth > t.width && t.width > 0 {
Expand Down Expand Up @@ -472,7 +475,10 @@ func (t *Table) String() string {
}
}

return s.String()
height := sum(t.heights) - 1 + boolToInt(hasHeaders) +
boolToInt(t.borderHeader) + boolToInt(t.borderTop) + boolToInt(t.borderBottom) +
len(t.rows)*boolToInt(t.borderRow)
return lipgloss.NewStyle().MaxHeight(height).MaxWidth(t.width).Render(s.String())
}

// Render returns the table as a string.
Expand Down
60 changes: 32 additions & 28 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,10 @@ func TestTableUnsetBorders(t *testing.T) {
French β”‚ Bonjour β”‚ Salut
Japanese β”‚ こんにけは β”‚ やあ
Russian β”‚ Zdravstvuyte β”‚ Privet
Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal?
`, "\n")
Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? `, "\n")

if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", debug(expected), debug(table.String()))
}
}

Expand All @@ -343,11 +342,10 @@ func TestTableUnsetHeaderSeparator(t *testing.T) {
French β”‚ Bonjour β”‚ Salut
Japanese β”‚ こんにけは β”‚ やあ
Russian β”‚ Zdravstvuyte β”‚ Privet
Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal?
`, "\n")
Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? `, "\n")

if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", debug(expected), debug(table.String()))
}
}

Expand Down Expand Up @@ -540,17 +538,21 @@ func TestTableWidthExpand(t *testing.T) {
Rows(rows...)

expected := strings.TrimSpace(`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMAL β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Chinese β”‚ Nǐn hǎo β”‚ Nǐ hǎo β”‚
β”‚ French β”‚ Bonjour β”‚ Salut β”‚
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
β”‚ Russian β”‚ Zdravstvuyte β”‚ Privet β”‚
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMAL β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Chinese β”‚ Nǐn hǎo β”‚ Nǐ hǎo β”‚
β”‚ French β”‚ Bonjour β”‚ Salut β”‚
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
β”‚ Russian β”‚ Zdravstvuyte β”‚ Privet β”‚
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© tal? β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
`)

if lipgloss.Width(table.String()) != 80 {
t.Fatalf("expected table width to be 80, got %d", lipgloss.Width(table.String()))
}

if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
}
Expand Down Expand Up @@ -653,7 +655,7 @@ func TestTableWidthSmartCropExtensive(t *testing.T) {
}
}

func TestTableWidthSmartCropFail(t *testing.T) {
func TestTableWidthSmartCropTiny(t *testing.T) {
rows := [][]any{
{"Chinese", "您ε₯½", "δ½ ε₯½"},
{"Japanese", "こんにけは", "やあ"},
Expand All @@ -663,24 +665,22 @@ func TestTableWidthSmartCropFail(t *testing.T) {
}

table := New().
Width(1). // Width is unreasonably small, fail to crop.
Width(1).
StyleFunc(TableStyle).
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)

// Width is too small, ignoring widths...
// TODO: Perhaps truncate the entire table to the table width?
expected := strings.TrimSpace(`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ LANGUAGE β”‚ FORMAL β”‚ INFORMA β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Chinese β”‚ 您ε₯½ β”‚ δ½ ε₯½ β”‚
β”‚ Japanese β”‚ こんにけは β”‚ やあ β”‚
β”‚ Russian β”‚ Здравствуйт β”‚ ΠŸΡ€ΠΈΠ²Π΅Ρ‚ β”‚
β”‚ Spanish β”‚ Hola β”‚ ΒΏQuΓ© β”‚
β”‚ English β”‚ You look β”‚ How's β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”Œ
β”‚
β”œ
β”‚
β”‚
β”‚
β”‚
β”‚
β””
`)

if table.String() != expected {
Expand Down Expand Up @@ -759,3 +759,7 @@ func TestTableWidthShrinkNoBorders(t *testing.T) {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
}
}

func debug(s string) string {
return strings.ReplaceAll(s, " ", ".")
}

0 comments on commit d28947e

Please sign in to comment.