-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
print_table.go
66 lines (53 loc) · 1.03 KB
/
print_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
59
60
61
62
63
64
65
66
package print
import (
"fmt"
"github.com/alajmo/mani/core/dao"
)
type Items interface {
GetValue(string, int) string
}
type PrintTableOptions struct {
Output string
Theme dao.Theme
Tree bool
OmitEmpty bool
SuppressEmptyColumns bool
}
func PrintTable[T Items](
data []T,
options PrintTableOptions,
defaultHeaders []string,
taskHeaders []string,
) {
t := CreateTable(options, defaultHeaders, taskHeaders)
// Headers
var headers []any
for _, h := range defaultHeaders {
headers = append(headers, h)
}
for _, h := range taskHeaders {
headers = append(headers, h)
}
t.AppendHeader(headers)
// Rows
for _, item := range data {
var row []any
for i, h := range headers {
value := item.GetValue(fmt.Sprintf("%v", h), i)
row = append(row, value)
}
if options.OmitEmpty {
empty := true
for _, v := range row[1:] {
if v != "" {
empty = false
}
}
if empty {
continue
}
}
t.AppendRow(row)
}
RenderTable(t, options.Output)
}