Skip to content

Commit

Permalink
Merge pull request #19 from arteev/feature/REFACTOR-1
Browse files Browse the repository at this point in the history
Feature/refactor 1
  • Loading branch information
arteev committed Jun 7, 2018
2 parents 4000a4c + 9b351dc commit 864188b
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 218 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ _testmain.go

.idea/*
*.out
*.test.*
*.test.*

.vscode
Binary file added _example/console
Binary file not shown.
24 changes: 13 additions & 11 deletions _example/customborder.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package main

import (
"os"

"github.com/arteev/fmttab"
)

func main() {
BORDER_MY := fmttab.Border(100)
fmttab.Borders[BORDER_MY] = map[fmttab.BorderKind]string{
borderMy := fmttab.Border(100)
fmttab.Borders[borderMy] = map[fmttab.BorderKind]string{

fmttab.BKVertical: "|",
fmttab.BKHorizontal : "-",
fmttab.BKHorizontalBorder : "_",
fmttab.BKVertical: "|",
fmttab.BKHorizontal: "-",
fmttab.BKHorizontalBorder: "_",
}
tab := fmttab.New("Custom table:",BORDER_MY,nil)
tab.AddColumn("Key",15,fmttab.AlignLeft).
AddColumn("Value",15,fmttab.AlignRight)
tab := fmttab.New("Custom table:", borderMy, nil)
tab.AddColumn("Key", 15, fmttab.AlignLeft).
AddColumn("Value", 15, fmttab.AlignRight)
tab.Data = []map[string]interface{}{
{
"Key":"Key One",
"Key": "Key One",
"Value": "Value One",
},
{
"Key":"Key Two",
"Key": "Key Two",
"Value": "Value Two",
},
}
tab.WriteTo(os.Stdout)
}
}
3 changes: 2 additions & 1 deletion _example/envlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func main() {
tab := fmttab.New("Environments", fmttab.BorderDouble, nil)
tab.CloseEachColumn = true
tab.AddColumn("ENV", 25, fmttab.AlignLeft).
AddColumn("VALUE", 25, fmttab.AlignLeft)
for _, env := range os.Environ() {
Expand All @@ -18,6 +19,6 @@ func main() {
"VALUE": keyval[1],
})
}
tab.Columns[0].Caption = "Environment"
tab.Columns.Get(0).Caption = "Environment"
tab.WriteTo(os.Stdout)
}
31 changes: 16 additions & 15 deletions _example/envlist_2.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package main

import (
"os"
"strings"

"github.com/arteev/fmttab"
"github.com/arteev/fmttab/columns"
)

func main() {
tab := fmttab.New("Environments:",fmttab.BorderNone,nil)
tab.Columns = []*fmttab.Column{
{
Name:"ENV",Width:25,Aling:fmttab.AlignLeft,
},
{
Name:"VALUE",Width:25,Aling:fmttab.AlignLeft,
},
}
for _,env:=range os.Environ() {
keyval := strings.Split(env,"=")
tab.Data = append(tab.Data,map[string]interface{} {
"ENV": keyval[0],
"VALUE" : keyval[1],
tab := fmttab.New("Environments:", fmttab.BorderNone, nil)
tab.AddColumn("ENV", 25, fmttab.AlignLeft)
tab.Columns.Add(&columns.Column{
Name: "VALUE", Width: 25, Aling: fmttab.AlignLeft, Visible: true,
})

for _, env := range os.Environ() {
keyval := strings.Split(env, "=")
tab.Data = append(tab.Data, map[string]interface{}{
"ENV": keyval[0],
"VALUE": keyval[1],
})
}
tab.WriteTo(os.Stdout)
}
}
75 changes: 62 additions & 13 deletions columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package columns

import (
"errors"
"strconv"
)

const (
Expand Down Expand Up @@ -32,26 +33,28 @@ type Column struct {
}

//A Columns array of the columns
type Columns []*Column
type Columns struct {
columns []*Column
}

//IsAutoSize returns auto sizing of width column
func (t Column) IsAutoSize() bool {
return t.Width == WidthAuto
func (c Column) IsAutoSize() bool {
return c.Width == WidthAuto
}

//Len returns count columns
func (c *Columns) Len() int {
if c == nil {
return 0
}
return len(*c)
return len(c.columns)
}

//FindByName returns columns by name if exists or nil
func (c *Columns) FindByName(name string) *Column {
for i := range *c {
if (*c)[i].Name == name {
return (*c)[i]
for i, col := range c.columns {
if col.Name == name {
return c.columns[i]
}
}
return nil
Expand All @@ -69,27 +72,73 @@ func (c *Columns) NewColumn(name, caption string, width int, aling Align) (*Colu
Aling: aling,
Visible: true,
}
*c = append(*c, column)
c.columns = append(c.columns, column)
return column, nil
}

//Add append column with check exists
func (c *Columns) Add(col *Column) error {
for i := range *c {
if (*c)[i] == col {
for i := range c.columns {
if c.columns[i] == col {
return ErrorAlreadyExists
}
}
*c = append(*c, col)
if c.FindByName(col.Name) != nil {
return ErrorAlreadyExists
}

c.columns = append(c.columns, col)
return nil
}

//ColumnsVisible returns count visible columns
func (c *Columns) ColumnsVisible() (res Columns) {
for i, col := range *c {
for _, col := range c.columns {
if col.Visible {
res = append(res, (*c)[i])
res.columns = append(res.columns, col)
}
}
return
}

//Columns gets array columns
func (c *Columns) Columns() []Column {
var result []Column
for _, col := range c.columns {
result = append(result, *col)
}
return result
}

//Get returns columns by index
func (c *Columns) Get(index int) *Column {
return c.columns[index]
}

//Visit bypasses the columns
func (c *Columns) Visit(f func(c *Column) error) error {
for i := range c.columns {
err := f(c.columns[i])
if err != nil {
return err
}
}
return nil
}

//GetWidth returns the width of the column or the maximum possible width
func (c *Column) GetWidth() int {
if c.Width < c.MaxLen {
return c.MaxLen
}
return c.Width

}

//GetMaskFormat returns a pattern string for formatting text in table column alignment
func (c *Column) GetMaskFormat() string {
if c.Aling == AlignLeft {
return "%-" + strconv.Itoa(c.GetWidth()) + "v"
}
return "%" + strconv.Itoa(c.GetWidth()) + "v"
}
15 changes: 10 additions & 5 deletions columns/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestColumnsVisible(t *testing.T) {
if cls := columns.ColumnsVisible(); cls.Len() != 2 {
t.Errorf("Excepted CountVisible %d, got %d", 2, cls.Len())
}
columns[0].Visible = false
columns.columns[0].Visible = false
if cls := columns.ColumnsVisible(); cls.Len() != 1 {
t.Errorf("Excepted CountVisible %d, got %d", 1, cls.Len())
}
Expand All @@ -48,11 +48,11 @@ func TestColumns(t *testing.T) {
t.Errorf("Excepted %q, got %q", "Col1", col1.Name)
}
if col1.IsAutoSize() {
t.Errorf("Excepted IsAutoSize=false, got %q", col1.IsAutoSize())
t.Errorf("Excepted IsAutoSize=false, got %t", col1.IsAutoSize())
}
col1.Width = WidthAuto
if !col1.IsAutoSize() {
t.Errorf("Excepted IsAutoSize=true, got %q", col1.IsAutoSize())
t.Errorf("Excepted IsAutoSize=true, got %t", col1.IsAutoSize())
}

if columns.Len() != 1 {
Expand All @@ -68,8 +68,8 @@ func TestColumns(t *testing.T) {
t.Errorf("Excepted nil,got %v", colnot)
}

if col1 != columns[0] {
t.Errorf("Excepted %v,got %v", col1, columns[0])
if col1 != columns.columns[0] {
t.Errorf("Excepted %v,got %v", col1, columns.columns[0])
}

err = columns.Add(col1)
Expand All @@ -89,4 +89,9 @@ func TestColumns(t *testing.T) {
t.Errorf("Excepted %v, got %v", ErrorAlreadyExists, err)
}

err = columns.Add(&Column{Name: col1.Name})
if err != ErrorAlreadyExists {
t.Errorf("Excepted %v, got %v", ErrorAlreadyExists, err)
}

}
21 changes: 2 additions & 19 deletions fmttab.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fmttab

import (
"strconv"
"unicode/utf8"

"github.com/arteev/fmttab/columns"
Expand Down Expand Up @@ -106,7 +105,7 @@ type Table struct {
Columns columns.Columns
Data []map[string]interface{}
VisibleHeader bool
masks map[*columns.Column]string
masks map[string]string
columnsvisible columns.Columns
}

Expand All @@ -122,23 +121,6 @@ func trimEnds(val string, max int) string {
return Trimend[:max]
}

//GetMaskFormat returns a pattern string for formatting text in table column alignment
func (t *Table) GetMaskFormat(c *columns.Column) string {
if c.Aling == AlignLeft {
return "%-" + strconv.Itoa(t.getWidth(c)) + "v"
}
return "%" + strconv.Itoa(t.getWidth(c)) + "v"
}

//must be calculated before call
func (t *Table) getWidth(c *columns.Column) int {
if c.IsAutoSize() || t.autoSize > 0 {
return c.MaxLen
}
return c.Width

}

//AddColumn adds a column to the table
func (t *Table) AddColumn(name string, width int, aling columns.Align) *Table {
_, err := t.Columns.NewColumn(name, name, width, aling)
Expand Down Expand Up @@ -177,6 +159,7 @@ func (t *Table) CountData() int {

//SetBorder - set type of border table
func (t *Table) SetBorder(b Border) {
//TODO: set custom border
t.border = b
}

Expand Down
4 changes: 2 additions & 2 deletions fmttab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestHideColumn(t *testing.T) {
tab := New("Table", BorderThin, nil)
tab.AddColumn("Column1", 8, AlignLeft)
tab.AddColumn("Column2", 8, AlignLeft)
tab.Columns[tab.Columns.Len()-1].Visible = false
tab.Columns.Get(tab.Columns.Len() - 1).Visible = false
org := fmt.Sprintf("Table%[1]s┌────────┐%[1]s│Column1 │%[1]s├────────┤%[1]s└────────┘%[1]s", eol.EOL)
res := tab.String()
if org != res {
Expand All @@ -232,7 +232,7 @@ func TestHideColumn(t *testing.T) {
t.Errorf("Excepted \n%q, got:\n%q", org2, res)
}

tab.Columns[0].Visible = false
tab.Columns.Get(0).Visible = false
res = tab.String()
if "" != res {
t.Errorf("Excepted \n%q, got:\n%q", "", res)
Expand Down

0 comments on commit 864188b

Please sign in to comment.