From 7ce2564652a033392b18d1b8565f2b1a3a7f08ac Mon Sep 17 00:00:00 2001 From: wanzysky Date: Wed, 23 Nov 2016 15:55:22 +0800 Subject: [PATCH] optional seperators and specific color for row --- _example/table.go | 22 ++++++++++++++++++++-- table.go | 47 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/_example/table.go b/_example/table.go index dbb06b2..2e44229 100644 --- a/_example/table.go +++ b/_example/table.go @@ -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 diff --git a/table.go b/table.go index 267ff95..663305c 100644 --- a/table.go +++ b/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) - table.Height = length*2 + 1 + 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,14 +110,20 @@ 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) - 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) - } + 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) + } - for x, cell := range border { - buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i*2+1, cell) + 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