change TextAlign type to Align\n fix backgound full fill

This commit is contained in:
wanzysky 2016-11-29 12:21:02 +08:00
parent 0d2f54137b
commit c0ca424444
2 changed files with 8 additions and 8 deletions

View File

@ -1,6 +1,6 @@
package main package main
import "termui" import "github.com/gizak/termui"
func main() { func main() {
err := termui.Init() err := termui.Init()
@ -35,7 +35,7 @@ func main() {
table.Rows = rows table.Rows = rows
table.FgColor = termui.ColorWhite table.FgColor = termui.ColorWhite
table.BgColor = termui.ColorDefault table.BgColor = termui.ColorDefault
table.TextAlign = "center" table.TextAlign = termui.AlignCenter
table.Seperator = false table.Seperator = false
table.Analysis() table.Analysis()
table.SetSize() table.SetSize()

View File

@ -23,14 +23,13 @@ type Table struct {
FgColors []Attribute FgColors []Attribute
BgColors []Attribute BgColors []Attribute
Seperator bool Seperator bool
TextAlign string TextAlign Align
} }
func NewTable() *Table { func NewTable() *Table {
table := &Table{Block: *NewBlock()} table := &Table{Block: *NewBlock()}
table.FgColor = ColorWhite table.FgColor = ColorWhite
table.BgColor = ColorDefault table.BgColor = ColorDefault
table.TextAlign = "left"
table.Seperator = true table.Seperator = true
return table return table
} }
@ -118,11 +117,12 @@ func (table *Table) CalculatePosition(x int, y int, x_coordinate *int, y_coordib
*cell_beginning += table.CellWidth[x-1] + 3 *cell_beginning += table.CellWidth[x-1] + 3
} }
if table.TextAlign == "right" { switch table.TextAlign {
case AlignRight:
*x_coordinate = *cell_beginning + (table.CellWidth[x] - len(table.Rows[y][x])) + 2 *x_coordinate = *cell_beginning + (table.CellWidth[x] - len(table.Rows[y][x])) + 2
} else if table.TextAlign == "center" { case AlignCenter:
*x_coordinate = *cell_beginning + (table.CellWidth[x]-len(table.Rows[y][x]))/2 + 2 *x_coordinate = *cell_beginning + (table.CellWidth[x]-len(table.Rows[y][x]))/2 + 2
} else { default:
*x_coordinate = *cell_beginning + 2 *x_coordinate = *cell_beginning + 2
} }
} }
@ -137,7 +137,7 @@ func (table *Table) Buffer() Buffer {
for y, row := range table.Rows { for y, row := range table.Rows {
for x, cell := range row { for x, cell := range row {
table.CalculatePosition(x, y, &pointer_x, &pointer_y, &border_pointer_x) 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]) backgraound := DefaultTxBuilder.Build(strings.Repeat(" ", table.CellWidth[x]+3), table.BgColors[y], table.BgColors[y])
cells := DefaultTxBuilder.Build(cell, table.FgColors[y], table.BgColors[y]) cells := DefaultTxBuilder.Build(cell, table.FgColors[y], table.BgColors[y])
for i, back := range backgraound { for i, back := range backgraound {