-
Notifications
You must be signed in to change notification settings - Fork 129
/
table.go
45 lines (34 loc) · 862 Bytes
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ui
import (
"encoding/json"
"io"
"github.com/olekukonko/tablewriter"
)
type TableData interface {
Table() (header []string, data [][]string)
}
func (ui *UI) Table(tableData TableData, writer io.Writer) {
table := tablewriter.NewWriter(writer)
table.SetBorder(false)
table.SetHeaderLine(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
header, data := tableData.Table()
if len(header) > 0 {
table.SetHeader(header)
}
for _, v := range data {
table.Append(v)
}
table.Render()
}
func (ui *UI) JSONTable(tableData TableData, writer io.Writer) error {
_, data := tableData.Table()
return json.NewEncoder(writer).Encode(data)
}
func (ui *UI) NewArrayTable(a [][]string) ArrayTable {
return ArrayTable(a)
}
type ArrayTable [][]string
func (a ArrayTable) Table() (header []string, data [][]string) {
return []string{}, a
}