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
feature: support table (#1)
  • Loading branch information
wanzysky authored Nov 10, 2016
commit 25c85a59b08d4e978f2e60c5817ea8dd59223968
34 changes: 34 additions & 0 deletions _example/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import "termui"

func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()

rows := [][]string{
[]string{"header1", "header2", "header3"},
[]string{"I love Veronica", "Go-lang is so cool", "Im working on Ruby"},
[]string{"2016", "11", "11"},
}

table := termui.NewTable()
table.Rows = rows
table.FgColor = termui.ColorWhite
table.BgColor = termui.ColorDefault
table.TextAlign = "center"
table.Analysis()
table.SetSize()
table.Y = 0
table.X = 0
table.Border = true

termui.Render(table)
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
})
termui.Loop()
}
Binary file added _example/table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package termui

import (
"fmt"
"strings"
)

/*
table := termui.NewTable()
table.Rows = rows
table.FgColor = termui.ColorWhite
table.BgColor = termui.ColorDefault
table.Height = 7
table.Width = 62
table.Y = 0
table.X = 0
table.Border = true
*/

type Table struct {
Block
Rows [][]string
FgColor Attribute
BgColor Attribute
TextAlign string
}

func NewTable() *Table {
table := &Table{Block: *NewBlock()}
table.FgColor = ColorWhite
table.BgColor = ColorDefault
table.TextAlign = "left"
return table
}

func (table *Table) Analysis() {
length := len(table.Rows)
if length < 1 {
return
}

row_width := len(table.Rows[0])
cellWidthes := make([]int, row_width)

for _, row := range table.Rows {
for i, str := range row {
if cellWidthes[i] < len(str) {
cellWidthes[i] = len(str)
}
}
}

width_sum := 2
for i, width := range cellWidthes {
width_sum += (width + 2)
for u, row := range table.Rows {
switch table.TextAlign {
case "right":
row[i] = fmt.Sprintf(" %*s ", width, table.Rows[u][i])
case "center":
word_width := len(table.Rows[u][i])
offset := (width - word_width) / 2
row[i] = fmt.Sprintf(" %*s ", width, fmt.Sprintf("%-*s", offset+word_width, table.Rows[u][i]))
default: // left
row[i] = fmt.Sprintf(" %-*s ", width, table.Rows[u][i])
}
}
}

if table.Width == 0 {
table.Width = width_sum
}
}

func (table *Table) SetSize() {
length := len(table.Rows)
table.Height = length*2 + 1
table.Width = 2
if length != 0 {
for _, str := range table.Rows[0] {
table.Width += len(str) + 2 + 1
}
}
}

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)
}

for x, cell := range border {
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i*2+1, cell)
}
}
return buffer
}