This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
table.go
134 lines (114 loc) · 2.58 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package textprint
import (
"io"
"reflect"
"slices"
"strings"
"text/tabwriter"
"github.com/stealthrocket/timecraft/internal/stream"
)
type TableOption[T any] func(*tableWriter[T])
func Header[T any](enable bool) TableOption[T] {
return func(t *tableWriter[T]) { t.header = enable }
}
func List[T any](enable bool) TableOption[T] {
return func(t *tableWriter[T]) { t.list = enable }
}
func OrderBy[T any](f func(T, T) int) TableOption[T] {
return func(t *tableWriter[T]) { t.orderBy = f }
}
func NewTableWriter[T any](w io.Writer, opts ...TableOption[T]) stream.WriteCloser[T] {
t := &tableWriter[T]{
output: w,
header: true,
}
for _, opt := range opts {
opt(t)
}
return t
}
type tableWriter[T any] struct {
output io.Writer
values []T
header bool
list bool
orderBy func(T, T) int
}
func (t *tableWriter[T]) Write(values []T) (int, error) {
t.values = append(t.values, values...)
return len(values), nil
}
func (t *tableWriter[T]) Close() error {
tw := tabwriter.NewWriter(t.output, 0, 4, 2, ' ', 0)
if t.orderBy != nil {
slices.SortFunc(t.values, t.orderBy)
}
valueOf := func(values []T, index int) reflect.Value {
return reflect.ValueOf(&values[index]).Elem()
}
var v T
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Pointer {
valueType = valueType.Elem()
valueOf = func(values []T, index int) reflect.Value {
return reflect.ValueOf(values[index]).Elem()
}
}
var columns []string
var encoders []encodeFunc
for _, f := range reflect.VisibleFields(valueType) {
name := f.Name
if textTag := f.Tag.Get("text"); textTag != "" {
tag := strings.Split(textTag, ",")
name, tag = tag[0], tag[1:]
for _, s := range tag {
switch s {
// TODO: other tags
}
}
}
if name == "-" {
continue
}
columns = append(columns, name)
encoders = append(encoders, encodeFuncOfStructField(f.Type, f.Index))
}
if t.list {
columns = columns[:1]
encoders = encoders[:1]
}
if t.header {
for i, name := range columns {
if i != 0 {
if _, err := io.WriteString(tw, "\t"); err != nil {
return err
}
}
if _, err := io.WriteString(tw, name); err != nil {
return err
}
}
if _, err := io.WriteString(tw, "\n"); err != nil {
return err
}
}
for n := range t.values {
v := valueOf(t.values, n)
w := io.Writer(tw)
for i, enc := range encoders {
if i != 0 {
_, err := io.WriteString(w, "\t")
if err != nil {
return err
}
}
if err := enc(w, v); err != nil {
return err
}
}
if _, err := io.WriteString(w, "\n"); err != nil {
return err
}
}
return tw.Flush()
}