Skip to content

Commit 9df12cf

Browse files
authored
Send highlighted status to row style func (#177)
1 parent ef5b263 commit 9df12cf

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

table/options.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ type RowStyleFuncInput struct {
1717

1818
// Row is the full row data.
1919
Row Row
20+
21+
// IsHighlighted is true if the row is currently highlighted.
22+
IsHighlighted bool
2023
}
2124

2225
// WithRowStyleFunc sets a function that can be used to apply a style to each row
2326
// based on the row data. This is useful for things like zebra striping or other
2427
// data-based styles. It can be safely set to nil to remove it later.
2528
// This style is applied after the base style and before individual row styles.
29+
// This will override any HighlightStyle settings.
2630
func (m Model) WithRowStyleFunc(f func(RowStyleFuncInput) lipgloss.Style) Model {
2731
m.rowStyleFunc = f
2832

@@ -136,7 +140,8 @@ func (m Model) SelectedRows() []Row {
136140
}
137141

138142
// HighlightStyle sets a custom style to use when the row is being highlighted
139-
// by the cursor.
143+
// by the cursor. This should not be used with WithRowStyleFunc. Instead, use
144+
// the IsHighlighted field in the style function.
140145
func (m Model) HighlightStyle(style lipgloss.Style) Model {
141146
m.highlightStyle = style
142147

table/row.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,13 @@ func (m Model) renderRow(rowIndex int, last bool) string {
117117

118118
if m.rowStyleFunc != nil {
119119
styleResult := m.rowStyleFunc(RowStyleFuncInput{
120-
Index: rowIndex,
121-
Row: row,
120+
Index: rowIndex,
121+
Row: row,
122+
IsHighlighted: m.focused && highlighted,
122123
})
123124

124125
rowStyle = rowStyle.Inherit(styleResult)
125-
}
126-
127-
if m.focused && highlighted {
126+
} else if m.focused && highlighted {
128127
rowStyle = rowStyle.Inherit(m.highlightStyle)
129128
}
130129

table/view_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,59 @@ func TestStyleFuncAppliesAfterBaseStyleAndColStylesAndBeforeRowStyle(t *testing.
849849
assert.Equal(t, expectedTable, rendered)
850850
}
851851

852+
func TestStyleFuncAppliesHighlighted(t *testing.T) {
853+
styleFunc := func(input RowStyleFuncInput) lipgloss.Style {
854+
if input.IsHighlighted {
855+
return lipgloss.NewStyle().Align(lipgloss.Center)
856+
}
857+
858+
if input.Index%2 == 0 {
859+
return lipgloss.NewStyle().Align(lipgloss.Right)
860+
}
861+
862+
return lipgloss.NewStyle().Align(lipgloss.Left)
863+
}
864+
865+
model := New([]Column{
866+
NewColumn("1", "1", 6),
867+
NewColumn("2", "2", 6),
868+
NewColumn("3", "3", 6),
869+
}).
870+
WithRowStyleFunc(styleFunc).
871+
Focused(true)
872+
873+
rows := []Row{}
874+
875+
for rowIndex := 1; rowIndex <= 5; rowIndex++ {
876+
rowData := RowData{}
877+
878+
for columnIndex := 1; columnIndex <= 3; columnIndex++ {
879+
id := fmt.Sprintf("%d", columnIndex)
880+
881+
rowData[id] = fmt.Sprintf("%d,%d", columnIndex, rowIndex)
882+
}
883+
884+
rows = append(rows, NewRow(rowData))
885+
}
886+
887+
model = model.WithRows(rows).
888+
WithHighlightedRow(2)
889+
890+
const expectedTable = `┏━━━━━━┳━━━━━━┳━━━━━━┓
891+
┃ 1┃ 2┃ 3┃
892+
┣━━━━━━╋━━━━━━╋━━━━━━┫
893+
┃ 1,1┃ 2,1┃ 3,1┃
894+
┃1,2 ┃2,2 ┃3,2 ┃
895+
┃ 1,3 ┃ 2,3 ┃ 3,3 ┃
896+
┃1,4 ┃2,4 ┃3,4 ┃
897+
┃ 1,5┃ 2,5┃ 3,5┃
898+
┗━━━━━━┻━━━━━━┻━━━━━━┛`
899+
900+
rendered := model.View()
901+
902+
assert.Equal(t, expectedTable, rendered)
903+
}
904+
852905
// This is a long test due to typing and multiple big table strings, that's okay
853906
//
854907
//nolint:funlen

0 commit comments

Comments
 (0)