From d1c58b4ba94946bfb9685dbcc544e97faf84a965 Mon Sep 17 00:00:00 2001 From: Ryan Lewis Date: Fri, 1 Feb 2019 17:22:43 +0000 Subject: [PATCH 1/4] Set the separator styles to that of the block --- widgets/table.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/widgets/table.go b/widgets/table.go index c7f3761..2ba8cc6 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -85,7 +85,7 @@ func (self *Table) Draw(buf *Buffer) { // draw vertical separators separatorXCoordinate := self.Inner.Min.X - verticalCell := NewCell(VERTICAL_LINE, NewStyle(ColorWhite)) + verticalCell := NewCell(VERTICAL_LINE, self.Block.BorderStyle) for _, width := range columnWidths { separatorXCoordinate += width buf.SetCell(verticalCell, image.Pt(separatorXCoordinate, yCoordinate)) @@ -95,7 +95,7 @@ func (self *Table) Draw(buf *Buffer) { yCoordinate++ // draw horizontal separator - horizontalCell := NewCell(HORIZONTAL_LINE, NewStyle(ColorWhite)) + horizontalCell := NewCell(HORIZONTAL_LINE, self.Block.BorderStyle) if self.RowSeparator && yCoordinate < self.Inner.Max.Y && i != len(self.Rows)-1 { buf.Fill(horizontalCell, image.Rect(self.Inner.Min.X, yCoordinate, self.Inner.Max.X, yCoordinate+1)) yCoordinate++ From ddd058e89d1465e201dd13d0322d813d4b09806e Mon Sep 17 00:00:00 2001 From: Ryan Lewis Date: Fri, 1 Feb 2019 17:24:33 +0000 Subject: [PATCH 2/4] 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 { From 83319fb8003bf6e764d98630663765117e6c3fff Mon Sep 17 00:00:00 2001 From: Ryan Lewis Date: Fri, 1 Feb 2019 18:12:22 +0000 Subject: [PATCH 3/4] Fill whole rows with row style --- _examples/table.go | 1 + widgets/table.go | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/_examples/table.go b/_examples/table.go index 2cbff69..3fda66b 100644 --- a/_examples/table.go +++ b/_examples/table.go @@ -54,6 +54,7 @@ func main() { table3.RowSeparator = true table3.BorderStyle = ui.NewStyle(ui.ColorGreen) table3.SetRect(0, 30, 70, 20) + table3.FillRow = true 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) diff --git a/widgets/table.go b/widgets/table.go index 801a74e..b6a2f62 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -27,6 +27,7 @@ type Table struct { RowSeparator bool TextAlign Alignment RowStyles map[int]Style + FillRow bool } func NewTable() *Table { @@ -56,13 +57,20 @@ func (self *Table) Draw(buf *Buffer) { for i := 0; i < len(self.Rows) && yCoordinate < self.Inner.Max.Y; i++ { row := self.Rows[i] colXCoordinate := self.Inner.Min.X + + rowStyle := self.TextStyle + // get the row style if one exists + if style, ok := self.RowStyles[i]; ok { + rowStyle = style + } + + if self.FillRow { + blankCell := NewCell(' ', rowStyle) + buf.Fill(blankCell, image.Rect(self.Inner.Min.X, yCoordinate, self.Inner.Max.X, yCoordinate+1)) + } + // draw row cells for j := 0; j < len(row); j++ { - 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 { @@ -91,8 +99,13 @@ func (self *Table) Draw(buf *Buffer) { } // draw vertical separators + separatorStyle := self.Block.BorderStyle + if self.FillRow { + separatorStyle.Bg = rowStyle.Bg + } + separatorXCoordinate := self.Inner.Min.X - verticalCell := NewCell(VERTICAL_LINE, self.Block.BorderStyle) + verticalCell := NewCell(VERTICAL_LINE, separatorStyle) for _, width := range columnWidths { separatorXCoordinate += width buf.SetCell(verticalCell, image.Pt(separatorXCoordinate, yCoordinate)) @@ -102,7 +115,7 @@ func (self *Table) Draw(buf *Buffer) { yCoordinate++ // draw horizontal separator - horizontalCell := NewCell(HORIZONTAL_LINE, self.Block.BorderStyle) + horizontalCell := NewCell(HORIZONTAL_LINE, separatorStyle) if self.RowSeparator && yCoordinate < self.Inner.Max.Y && i != len(self.Rows)-1 { buf.Fill(horizontalCell, image.Rect(self.Inner.Min.X, yCoordinate, self.Inner.Max.X, yCoordinate+1)) yCoordinate++ From 3eaafc770691cae9ae8a53846876cd9c9ca5d164 Mon Sep 17 00:00:00 2001 From: Ryan Lewis Date: Fri, 1 Feb 2019 18:24:46 +0000 Subject: [PATCH 4/4] Tweaked background colour rendering on seperators when using row fill --- _examples/table.go | 2 +- widgets/table.go | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/_examples/table.go b/_examples/table.go index 3fda66b..ee42212 100644 --- a/_examples/table.go +++ b/_examples/table.go @@ -56,7 +56,7 @@ func main() { table3.SetRect(0, 30, 70, 20) table3.FillRow = true table3.RowStyles[0] = ui.NewStyle(ui.ColorWhite, ui.ColorBlack, ui.ModifierBold) - table3.RowStyles[2] = ui.NewStyle(ui.ColorRed) + table3.RowStyles[2] = ui.NewStyle(ui.ColorWhite, ui.ColorRed, ui.ModifierBold) table3.RowStyles[3] = ui.NewStyle(ui.ColorYellow) ui.Render(table3) diff --git a/widgets/table.go b/widgets/table.go index b6a2f62..14f58ef 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -100,13 +100,16 @@ func (self *Table) Draw(buf *Buffer) { // draw vertical separators separatorStyle := self.Block.BorderStyle - if self.FillRow { - separatorStyle.Bg = rowStyle.Bg - } separatorXCoordinate := self.Inner.Min.X verticalCell := NewCell(VERTICAL_LINE, separatorStyle) - for _, width := range columnWidths { + for i, width := range columnWidths { + if self.FillRow && i < len(columnWidths)-1 { + verticalCell.Style.Bg = rowStyle.Bg + } else { + verticalCell.Style.Bg = self.Block.BorderStyle.Bg + } + separatorXCoordinate += width buf.SetCell(verticalCell, image.Pt(separatorXCoordinate, yCoordinate)) separatorXCoordinate++