From c0ca4244445b0e20b12921890eeb3f4eb68459a5 Mon Sep 17 00:00:00 2001 From: wanzysky Date: Tue, 29 Nov 2016 12:21:02 +0800 Subject: [PATCH] change TextAlign type to Align\n fix backgound full fill --- _example/table.go | 4 ++-- table.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/_example/table.go b/_example/table.go index 76916f5..7c86493 100644 --- a/_example/table.go +++ b/_example/table.go @@ -1,6 +1,6 @@ package main -import "termui" +import "github.com/gizak/termui" func main() { err := termui.Init() @@ -35,7 +35,7 @@ func main() { table.Rows = rows table.FgColor = termui.ColorWhite table.BgColor = termui.ColorDefault - table.TextAlign = "center" + table.TextAlign = termui.AlignCenter table.Seperator = false table.Analysis() table.SetSize() diff --git a/table.go b/table.go index cde3ae7..319b897 100644 --- a/table.go +++ b/table.go @@ -23,14 +23,13 @@ type Table struct { FgColors []Attribute BgColors []Attribute Seperator bool - TextAlign string + TextAlign Align } func NewTable() *Table { table := &Table{Block: *NewBlock()} table.FgColor = ColorWhite table.BgColor = ColorDefault - table.TextAlign = "left" table.Seperator = true 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 } - if table.TextAlign == "right" { + switch table.TextAlign { + case AlignRight: *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 - } else { + default: *x_coordinate = *cell_beginning + 2 } } @@ -137,7 +137,7 @@ func (table *Table) Buffer() Buffer { for y, row := range table.Rows { 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]) + 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]) for i, back := range backgraound {