Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table widget #111

Merged
merged 7 commits into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
optional seperators and specific color for row
  • Loading branch information
wanzysky committed Nov 23, 2016
commit 7ce2564652a033392b18d1b8565f2b1a3a7f08ac
22 changes: 20 additions & 2 deletions _example/table.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package main

import "github.com/gizak/termui"
import "termui"

func main() {
err := termui.Init()
if err != nil {
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"},
Expand All @@ -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

Expand Down
47 changes: 38 additions & 9 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Table struct {
Rows [][]string
FgColor Attribute
BgColor Attribute
FgColors []Attribute
BgColors []Attribute
Seperator bool
TextAlign string
}

Expand All @@ -30,6 +33,7 @@ func NewTable() *Table {
table.FgColor = ColorWhite
table.BgColor = ColorDefault
table.TextAlign = "left"
table.Seperator = true
return table
}

Expand All @@ -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
Expand All @@ -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] {
Expand All @@ -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
Expand Down