From ddd058e89d1465e201dd13d0322d813d4b09806e Mon Sep 17 00:00:00 2001 From: Ryan Lewis Date: Fri, 1 Feb 2019 17:24:33 +0000 Subject: [PATCH] Allow override of styles for individual rows --- _examples/table.go | 17 +++++++++++++++++ widgets/table.go | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/_examples/table.go b/_examples/table.go index 8dbfdde..2cbff69 100644 --- a/_examples/table.go +++ b/_examples/table.go @@ -43,6 +43,23 @@ func main() { ui.Render(table2) + table3 := widgets.NewTable() + table3.Rows = [][]string{ + []string{"header1", "header2", "header3"}, + []string{"AAA", "BBB", "CCC"}, + []string{"DDD", "EEE", "FFF"}, + []string{"GGG", "HHH", "III"}, + } + table3.TextStyle = ui.NewStyle(ui.ColorWhite) + table3.RowSeparator = true + table3.BorderStyle = ui.NewStyle(ui.ColorGreen) + table3.SetRect(0, 30, 70, 20) + table3.RowStyles[0] = ui.NewStyle(ui.ColorWhite, ui.ColorBlack, ui.ModifierBold) + table3.RowStyles[2] = ui.NewStyle(ui.ColorRed) + table3.RowStyles[3] = ui.NewStyle(ui.ColorYellow) + + ui.Render(table3) + uiEvents := ui.PollEvents() for { e := <-uiEvents diff --git a/widgets/table.go b/widgets/table.go index 2ba8cc6..801a74e 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -26,6 +26,7 @@ type Table struct { TextStyle Style RowSeparator bool TextAlign Alignment + RowStyles map[int]Style } func NewTable() *Table { @@ -33,6 +34,7 @@ func NewTable() *Table { Block: *NewBlock(), TextStyle: Theme.Table.Text, RowSeparator: true, + RowStyles: make(map[int]Style), } } @@ -56,7 +58,12 @@ func (self *Table) Draw(buf *Buffer) { colXCoordinate := self.Inner.Min.X // draw row cells for j := 0; j < len(row); j++ { - col := ParseText(row[j], self.TextStyle) + rowStyle := self.TextStyle + // get the row style if one exists + if style, ok := self.RowStyles[i]; ok { + rowStyle = style + } + col := ParseText(row[j], rowStyle) // draw row cell if len(col) > columnWidths[j] || self.TextAlign == AlignLeft { for k, cell := range col {