-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliTable.go
128 lines (100 loc) · 2.23 KB
/
cliTable.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
package lib
import (
"bufio"
"fmt"
"strings"
)
type CLITable struct {
sizes []int
colNames []string
rows [][]string
currentRow int
currentCol int
}
func NewCLITable(colNames []string) *CLITable {
sizes := make([]int, len(colNames))
rows := [][]string{
[]string{},
}
for k := range colNames {
sizes[k] = len(colNames[k])
rows[0] = append(rows[0], colNames[k])
}
return &CLITable{
sizes,
colNames,
rows,
0,
0,
}
}
// row := t.NewRow()
// row[0] = col.Name
// row[1] = col.DataType
// row[2] = fmt.Sprintf("%d", col.MaxLength)
// if col.IsNullable {
// row[3] = "YES"
// } else {
// row[3] = "NO"
// }
// row[4] = col.Default
// row[5] = col.Extra
// t.AddRow(row)
func (c *CLITable) NewRow() []string {
return make([]string, len(c.sizes))
}
func (c *CLITable) AddRow(row []string) {
if len(row) != len(c.sizes) {
panic(fmt.Sprintf("New column (%d) count does not match initial column count (%d)", len(row), len(c.sizes)))
}
c.rows = append(c.rows, row)
for k := range row {
if len(row[k]) > c.sizes[k] {
c.sizes[k] = len(row[k])
}
}
}
func (c *CLITable) Row() {
c.currentRow++
c.currentCol = 0
c.rows = append(c.rows, make([]string, len(c.sizes)))
}
func (c *CLITable) Col(val string) {
c.rows[c.currentRow][c.currentCol] = val
if len(val) > c.sizes[c.currentCol] {
c.sizes[c.currentCol] = len(val)
}
c.currentCol++
}
func (c *CLITable) Colf(template string, args ...interface{}) {
c.Col(fmt.Sprintf(template, args...))
}
func (c *CLITable) String() string {
totalWidth := 0
for k := range c.sizes {
totalWidth += c.sizes[k] + 3
}
// Last pipe
totalWidth++
s := ""
s += fmt.Sprintf("+" + strings.Repeat("-", totalWidth-2) + "+\n")
for n := range c.rows {
for col := range c.rows[n] {
s += "| " + fmt.Sprintf(fmt.Sprintf("%%-%dv", c.sizes[col]), c.rows[n][col]) + " "
}
s += "|"
s += fmt.Sprint("\n")
if n == 0 {
s += fmt.Sprintf("+" + strings.Repeat("-", totalWidth-2) + "+")
s += fmt.Sprint("\n")
}
}
s += fmt.Sprintf("+" + strings.Repeat("-", totalWidth-2) + "+")
return s
}
func ReadCliInput(reader *bufio.Reader, title string) string {
fmt.Print("> " + title)
val, _ := reader.ReadString('\n')
val = strings.Replace(val, "\n", "", -1)
return val
}