optional seperators and specific color for row
This commit is contained in:
parent
c178559c26
commit
7ce2564652
@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import "github.com/gizak/termui"
|
||||
import "termui"
|
||||
|
||||
func main() {
|
||||
err := termui.Init()
|
||||
@ -8,6 +8,22 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
defer termui.Close()
|
||||
rows_1 := [][]string{
|
||||
[]string{"header1", "header2", "header3"},
|
||||
[]string{"I love Veronica", "Go-lang is so cool", "Im working on Ruby"},
|
||||
[]string{"2016", "11", "11"},
|
||||
}
|
||||
|
||||
table_1 := termui.NewTable()
|
||||
table_1.Rows = rows_1
|
||||
table_1.FgColor = termui.ColorWhite
|
||||
table_1.BgColor = termui.ColorDefault
|
||||
table_1.Y = 0
|
||||
table_1.X = 0
|
||||
table_1.Width = 62
|
||||
table_1.Height = 7
|
||||
|
||||
termui.Render(table_1)
|
||||
|
||||
rows := [][]string{
|
||||
[]string{"header1", "header2", "header3"},
|
||||
@ -20,9 +36,11 @@ func main() {
|
||||
table.FgColor = termui.ColorWhite
|
||||
table.BgColor = termui.ColorDefault
|
||||
table.TextAlign = "center"
|
||||
table.Seperator = false
|
||||
table.Analysis()
|
||||
table.SetSize()
|
||||
table.Y = 0
|
||||
table.BgColors[2] = termui.ColorRed
|
||||
table.Y = 20
|
||||
table.X = 0
|
||||
table.Border = true
|
||||
|
||||
|
33
table.go
33
table.go
@ -22,6 +22,9 @@ type Table struct {
|
||||
Rows [][]string
|
||||
FgColor Attribute
|
||||
BgColor Attribute
|
||||
FgColors []Attribute
|
||||
BgColors []Attribute
|
||||
Seperator bool
|
||||
TextAlign string
|
||||
}
|
||||
|
||||
@ -30,6 +33,7 @@ func NewTable() *Table {
|
||||
table.FgColor = ColorWhite
|
||||
table.BgColor = ColorDefault
|
||||
table.TextAlign = "left"
|
||||
table.Seperator = true
|
||||
return table
|
||||
}
|
||||
|
||||
@ -39,15 +43,30 @@ func (table *Table) Analysis() {
|
||||
return
|
||||
}
|
||||
|
||||
if len(table.FgColors) == 0 {
|
||||
table.FgColors = make([]Attribute, len(table.Rows))
|
||||
}
|
||||
if len(table.BgColors) == 0 {
|
||||
table.BgColors = make([]Attribute, len(table.Rows))
|
||||
}
|
||||
|
||||
row_width := len(table.Rows[0])
|
||||
cellWidthes := make([]int, row_width)
|
||||
|
||||
for _, row := range table.Rows {
|
||||
for index, row := range table.Rows {
|
||||
for i, str := range row {
|
||||
if cellWidthes[i] < len(str) {
|
||||
cellWidthes[i] = len(str)
|
||||
}
|
||||
}
|
||||
|
||||
if table.FgColors[index] == 0 {
|
||||
table.FgColors[index] = table.FgColor
|
||||
}
|
||||
|
||||
if table.BgColors[index] == 0 {
|
||||
table.BgColors[index] = table.BgColor
|
||||
}
|
||||
}
|
||||
|
||||
width_sum := 2
|
||||
@ -74,7 +93,11 @@ func (table *Table) Analysis() {
|
||||
|
||||
func (table *Table) SetSize() {
|
||||
length := len(table.Rows)
|
||||
if table.Seperator {
|
||||
table.Height = length*2 + 1
|
||||
} else {
|
||||
table.Height = length + 2
|
||||
}
|
||||
table.Width = 2
|
||||
if length != 0 {
|
||||
for _, str := range table.Rows[0] {
|
||||
@ -87,7 +110,8 @@ func (table *Table) Buffer() Buffer {
|
||||
buffer := table.Block.Buffer()
|
||||
table.Analysis()
|
||||
for i, row := range table.Rows {
|
||||
cells := DefaultTxBuilder.Build(strings.Join(row, "|"), table.FgColor, table.BgColor)
|
||||
cells := DefaultTxBuilder.Build(strings.Join(row, "|"), table.FgColors[i], table.BgColors[i])
|
||||
if table.Seperator {
|
||||
border := DefaultTxBuilder.Build(strings.Repeat("─", table.Width-2), table.FgColor, table.BgColor)
|
||||
for x, cell := range cells {
|
||||
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i*2, cell)
|
||||
@ -96,6 +120,11 @@ func (table *Table) Buffer() Buffer {
|
||||
for x, cell := range border {
|
||||
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i*2+1, cell)
|
||||
}
|
||||
} else {
|
||||
for x, cell := range cells {
|
||||
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i, cell)
|
||||
}
|
||||
}
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user