forked from dynport/gocli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
216 lines (193 loc) · 4.43 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gocli
import (
"bufio"
"fmt"
"math"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
)
func NewTable() *Table {
return &Table{
Columns: [][]string{},
Separator: "\t",
}
}
type Table struct {
Columns [][]string
Separator string
header []string
SortBy int
}
func (t *Table) Select(message string) int {
for {
fmt.Fprintf(os.Stdout, t.StringWithIndex()+"\n"+message+": ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
i, e := strconv.Atoi(scanner.Text())
if e == nil {
if i > 0 && i <= len(t.Columns) {
return i - 1
}
}
}
}
func (t *Table) Len() int { return len(t.Columns) }
func (t *Table) Swap(a, b int) { t.Columns[a], t.Columns[b] = t.Columns[b], t.Columns[a] }
func (t *Table) Less(a, b int) bool {
if len(t.Columns[a]) <= t.SortBy {
return false
} else if len(t.Columns[b]) <= t.SortBy {
return true
} else {
return fmt.Sprint(t.Columns[a][t.SortBy]) <= fmt.Sprintf(t.Columns[b][t.SortBy])
}
return true
}
func (t *Table) String() string {
return strings.Join(t.Lines(false), "\n")
}
func (t *Table) StringWithIndex() string {
return strings.Join(t.Lines(true), "\n")
}
var uncolorRegexp = regexp.MustCompile("\033\\[38;5;\\d+m([^\033]+)\033\\[0m")
func stringLength(s string) int {
return utf8.RuneCountInString((uncolorRegexp.ReplaceAllString(s, "$1")))
}
func (t *Table) Lines(printIndex bool) (lines []string) {
toPrint := t.Columns
if len(t.header) > 0 {
toPrint = append([][]string{t.header}, toPrint...)
}
lengths := map[int]int{}
for _, c := range toPrint {
for i, v := range c {
if l := stringLength(v); l > lengths[i] {
lengths[i] = l
}
}
}
for row, col := range toPrint {
cl := []string{}
if printIndex {
col = append([]string{strconv.Itoa(row + 1)}, col...)
}
for i, v := range col {
theLen := lengths[i]
if printIndex {
if i == 0 {
theLen = intLength(len(t.Columns))
} else {
theLen = lengths[i-1]
}
}
pad := theLen - stringLength(v)
cl = append(cl, v+strings.Repeat(" ", pad))
}
lines = append(lines, strings.Join(cl, t.Separator))
}
return
}
func (t *Table) toPrint() [][]string {
toPrint := t.Columns
if len(t.header) > 0 {
toPrint = append([][]string{t.header}, toPrint...)
}
return toPrint
}
func (t *Table) lengths() map[int]int {
m := map[int]int{}
for _, c := range t.toPrint() {
for i, v := range c {
if l := stringLength(v); l > m[i] {
m[i] = l
}
}
}
return m
}
func intLength(i int) int {
if i == 0 {
return 1
} else if i < 0 {
return intLength(int(math.Abs(float64(i)))) + 1
}
return int(math.Ceil(math.Log10(float64(i + 1))))
}
func (t *Table) AddStrings(list []string) {
t.Columns = append(t.Columns, list)
}
// Add adds a column to the table
func (t *Table) Add(cols ...interface{}) {
t.AddStrings(convertIArray(cols...))
}
func convertIArray(in ...interface{}) (out []string) {
out = make([]string, 0, len(in))
for _, v := range in {
out = append(out, vToS(v))
}
return out
}
func vToS(in interface{}) string {
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr && v.IsNil() {
return "<nil>"
}
switch c := in.(type) {
case *string:
return *c
case *int:
return fmt.Sprint(*c)
case *int64:
return fmt.Sprint(*c)
case *float64:
return fmt.Sprint(*c)
case *time.Time:
return fmt.Sprint(*c)
case *bool:
return fmt.Sprint(*c)
default:
return fmt.Sprint(in)
}
}
func (t *Table) Header(cols ...interface{}) {
t.header = convertIArray(cols...)
}
// Dereferencing pointers if not nil
// TODO: Please someone tell me, how to do this right!
func (t *Table) AddP(cols ...interface{}) {
converted := make([]string, 0, len(cols))
var str string
for _, v := range cols {
if value := reflect.ValueOf(v); value.Kind() == reflect.Ptr {
indirect := reflect.Indirect(value)
switch {
case indirect != reflect.Zero(value.Type()) && indirect.IsValid() == true:
switch {
case indirect.Kind() == reflect.String:
str = fmt.Sprint(indirect.String())
case indirect.Kind() == reflect.Int:
str = fmt.Sprint(indirect.Int())
case indirect.Kind() == reflect.Float32:
str = fmt.Sprint(indirect.Float())
case indirect.Kind() == reflect.Bool:
str = fmt.Sprint(indirect.Bool())
case indirect.Kind() == reflect.Slice:
str = fmt.Sprint(v)
default:
str = fmt.Sprint(v)
}
default:
str = ""
}
} else {
str = fmt.Sprint(v)
}
converted = append(converted, str)
}
t.AddStrings(converted)
}