-
Notifications
You must be signed in to change notification settings - Fork 314
/
table.go
55 lines (46 loc) · 1.33 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
package table
import (
"io"
"runtime"
"github.com/loft-sh/devpod/pkg/log"
"github.com/loft-sh/devpod/pkg/scanner"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
)
func PrintTable(s log.Logger, header []string, values [][]string) {
PrintTableWithOptions(s, header, values, nil)
}
// PrintTableWithOptions prints a table with header columns and string values
func PrintTableWithOptions(s log.Logger, header []string, values [][]string, modify func(table *tablewriter.Table)) {
reader, writer := io.Pipe()
defer writer.Close()
done := make(chan struct{})
go func() {
defer close(done)
sa := scanner.NewScanner(reader)
for sa.Scan() {
s.WriteString(logrus.InfoLevel, " "+sa.Text()+"\n")
}
}()
table := tablewriter.NewWriter(writer)
table.SetHeader(header)
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
colors := []tablewriter.Colors{}
for range header {
colors = append(colors, tablewriter.Color(tablewriter.FgGreenColor))
}
table.SetHeaderColor(colors...)
}
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.AppendBulk(values)
if modify != nil {
modify(table)
}
// Render
_, _ = writer.Write([]byte("\n"))
table.Render()
_, _ = writer.Write([]byte("\n"))
_ = writer.Close()
<-done
}