Skip to content

Commit

Permalink
New function table.AppendRowLine() to manually add row separators.
Browse files Browse the repository at this point in the history
  • Loading branch information
mittwingate committed Feb 3, 2019
1 parent e6d60cf commit 8ecdf66
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,34 @@ Note: Caption text will wrap with total width of rendered table.
+------+-----------------------+--------+
Movie ratings.
```
#### Example 7 - Grouping by adding row lines
```go
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Food", "Rating"})

table.Append([]string{"Filet Mignon", "****"})
table.Append([]string{"New York Steak", "***"})

table.AppendRowLine() // Manually add separator

table.Append([]string{"Broccoli", "*"})
table.Append([]string{"Cauliflower", "***"})

table.Render() // Send output
```

##### Output 7
```
+----------------+--------+
| FOOD | RATING |
+----------------+--------+
| Filet Mignon | **** |
| New York Steak | *** |
+----------------+--------+
| Broccoli | * |
| Cauliflower | *** |
+----------------+--------+
```

#### TODO
- ~~Import Directly from CSV~~ - `done`
Expand Down
16 changes: 15 additions & 1 deletion table.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Table struct {
columnsParams []string
footerParams []string
columnsAlign []int
rowLineIdxs map[int]int
}

// Start New Table
Expand Down Expand Up @@ -114,7 +115,9 @@ func NewWriter(writer io.Writer) *Table {
headerParams: []string{},
columnsParams: []string{},
footerParams: []string{},
columnsAlign: []int{}}
columnsAlign: []int{},
rowLineIdxs: make(map[int]int),
}
return t
}

Expand Down Expand Up @@ -274,6 +277,11 @@ func (t *Table) SetBorders(border Border) {
t.borders = border
}

// Append a separator after the current line
func (t *Table) AppendRowLine() {
t.rowLineIdxs[len(t.lines)-1]++
}

// Append row to table
func (t *Table) Append(row []string) {
rowSize := len(t.headers)
Expand Down Expand Up @@ -672,6 +680,12 @@ func (t *Table) printRow(columns [][]string, rowIdx int) {
if t.rowLine {
t.printLine(true)
}

// Any manually added row lines?
for t.rowLineIdxs[rowIdx] > 0 {
t.printLine(true)
t.rowLineIdxs[rowIdx]--
}
}

// Print the rows of the table and merge the cells that are identical
Expand Down
25 changes: 25 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,3 +1119,28 @@ func TestTitle(t *testing.T) {
}
}
}

func ExampleRowLine() {
table := NewWriter(os.Stdout)
table.SetHeader([]string{"Food", "Rating"})

table.Append([]string{"Filet Mignon", "****"})
table.Append([]string{"New York Steak", "***"})

table.AppendRowLine()

table.Append([]string{"Broccoli", "*"})
table.Append([]string{"Cauliflower", "***"})

table.Render() // Send output

// Output: +----------------+--------+
// | FOOD | RATING |
// +----------------+--------+
// | Filet Mignon | **** |
// | New York Steak | *** |
// +----------------+--------+
// | Broccoli | * |
// | Cauliflower | *** |
// +----------------+--------+
}

0 comments on commit 8ecdf66

Please sign in to comment.