-
Notifications
You must be signed in to change notification settings - Fork 5
/
table.go
58 lines (46 loc) · 1.16 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
package stdout
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/fatih/color"
"github.com/hideckies/hermit/pkg/common/utils"
"github.com/rodaine/table"
)
type SingleTableItem struct {
Key string
Value string
}
func NewSingleTableItem(key string, value string) SingleTableItem {
return SingleTableItem{
Key: key,
Value: value,
}
}
func PrintSingleTable(title string, table []SingleTableItem) {
fmt.Printf("\n%s\n%s\n", title, strings.Repeat("=", len(title)))
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
for _, item := range table {
fmt.Fprintf(
writer,
"%s\t: %s\n",
item.Key,
item.Value,
)
}
fmt.Fprintln(writer)
writer.Flush()
}
func PrintTable(headers []string, rows [][]string) {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
headersIface := utils.ConvertStringsToInterfaces(headers)
tbl := table.New(headersIface...)
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, row := range rows {
rowIface := utils.ConvertStringsToInterfaces(row)
tbl.AddRow(rowIface...)
}
tbl.Print()
}