-
Notifications
You must be signed in to change notification settings - Fork 277
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(table): More granular table styles #246 #340
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,23 @@ type Model struct { | |
cursor int | ||
focus bool | ||
styles Styles | ||
CustomStyles | ||
|
||
viewport viewport.Model | ||
start int | ||
end int | ||
} | ||
|
||
type CustomStyles struct { | ||
CustomCellStyle CustomStyle | ||
CustomRowStyle CustomStyle | ||
CustomSelectedStyle CustomStyle | ||
} | ||
|
||
type CustomStyle interface { | ||
Style(int, Row) lipgloss.Style | ||
} | ||
|
||
// Row represents one line in the table. | ||
type Row []string | ||
|
||
|
@@ -388,19 +399,52 @@ func (m Model) headersView() string { | |
} | ||
|
||
func (m *Model) renderRow(rowID int) string { | ||
var s = make([]string, 0, len(m.cols)) | ||
var ( | ||
selStyle lipgloss.Style | ||
rowStyle lipgloss.Style | ||
cellStyles = make([]lipgloss.Style, len(m.cols)) | ||
s = make([]string, 0, len(m.cols)) | ||
) | ||
|
||
// Prepare RowStyle | ||
if m.CustomRowStyle != nil { | ||
rowStyle = m.CustomRowStyle.Style(rowID, m.rows[rowID]) | ||
} else { | ||
// We don't have rowstyle separately, what now? | ||
// do an empty one? or m.Styles.Cell ??? | ||
rowStyle = lipgloss.NewStyle() | ||
} | ||
// If row is selected, prepare selStyle | ||
if rowID == m.cursor { | ||
if m.CustomSelectedStyle != nil { | ||
selStyle = m.CustomSelectedStyle.Style(rowID, m.rows[rowID]) | ||
} else { | ||
selStyle = m.styles.Selected | ||
} | ||
} else { | ||
selStyle = lipgloss.NewStyle() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this do? |
||
} | ||
// For each cell in a row get its style and save it | ||
for i := range m.rows[rowID] { | ||
if m.CustomCellStyle != nil { | ||
cellStyles[i] = m.CustomCellStyle.Style(i, m.rows[rowID]) | ||
} else { | ||
cellStyles[i] = m.styles.Cell | ||
} | ||
} | ||
// Loop through the cells again Render() with overlayed styles | ||
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, "…"))) | ||
overlayedStyle := cellStyles[i].Inherit(rowStyle) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this overlayedStyle do? |
||
overlayedStyle = overlayedStyle.Copy().Inherit(selStyle).Padding(0, 1) | ||
//renderedCell := cellStyles[i].Inherit(rowStyle).Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete commented code |
||
renderedCell := overlayedStyle.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…"))) | ||
|
||
s = append(s, renderedCell) | ||
} | ||
|
||
row := lipgloss.JoinHorizontal(lipgloss.Left, s...) | ||
|
||
if rowID == m.cursor { | ||
return m.styles.Selected.Render(row) | ||
} | ||
|
||
return row | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is worth having a field in
Styles
forRow
, this would make this chunk a lot simpler