Merge pull request #222 from ryanlewis/master
Table render improvements
This commit is contained in:
commit
9bbf7adda8
@ -43,6 +43,24 @@ func main() {
|
|||||||
|
|
||||||
ui.Render(table2)
|
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.FillRow = true
|
||||||
|
table3.RowStyles[0] = ui.NewStyle(ui.ColorWhite, ui.ColorBlack, ui.ModifierBold)
|
||||||
|
table3.RowStyles[2] = ui.NewStyle(ui.ColorWhite, ui.ColorRed, ui.ModifierBold)
|
||||||
|
table3.RowStyles[3] = ui.NewStyle(ui.ColorYellow)
|
||||||
|
|
||||||
|
ui.Render(table3)
|
||||||
|
|
||||||
uiEvents := ui.PollEvents()
|
uiEvents := ui.PollEvents()
|
||||||
for {
|
for {
|
||||||
e := <-uiEvents
|
e := <-uiEvents
|
||||||
|
@ -26,6 +26,8 @@ type Table struct {
|
|||||||
TextStyle Style
|
TextStyle Style
|
||||||
RowSeparator bool
|
RowSeparator bool
|
||||||
TextAlign Alignment
|
TextAlign Alignment
|
||||||
|
RowStyles map[int]Style
|
||||||
|
FillRow bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTable() *Table {
|
func NewTable() *Table {
|
||||||
@ -33,6 +35,7 @@ func NewTable() *Table {
|
|||||||
Block: *NewBlock(),
|
Block: *NewBlock(),
|
||||||
TextStyle: Theme.Table.Text,
|
TextStyle: Theme.Table.Text,
|
||||||
RowSeparator: true,
|
RowSeparator: true,
|
||||||
|
RowStyles: make(map[int]Style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,9 +57,21 @@ func (self *Table) Draw(buf *Buffer) {
|
|||||||
for i := 0; i < len(self.Rows) && yCoordinate < self.Inner.Max.Y; i++ {
|
for i := 0; i < len(self.Rows) && yCoordinate < self.Inner.Max.Y; i++ {
|
||||||
row := self.Rows[i]
|
row := self.Rows[i]
|
||||||
colXCoordinate := self.Inner.Min.X
|
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
|
// draw row cells
|
||||||
for j := 0; j < len(row); j++ {
|
for j := 0; j < len(row); j++ {
|
||||||
col := ParseText(row[j], self.TextStyle)
|
col := ParseText(row[j], rowStyle)
|
||||||
// draw row cell
|
// draw row cell
|
||||||
if len(col) > columnWidths[j] || self.TextAlign == AlignLeft {
|
if len(col) > columnWidths[j] || self.TextAlign == AlignLeft {
|
||||||
for k, cell := range col {
|
for k, cell := range col {
|
||||||
@ -84,9 +99,17 @@ func (self *Table) Draw(buf *Buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// draw vertical separators
|
// draw vertical separators
|
||||||
|
separatorStyle := self.Block.BorderStyle
|
||||||
|
|
||||||
separatorXCoordinate := self.Inner.Min.X
|
separatorXCoordinate := self.Inner.Min.X
|
||||||
verticalCell := NewCell(VERTICAL_LINE, NewStyle(ColorWhite))
|
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
|
separatorXCoordinate += width
|
||||||
buf.SetCell(verticalCell, image.Pt(separatorXCoordinate, yCoordinate))
|
buf.SetCell(verticalCell, image.Pt(separatorXCoordinate, yCoordinate))
|
||||||
separatorXCoordinate++
|
separatorXCoordinate++
|
||||||
@ -95,7 +118,7 @@ func (self *Table) Draw(buf *Buffer) {
|
|||||||
yCoordinate++
|
yCoordinate++
|
||||||
|
|
||||||
// draw horizontal separator
|
// draw horizontal separator
|
||||||
horizontalCell := NewCell(HORIZONTAL_LINE, NewStyle(ColorWhite))
|
horizontalCell := NewCell(HORIZONTAL_LINE, separatorStyle)
|
||||||
if self.RowSeparator && yCoordinate < self.Inner.Max.Y && i != len(self.Rows)-1 {
|
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))
|
buf.Fill(horizontalCell, image.Rect(self.Inner.Min.X, yCoordinate, self.Inner.Max.X, yCoordinate+1))
|
||||||
yCoordinate++
|
yCoordinate++
|
||||||
|
Loading…
Reference in New Issue
Block a user