2017-01-14 14:07:43 +08:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2016-11-10 18:55:42 +08:00
|
|
|
package termui
|
|
|
|
|
2016-11-25 16:35:00 +08:00
|
|
|
import "strings"
|
2016-11-10 18:55:42 +08:00
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
/* Table is like:
|
|
|
|
|
|
|
|
┌Awesome Table ────────────────────────────────────────────────┐
|
|
|
|
│ Col0 | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 |
|
|
|
|
│──────────────────────────────────────────────────────────────│
|
|
|
|
│ Some Item #1 | AAA | 123 | CCCCC | EEEEE | GGGGG | IIIII |
|
|
|
|
│──────────────────────────────────────────────────────────────│
|
|
|
|
│ Some Item #2 | BBB | 456 | DDDDD | FFFFF | HHHHH | JJJJJ |
|
|
|
|
└──────────────────────────────────────────────────────────────┘
|
|
|
|
|
|
|
|
Datapoints are a two dimensional array of strings: [][]string
|
|
|
|
|
|
|
|
Example:
|
|
|
|
data := [][]string{
|
|
|
|
{"Col0", "Col1", "Col3", "Col4", "Col5", "Col6"},
|
|
|
|
{"Some Item #1", "AAA", "123", "CCCCC", "EEEEE", "GGGGG", "IIIII"},
|
|
|
|
{"Some Item #2", "BBB", "456", "DDDDD", "FFFFF", "HHHHH", "JJJJJ"},
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:55:42 +08:00
|
|
|
table := termui.NewTable()
|
2016-12-22 10:15:51 +08:00
|
|
|
table.Rows = data // type [][]string
|
2016-11-10 18:55:42 +08:00
|
|
|
table.FgColor = termui.ColorWhite
|
|
|
|
table.BgColor = termui.ColorDefault
|
|
|
|
table.Height = 7
|
|
|
|
table.Width = 62
|
|
|
|
table.Y = 0
|
|
|
|
table.X = 0
|
|
|
|
table.Border = true
|
|
|
|
*/
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
// Table tracks all the attributes of a Table instance
|
2016-11-10 18:55:42 +08:00
|
|
|
type Table struct {
|
|
|
|
Block
|
|
|
|
Rows [][]string
|
2016-11-25 16:35:00 +08:00
|
|
|
CellWidth []int
|
2016-11-10 18:55:42 +08:00
|
|
|
FgColor Attribute
|
|
|
|
BgColor Attribute
|
2016-11-23 15:55:22 +08:00
|
|
|
FgColors []Attribute
|
|
|
|
BgColors []Attribute
|
2016-12-22 10:15:51 +08:00
|
|
|
Separator bool
|
2016-11-29 12:21:02 +08:00
|
|
|
TextAlign Align
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
// NewTable returns a new Table instance
|
2016-11-10 18:55:42 +08:00
|
|
|
func NewTable() *Table {
|
|
|
|
table := &Table{Block: *NewBlock()}
|
|
|
|
table.FgColor = ColorWhite
|
|
|
|
table.BgColor = ColorDefault
|
2016-12-22 10:15:51 +08:00
|
|
|
table.Separator = true
|
2016-11-10 18:55:42 +08:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
// CellsWidth calculates the width of a cell array and returns an int
|
|
|
|
func cellsWidth(cells []Cell) int {
|
|
|
|
width := 0
|
|
|
|
for _, c := range cells {
|
|
|
|
width += c.Width()
|
|
|
|
}
|
|
|
|
return width
|
|
|
|
}
|
|
|
|
|
|
|
|
// Analysis generates and returns an array of []Cell that represent all columns in the Table
|
|
|
|
func (table *Table) Analysis() [][]Cell {
|
|
|
|
var rowCells [][]Cell
|
2016-11-10 18:55:42 +08:00
|
|
|
length := len(table.Rows)
|
|
|
|
if length < 1 {
|
2016-12-22 10:15:51 +08:00
|
|
|
return rowCells
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 15:55:22 +08:00
|
|
|
if len(table.FgColors) == 0 {
|
|
|
|
table.FgColors = make([]Attribute, len(table.Rows))
|
|
|
|
}
|
|
|
|
if len(table.BgColors) == 0 {
|
|
|
|
table.BgColors = make([]Attribute, len(table.Rows))
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
cellWidths := make([]int, len(table.Rows[0]))
|
2016-11-10 18:55:42 +08:00
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
for y, row := range table.Rows {
|
|
|
|
if table.FgColors[y] == 0 {
|
|
|
|
table.FgColors[y] = table.FgColor
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
2016-12-22 10:15:51 +08:00
|
|
|
if table.BgColors[y] == 0 {
|
|
|
|
table.BgColors[y] = table.BgColor
|
2016-11-23 15:55:22 +08:00
|
|
|
}
|
2016-12-22 10:15:51 +08:00
|
|
|
for x, str := range row {
|
|
|
|
cells := DefaultTxBuilder.Build(str, table.FgColors[y], table.BgColors[y])
|
|
|
|
cw := cellsWidth(cells)
|
|
|
|
if cellWidths[x] < cw {
|
|
|
|
cellWidths[x] = cw
|
|
|
|
}
|
|
|
|
rowCells = append(rowCells, cells)
|
2016-11-23 15:55:22 +08:00
|
|
|
}
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
2016-12-22 10:15:51 +08:00
|
|
|
table.CellWidth = cellWidths
|
|
|
|
return rowCells
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
// SetSize calculates the table size and sets the internal value
|
2016-11-10 18:55:42 +08:00
|
|
|
func (table *Table) SetSize() {
|
|
|
|
length := len(table.Rows)
|
2016-12-22 10:15:51 +08:00
|
|
|
if table.Separator {
|
2016-11-23 15:55:22 +08:00
|
|
|
table.Height = length*2 + 1
|
|
|
|
} else {
|
|
|
|
table.Height = length + 2
|
|
|
|
}
|
2016-11-10 18:55:42 +08:00
|
|
|
table.Width = 2
|
|
|
|
if length != 0 {
|
2016-12-22 10:15:51 +08:00
|
|
|
for _, cellWidth := range table.CellWidth {
|
|
|
|
table.Width += cellWidth + 3
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
// CalculatePosition ...
|
|
|
|
func (table *Table) CalculatePosition(x int, y int, coordinateX *int, coordinateY *int, cellStart *int) {
|
|
|
|
if table.Separator {
|
|
|
|
*coordinateY = table.innerArea.Min.Y + y*2
|
2016-11-25 16:35:00 +08:00
|
|
|
} else {
|
2016-12-22 10:15:51 +08:00
|
|
|
*coordinateY = table.innerArea.Min.Y + y
|
2016-11-25 16:35:00 +08:00
|
|
|
}
|
|
|
|
if x == 0 {
|
2016-12-22 10:15:51 +08:00
|
|
|
*cellStart = table.innerArea.Min.X
|
2016-11-25 16:35:00 +08:00
|
|
|
} else {
|
2016-12-22 10:15:51 +08:00
|
|
|
*cellStart += table.CellWidth[x-1] + 3
|
2016-11-25 16:35:00 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 12:21:02 +08:00
|
|
|
switch table.TextAlign {
|
|
|
|
case AlignRight:
|
2016-12-22 10:15:51 +08:00
|
|
|
*coordinateX = *cellStart + (table.CellWidth[x] - len(table.Rows[y][x])) + 2
|
2016-11-29 12:21:02 +08:00
|
|
|
case AlignCenter:
|
2016-12-22 10:15:51 +08:00
|
|
|
*coordinateX = *cellStart + (table.CellWidth[x]-len(table.Rows[y][x]))/2 + 2
|
2016-11-29 12:21:02 +08:00
|
|
|
default:
|
2016-12-22 10:15:51 +08:00
|
|
|
*coordinateX = *cellStart + 2
|
2016-11-25 16:35:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
// Buffer ...
|
2016-11-10 18:55:42 +08:00
|
|
|
func (table *Table) Buffer() Buffer {
|
|
|
|
buffer := table.Block.Buffer()
|
2016-12-22 10:15:51 +08:00
|
|
|
rowCells := table.Analysis()
|
|
|
|
pointerX := table.innerArea.Min.X + 2
|
|
|
|
pointerY := table.innerArea.Min.Y
|
|
|
|
borderPointerX := table.innerArea.Min.X
|
2016-11-25 16:35:00 +08:00
|
|
|
for y, row := range table.Rows {
|
2016-12-22 10:15:51 +08:00
|
|
|
for x := range row {
|
|
|
|
table.CalculatePosition(x, y, &pointerX, &pointerY, &borderPointerX)
|
|
|
|
background := DefaultTxBuilder.Build(strings.Repeat(" ", table.CellWidth[x]+3), table.BgColors[y], table.BgColors[y])
|
|
|
|
cells := rowCells[y*len(row)+x]
|
|
|
|
for i, back := range background {
|
|
|
|
buffer.Set(borderPointerX+i, pointerY, back)
|
2016-11-23 15:55:22 +08:00
|
|
|
}
|
2016-11-10 18:55:42 +08:00
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
coordinateX := pointerX
|
2016-11-25 16:35:00 +08:00
|
|
|
for _, printer := range cells {
|
2016-12-22 10:15:51 +08:00
|
|
|
buffer.Set(coordinateX, pointerY, printer)
|
|
|
|
coordinateX += printer.Width()
|
2016-11-23 15:55:22 +08:00
|
|
|
}
|
2016-11-25 16:35:00 +08:00
|
|
|
|
|
|
|
if x != 0 {
|
2016-12-22 10:15:51 +08:00
|
|
|
dividors := DefaultTxBuilder.Build("|", table.FgColors[y], table.BgColors[y])
|
|
|
|
for _, dividor := range dividors {
|
|
|
|
buffer.Set(borderPointerX, pointerY, dividor)
|
2016-11-25 16:35:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:15:51 +08:00
|
|
|
if table.Separator {
|
2016-11-25 16:35:00 +08:00
|
|
|
border := DefaultTxBuilder.Build(strings.Repeat("─", table.Width-2), table.FgColor, table.BgColor)
|
|
|
|
for i, cell := range border {
|
2016-12-22 10:15:51 +08:00
|
|
|
buffer.Set(i+1, pointerY+1, cell)
|
2016-11-23 15:55:22 +08:00
|
|
|
}
|
2016-11-10 18:55:42 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 16:35:00 +08:00
|
|
|
|
2016-11-10 18:55:42 +08:00
|
|
|
return buffer
|
|
|
|
}
|