uft8 characters support

This commit is contained in:
wanzysky 2016-11-25 16:35:00 +08:00
parent 7ce2564652
commit 0d2f54137b
2 changed files with 79 additions and 39 deletions

View File

@ -10,8 +10,8 @@ func main() {
defer termui.Close() defer termui.Close()
rows_1 := [][]string{ rows_1 := [][]string{
[]string{"header1", "header2", "header3"}, []string{"header1", "header2", "header3"},
[]string{"I love Veronica", "Go-lang is so cool", "Im working on Ruby"}, []string{"孙嘉你好吗", "Go-lang is so cool", "Im working on Ruby"},
[]string{"2016", "11", "11"}, []string{"2016", "10", "11"},
} }
table_1 := termui.NewTable() table_1 := termui.NewTable()
@ -27,7 +27,7 @@ func main() {
rows := [][]string{ rows := [][]string{
[]string{"header1", "header2", "header3"}, []string{"header1", "header2", "header3"},
[]string{"I love Veronica", "Go-lang is so cool", "Im working on Ruby"}, []string{"Foundations", "Go-lang is so cool", "Im working on Ruby"},
[]string{"2016", "11", "11"}, []string{"2016", "11", "11"},
} }

112
table.go
View File

@ -1,9 +1,6 @@
package termui package termui
import ( import "strings"
"fmt"
"strings"
)
/* /*
table := termui.NewTable() table := termui.NewTable()
@ -20,6 +17,7 @@ import (
type Table struct { type Table struct {
Block Block
Rows [][]string Rows [][]string
CellWidth []int
FgColor Attribute FgColor Attribute
BgColor Attribute BgColor Attribute
FgColors []Attribute FgColors []Attribute
@ -69,26 +67,28 @@ func (table *Table) Analysis() {
} }
} }
width_sum := 2 table.CellWidth = cellWidthes
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 { //width_sum := 2
table.Width = width_sum //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() { func (table *Table) SetSize() {
@ -100,31 +100,71 @@ func (table *Table) SetSize() {
} }
table.Width = 2 table.Width = 2
if length != 0 { if length != 0 {
for _, str := range table.Rows[0] { for _, cell_width := range table.CellWidth {
table.Width += len(str) + 2 + 1 table.Width += cell_width + 3
} }
} }
} }
func (table *Table) CalculatePosition(x int, y int, x_coordinate *int, y_coordibate *int, cell_beginning *int) {
if table.Seperator {
*y_coordibate = table.innerArea.Min.Y + y*2
} else {
*y_coordibate = table.innerArea.Min.Y + y
}
if x == 0 {
*cell_beginning = table.innerArea.Min.X
} else {
*cell_beginning += table.CellWidth[x-1] + 3
}
if table.TextAlign == "right" {
*x_coordinate = *cell_beginning + (table.CellWidth[x] - len(table.Rows[y][x])) + 2
} else if table.TextAlign == "center" {
*x_coordinate = *cell_beginning + (table.CellWidth[x]-len(table.Rows[y][x]))/2 + 2
} else {
*x_coordinate = *cell_beginning + 2
}
}
func (table *Table) Buffer() Buffer { func (table *Table) Buffer() Buffer {
buffer := table.Block.Buffer() buffer := table.Block.Buffer()
table.Analysis() table.Analysis()
for i, row := range table.Rows {
cells := DefaultTxBuilder.Build(strings.Join(row, "|"), table.FgColors[i], table.BgColors[i]) pointer_x := table.innerArea.Min.X + 2
if table.Seperator { pointer_y := table.innerArea.Min.Y
border := DefaultTxBuilder.Build(strings.Repeat("─", table.Width-2), table.FgColor, table.BgColor) border_pointer_x := table.innerArea.Min.X
for x, cell := range cells { for y, row := range table.Rows {
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i*2, cell) for x, cell := range row {
table.CalculatePosition(x, y, &pointer_x, &pointer_y, &border_pointer_x)
backgraound := DefaultTxBuilder.Build(strings.Repeat(" ", table.CellWidth[x]+2), table.BgColors[y], table.BgColors[y])
cells := DefaultTxBuilder.Build(cell, table.FgColors[y], table.BgColors[y])
for i, back := range backgraound {
buffer.Set(border_pointer_x+i, pointer_y, back)
} }
for x, cell := range border { coordinate_x := pointer_x
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i*2+1, cell) for _, printer := range cells {
buffer.Set(coordinate_x, pointer_y, printer)
coordinate_x += printer.Width()
} }
} else {
for x, cell := range cells { if x != 0 {
buffer.Set(table.innerArea.Min.X+x, table.innerArea.Min.Y+i, cell) devidors := DefaultTxBuilder.Build("|", table.FgColors[y], table.BgColors[y])
for _, devidor := range devidors {
buffer.Set(border_pointer_x, pointer_y, devidor)
}
}
}
if table.Seperator {
border := DefaultTxBuilder.Build(strings.Repeat("─", table.Width-2), table.FgColor, table.BgColor)
for i, cell := range border {
buffer.Set(i+1, pointer_y+1, cell)
} }
} }
} }
return buffer return buffer
} }