Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): include margins for cell width #401

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (t *Table) constructRows(availableLines int) string {
func (t *Table) constructRow(index int, isOverflow bool) string {
var s strings.Builder

hasHeaders := t.headers != nil && len(t.headers) > 0
hasHeaders := len(t.headers) > 0
height := t.heights[index+btoi(hasHeaders)]
if isOverflow {
height = 1
Expand All @@ -552,10 +552,12 @@ func (t *Table) constructRow(index int, isOverflow bool) string {
cell = t.data.At(index, c)
}

cells = append(cells, t.style(index, c).
Height(height).
cellStyle := t.style(index, c)
cells = append(cells, cellStyle.
// Account for the margins in the cell sizing.
Height(height-cellStyle.GetVerticalMargins()).
MaxHeight(height).
Width(t.widths[c]).
Width(t.widths[c]-cellStyle.GetHorizontalMargins()).
MaxWidth(t.widths[c]).
Render(ansi.Truncate(cell, cellWidth*height, "…")))

Expand Down
75 changes: 52 additions & 23 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
"github.com/muesli/termenv"
)

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

func TestStyleFunc(t *testing.T) {
TestStyle := func(row, col int) lipgloss.Style {
switch {
// this is the header
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
// this is the first row of data
case row == 0:
return lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Right)
default:
return lipgloss.NewStyle().Padding(0, 1)
}
lipgloss.SetColorProfile(termenv.TrueColor)
tests := []struct {
name string
style StyleFunc
}{
{
"right-aligned text with margins",
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
default:
return lipgloss.NewStyle().Margin(0, 1).Align(lipgloss.Right)
}
},
},
{
"margin and padding set",
// this test case uses background colors to differentiate margins
// and padding.
func(row, col int) lipgloss.Style {
switch {
case row == HeaderRow:
return lipgloss.NewStyle().Align(lipgloss.Center)
default:
return lipgloss.NewStyle().
Padding(1).
Margin(1).
// keeping right-aligned text as it's the most likely to
// be broken when truncated.
Align(lipgloss.Right).
Background(lipgloss.Color("#874bfc"))
}
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(tc.style).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")

golden.RequireEqual(t, []byte(table.String()))
})
}

table := New().
Border(lipgloss.NormalBorder()).
StyleFunc(TestStyle).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")

golden.RequireEqual(t, []byte(table.String()))
}

func TestClearRows(t *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions table/testdata/TestStyleFunc/margin_and_padding_set.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
┌────────────┬────────────────┬─────────────┐
│ LANGUAGE │ FORMAL │ INFORMAL │
├────────────┼────────────────┼─────────────┤
│ │ │ │
│   │   │   │
│   Chinese  │   Nǐn hǎo  │   Nǐ hǎo  │
│   │   │   │
│ │ │ │
│ │ │ │
│   │   │   │
│   French  │   Bonjour  │   Salut  │
│   │   │   │
│ │ │ │
│ │ │ │
│   │   │   │
│  Japanese  │   こんにちは  │   やあ  │
│   │   │   │
│ │ │ │
│ │ │ │
│   │   │   │
│   Russian  │  Zdravstvuyte  │   Privet  │
│   │   │   │
│ │ │ │
│ │ │ │
│   │   │   │
│   Spanish  │   Hola  │  ¿Qué tal?  │
│   │   │   │
│ │ │ │
└────────────┴────────────────┴─────────────┘
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
│ LANGUAGE │ FORMAL │ INFORMAL │
├──────────┼──────────────┼───────────┤
│ Chinese │ Nǐn hǎo │ Nǐ hǎo │
French BonjourSalut
│ Japanese │ こんにちは やあ
│ Russian │ Zdravstvuyte │ Privet
│ Spanish │ Hola │ ¿Qué tal? │
FrenchBonjourSalut
│ Japanese │ こんにちはやあ
Russian │ Zdravstvuyte │ Privet
Spanish Hola │ ¿Qué tal? │
└──────────┴──────────────┴───────────┘
Loading