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

feat: support custom styles for cells #397

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
20 changes: 16 additions & 4 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,21 @@ func DefaultKeyMap() KeyMap {
}

// Styles contains style definitions for this list component. By default, these
// values are generated by DefaultStyles.
// values are generated by DefaultStyles. RenderCell has higher priority then Cell.
type Styles struct {
Header lipgloss.Style
Cell lipgloss.Style
Selected lipgloss.Style

RenderCell func(value string, rowID int, columnID int) string
}

func (s Styles) renderCell(value string, rowID int, columnID int) string {
if s.RenderCell != nil {
return s.RenderCell(value, rowID, columnID)
}

return s.Cell.Render(value)
}

// DefaultStyles returns a set of default style definitions for this table.
Expand Down Expand Up @@ -380,7 +390,7 @@ func (m *Model) FromValues(value, separator string) {
}

func (m Model) headersView() string {
var s = make([]string, 0, len(m.cols))
s := make([]string, 0, len(m.cols))
for _, col := range m.cols {
style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
Expand All @@ -390,10 +400,12 @@ func (m Model) headersView() string {
}

func (m *Model) renderRow(rowID int) string {
var s = make([]string, 0, len(m.cols))
s := make([]string, 0, len(m.cols))
for i, value := range m.rows[rowID] {
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
renderedCell := m.styles.Cell.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))

renderedCell := style.Render(runewidth.Truncate(value, m.cols[i].Width, "…"))
renderedCell = m.styles.renderCell(renderedCell, rowID, i)
s = append(s, renderedCell)
}

Expand Down
49 changes: 48 additions & 1 deletion table/table_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package table

import "testing"
import (
"strings"
"testing"
)

func TestFromValues(t *testing.T) {
input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3"
Expand Down Expand Up @@ -52,3 +55,47 @@ func deepEqual(a, b []Row) bool {
}
return true
}

func TestRenderCell(t *testing.T) {
const expected = "rendered"

styles := DefaultStyles()

styles.RenderCell = func(value string, rowID, columnID int) string {
switch {
case rowID != 0:
t.Fatalf("Invalid rowID: %d", rowID)
case columnID != 0:
t.Fatalf("Invalid columnID: %d", columnID)
}

return expected
}

table := New(
WithColumns([]Column{{Title: "Foo", Width: 100}}),
WithRows([]Row{{"unexpected"}}),
WithStyles(styles),
)

rendered := table.View()

if !strings.Contains(rendered, expected) {
t.Fatalf("Expected: %q in \n%s", expected, rendered)
}
}

func TestCellDefault(t *testing.T) {
const expected = "rendered"

table := New(
WithColumns([]Column{{Title: "Foo", Width: 100}}),
WithRows([]Row{{expected}}),
)

rendered := table.View()

if !strings.Contains(rendered, expected) {
t.Fatalf("Expected: %q in \n%s", expected, rendered)
}
}