Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from InVisionApp/heading-fix
Browse files Browse the repository at this point in the history
fixes header padding error when first column is RightJustified
  • Loading branch information
vmogilev authored May 10, 2018
2 parents 4d2b8bc + 9d7aa76 commit 7afe17a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Simply define the table columns and `tabular` will parse the right [format speci

Table columns can be defined once and then reused over and over again making it easy to modify column length and heading in one place. And a subset of columns can be specified during `tabular.Print()` or `tabular.Parse()` calls to modify the table's title without redefining it.

Example (also available in [`example/example.go`](example/example.go):
Example (also available in [`example/example.go`](example/example.go)):

```go
package main
Expand Down
22 changes: 16 additions & 6 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,37 @@ import (

func TestFormat(t *testing.T) {
tab := tabular.New()
tab.Col("id", "ID", 6)
tab.Col("env", "Environment", 14)
tab.Col("cls", "Cluster", 10)
tab.Col("svc", "Service", 25)
tab.Col("hst", "Database Host", 25)
tab.Col("pct", "%CPU", 5)
tab["id"].RightJustified = true
tab["pct"].RightJustified = true

tWant := tabular.Table{
Header: "Environment Cluster Service Database Host %CPU",
SubHeader: "-------------- ---------- ------------------------- ------------------------- -----",
Format: "%-14v %-10v %-25v %-25v %5v\n",
Header: " ID Environment Cluster Service Database Host %CPU",
SubHeader: "------ -------------- ---------- ------------------------- ------------------------- -----",
Format: "%6v %-14v %-10v %-25v %-25v %5v\n",
}

// Test Printing
want := tWant.Format
if got := tab.Print("env", "cls", "svc", "hst", "pct"); got != want {
if got := tab.Print("id", "env", "cls", "svc", "hst", "pct"); got != want {
t.Errorf("ERROR: tab.Print() failed\n want: %q\n got: %q", want, got)
}

// Test Parsing
if tGot := tab.Parse("env", "cls", "svc", "hst", "pct"); tGot != tWant {
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant, tGot)
if tGot := tab.Parse("id", "env", "cls", "svc", "hst", "pct"); tGot != tWant {
if tGot.Header != tWant.Header {
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant.Header, tGot.Header)
}
if tGot.SubHeader != tWant.SubHeader {
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant.SubHeader, tGot.SubHeader)
}
if tGot.Format != tWant.Format {
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant.Format, tGot.Format)
}
}
}
15 changes: 9 additions & 6 deletions tabular.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ func (cl Columns) parse(cols ...string) Table {
var header string
var subHeader string
var format string
var space string
for _, c := range cols {
header = header + " " + fmt.Sprintf(cl[c].f(), cl[c].Name)
subHeader = subHeader + " " + fmt.Sprintf(cl[c].f(), r(cl[c].Length))
format = format + " " + cl[c].f()
cf := cl[c].f()
header = header + space + fmt.Sprintf(cf, cl[c].Name)
subHeader = subHeader + space + fmt.Sprintf(cf, r(cl[c].Length))
format = format + space + cf
space = " "
}

return Table{
Header: strings.TrimSpace(header),
SubHeader: strings.TrimSpace(subHeader),
Format: strings.TrimSpace(format) + "\n",
Header: header,
SubHeader: subHeader,
Format: format + "\n",
}
}

Expand Down

0 comments on commit 7afe17a

Please sign in to comment.