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 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
51 changes: 47 additions & 4 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ type Model struct {
end int
}

// CellPosition holds row and column indexes.
type CellPosition struct {
RowID int
Column int
IsRowSelected bool
}

// Row represents one line in the table.
type Row []string

Expand Down Expand Up @@ -92,6 +99,32 @@ type Styles struct {
Header lipgloss.Style
Cell lipgloss.Style
Selected lipgloss.Style

// RenderCell is a low-level primitive for stylizing cells.
// It is responsible for rendering the selection style. Styles.Cell is ignored.
//
// Example implementation:
// s.RenderCell = func(model table.Model, value string, position table.CellPosition) string {
// cellStyle := s.Cell.Copy()
//
// switch {
// case position.IsRowSelected:
// return cellStyle.Background(lipgloss.Color("57")).Render(value)
// case position.Column == 1:
// return cellStyle.Foreground(lipgloss.Color("21")).Render(value)
// default:
// return cellStyle.Render(value)
// }
// }
RenderCell func(model Model, value string, position CellPosition) string
}

func (s Styles) renderCell(model Model, value string, position CellPosition) string {
if s.RenderCell != nil {
return s.RenderCell(model, value, position)
}

return s.Cell.Render(value)
}

// DefaultStyles returns a set of default style definitions for this table.
Expand Down Expand Up @@ -380,7 +413,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,16 +423,26 @@ func (m Model) headersView() string {
}

func (m *Model) renderRow(rowID int) string {
var s = make([]string, 0, len(m.cols))
isRowSelected := rowID == m.cursor

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, "…")))

position := CellPosition{
RowID: rowID,
Column: i,
IsRowSelected: isRowSelected,
}

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

row := lipgloss.JoinHorizontal(lipgloss.Left, s...)

if rowID == m.cursor {
if isRowSelected {
return m.styles.Selected.Render(row)
}

Expand Down
51 changes: 50 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,49 @@ func deepEqual(a, b []Row) bool {
}
return true
}

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

styles := DefaultStyles()

styles.RenderCell = func(model Model, value string, position CellPosition) string {
switch {
case position.RowID != 0:
t.Fatalf("Invalid rowID: %d", position.RowID)
case position.Column != 0:
t.Fatalf("Invalid columnID: %d", position.Column)
case !position.IsRowSelected:
t.Fatalf("Invalid IsRowSelected: %t", position.IsRowSelected)
}

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)
}
}