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(table): More granular table styles #246 #340

Closed
wants to merge 1 commit into from
Closed
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
56 changes: 50 additions & 6 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Copy link
Member

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 for Row, this would make this chunk a lot simpler

// 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()
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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, "…")))
Copy link
Member

Choose a reason for hiding this comment

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

Expand Down