Skip to content

Commit

Permalink
Merge pull request olekukonko#144 from shreddedbacon/kube-format
Browse files Browse the repository at this point in the history
Add SetNoWhiteSpace and SetTablePadding options to have more kubectl like output
  • Loading branch information
mattn authored Nov 5, 2019
2 parents cc27d85 + fba499b commit b2a28af
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 15 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ table.Render()
#### Table with color Output
![Table with Color](https://cloud.githubusercontent.com/assets/6460392/21101956/bbc7b356-c0a1-11e6-9f36-dba694746efc.png)

#### Example 6 - Set table caption
#### Example 7 - Set table caption
```go
data := [][]string{
[]string{"A", "The Good", "500"},
Expand All @@ -254,7 +254,7 @@ table.Render() // Send output

Note: Caption text will wrap with total width of rendered table.

##### Output 6
##### Output 7
```
+------+-----------------------+--------+
| NAME | SIGN | RATING |
Expand All @@ -267,6 +267,41 @@ Note: Caption text will wrap with total width of rendered table.
Movie ratings.
```

#### Example 8 - Set NoWhiteSpace and TablePadding option
```go
data := [][]string{
{"node1.example.com", "Ready", "compute", "1.11"},
{"node2.example.com", "Ready", "compute", "1.11"},
{"node3.example.com", "Ready", "compute", "1.11"},
{"node4.example.com", "NotReady", "compute", "1.11"},
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Role", "Version"})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(ALIGN_LEFT)
table.SetAlignment(ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
table.AppendBulk(data) // Add Bulk Data
table.Render()
```

##### Output 8
```
NAME STATUS ROLE VERSION
node1.example.com Ready compute 1.11
node2.example.com Ready compute 1.11
node3.example.com Ready compute 1.11
node4.example.com NotReady compute 1.11
```

#### Render table into a string

Instead of rendering the table to `io.Stdout` you can also render it into a string. Go 1.10 introduced the `strings.Builder` type which implements the `io.Writer` interface and can therefore be used for this task. Example:
Expand Down
64 changes: 51 additions & 13 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Table struct {
newLine string
rowLine bool
autoMergeCells bool
noWhiteSpace bool
tablePadding string
hdrLine bool
borders Border
colSize int
Expand Down Expand Up @@ -225,6 +227,16 @@ func (t *Table) SetAlignment(align int) {
t.align = align
}

// Set No White Space
func (t *Table) SetNoWhiteSpace(allow bool) {
t.noWhiteSpace = allow
}

// Set Table Padding
func (t *Table) SetTablePadding(padding string) {
t.tablePadding = padding
}

func (t *Table) SetColumnAlignment(keys []int) {
for _, v := range keys {
switch v {
Expand Down Expand Up @@ -411,27 +423,45 @@ func (t *Table) printHeading() {
for x := 0; x < max; x++ {
// Check if border is set
// Replace with space if not set
fmt.Fprint(t.out, ConditionString(t.borders.Left, t.pColumn, SPACE))
if !t.noWhiteSpace {
fmt.Fprint(t.out, ConditionString(t.borders.Left, t.pColumn, SPACE))
}

for y := 0; y <= end; y++ {
v := t.cs[y]
h := ""

if y < len(t.headers) && x < len(t.headers[y]) {
h = t.headers[y][x]
}
if t.autoFmt {
h = Title(h)
}
pad := ConditionString((y == end && !t.borders.Left), SPACE, t.pColumn)

if t.noWhiteSpace {
pad = ConditionString((y == end && !t.borders.Left), SPACE, t.tablePadding)
}
if is_esc_seq {
fmt.Fprintf(t.out, " %s %s",
format(padFunc(h, SPACE, v),
t.headerParams[y]), pad)
if !t.noWhiteSpace {
fmt.Fprintf(t.out, " %s %s",
format(padFunc(h, SPACE, v),
t.headerParams[y]), pad)
} else {
fmt.Fprintf(t.out, "%s %s",
format(padFunc(h, SPACE, v),
t.headerParams[y]), pad)
}
} else {
fmt.Fprintf(t.out, " %s %s",
padFunc(h, SPACE, v),
pad)
if !t.noWhiteSpace {
fmt.Fprintf(t.out, " %s %s",
padFunc(h, SPACE, v),
pad)
} else {
// the spaces between breaks the kube formatting
fmt.Fprintf(t.out, "%s%s",
padFunc(h, SPACE, v),
pad)
}
}
}
// Next line
Expand Down Expand Up @@ -654,9 +684,11 @@ func (t *Table) printRow(columns [][]string, rowIdx int) {
for y := 0; y < total; y++ {

// Check if border is set
fmt.Fprint(t.out, ConditionString((!t.borders.Left && y == 0), SPACE, t.pColumn))
if !t.noWhiteSpace {
fmt.Fprint(t.out, ConditionString((!t.borders.Left && y == 0), SPACE, t.pColumn))
fmt.Fprintf(t.out, SPACE)
}

fmt.Fprintf(t.out, SPACE)
str := columns[y][x]

// Embedding escape sequence with column value
Expand Down Expand Up @@ -688,11 +720,17 @@ func (t *Table) printRow(columns [][]string, rowIdx int) {

}
}
fmt.Fprintf(t.out, SPACE)
if !t.noWhiteSpace {
fmt.Fprintf(t.out, SPACE)
} else {
fmt.Fprintf(t.out, t.tablePadding)
}
}
// Check if border is set
// Replace with space if not set
fmt.Fprint(t.out, ConditionString(t.borders.Left, t.pColumn, SPACE))
if !t.noWhiteSpace {
fmt.Fprint(t.out, ConditionString(t.borders.Left, t.pColumn, SPACE))
}
fmt.Fprint(t.out, t.newLine)
}

Expand Down Expand Up @@ -804,7 +842,7 @@ func (t *Table) printRowMergeCells(writer io.Writer, columns [][]string, rowIdx
//The new previous line is the current one
previousLine = make([]string, total)
for y := 0; y < total; y++ {
previousLine[y] = strings.TrimRight(strings.Join(columns[y], " ")," ") //Store the full line for multi-lines cells
previousLine[y] = strings.TrimRight(strings.Join(columns[y], " "), " ") //Store the full line for multi-lines cells
}
//Returns the newly added line and wether or not a border should be displayed above.
return previousLine, displayCellBorder
Expand Down
35 changes: 35 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,3 +1145,38 @@ func TestTitle(t *testing.T) {
}
}
}

func TestKubeFormat(t *testing.T) {
data := [][]string{
{"1/1/2014", "jan_hosting", "2233", "$10.98"},
{"1/1/2014", "feb_hosting", "2233", "$54.95"},
{"1/4/2014", "feb_extra_bandwidth", "2233", "$51.00"},
{"1/4/2014", "mar_hosting", "2233", "$30.00"},
}

var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(ALIGN_LEFT)
table.SetAlignment(ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
table.AppendBulk(data) // Add Bulk Data
table.Render()

want := `DATE DESCRIPTION CV2 AMOUNT
1/1/2014 jan_hosting 2233 $10.98
1/1/2014 feb_hosting 2233 $54.95
1/4/2014 feb_extra_bandwidth 2233 $51.00
1/4/2014 mar_hosting 2233 $30.00
`

checkEqual(t, buf.String(), want, "kube format rendering failed")
}

0 comments on commit b2a28af

Please sign in to comment.