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

Commit 9d7aa76

Browse files
committed
fixes header padding error when first column is RightJustified
1 parent 4d2b8bc commit 9d7aa76

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Simply define the table columns and `tabular` will parse the right [format speci
1212

1313
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.
1414

15-
Example (also available in [`example/example.go`](example/example.go):
15+
Example (also available in [`example/example.go`](example/example.go)):
1616

1717
```go
1818
package main

format_test.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,37 @@ import (
88

99
func TestFormat(t *testing.T) {
1010
tab := tabular.New()
11+
tab.Col("id", "ID", 6)
1112
tab.Col("env", "Environment", 14)
1213
tab.Col("cls", "Cluster", 10)
1314
tab.Col("svc", "Service", 25)
1415
tab.Col("hst", "Database Host", 25)
1516
tab.Col("pct", "%CPU", 5)
17+
tab["id"].RightJustified = true
1618
tab["pct"].RightJustified = true
1719

1820
tWant := tabular.Table{
19-
Header: "Environment Cluster Service Database Host %CPU",
20-
SubHeader: "-------------- ---------- ------------------------- ------------------------- -----",
21-
Format: "%-14v %-10v %-25v %-25v %5v\n",
21+
Header: " ID Environment Cluster Service Database Host %CPU",
22+
SubHeader: "------ -------------- ---------- ------------------------- ------------------------- -----",
23+
Format: "%6v %-14v %-10v %-25v %-25v %5v\n",
2224
}
2325

2426
// Test Printing
2527
want := tWant.Format
26-
if got := tab.Print("env", "cls", "svc", "hst", "pct"); got != want {
28+
if got := tab.Print("id", "env", "cls", "svc", "hst", "pct"); got != want {
2729
t.Errorf("ERROR: tab.Print() failed\n want: %q\n got: %q", want, got)
2830
}
2931

3032
// Test Parsing
31-
if tGot := tab.Parse("env", "cls", "svc", "hst", "pct"); tGot != tWant {
32-
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant, tGot)
33+
if tGot := tab.Parse("id", "env", "cls", "svc", "hst", "pct"); tGot != tWant {
34+
if tGot.Header != tWant.Header {
35+
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant.Header, tGot.Header)
36+
}
37+
if tGot.SubHeader != tWant.SubHeader {
38+
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant.SubHeader, tGot.SubHeader)
39+
}
40+
if tGot.Format != tWant.Format {
41+
t.Errorf("ERROR: tab.Parse() failed\n want: %v\n got: %v", tWant.Format, tGot.Format)
42+
}
3343
}
3444
}

tabular.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,19 @@ func (cl Columns) parse(cols ...string) Table {
7878
var header string
7979
var subHeader string
8080
var format string
81+
var space string
8182
for _, c := range cols {
82-
header = header + " " + fmt.Sprintf(cl[c].f(), cl[c].Name)
83-
subHeader = subHeader + " " + fmt.Sprintf(cl[c].f(), r(cl[c].Length))
84-
format = format + " " + cl[c].f()
83+
cf := cl[c].f()
84+
header = header + space + fmt.Sprintf(cf, cl[c].Name)
85+
subHeader = subHeader + space + fmt.Sprintf(cf, r(cl[c].Length))
86+
format = format + space + cf
87+
space = " "
8588
}
8689

8790
return Table{
88-
Header: strings.TrimSpace(header),
89-
SubHeader: strings.TrimSpace(subHeader),
90-
Format: strings.TrimSpace(format) + "\n",
91+
Header: header,
92+
SubHeader: subHeader,
93+
Format: format + "\n",
9194
}
9295
}
9396

0 commit comments

Comments
 (0)