-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
87 lines (72 loc) · 2.55 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
package main
import (
"github.com/olekukonko/tablewriter"
"os"
"strings"
)
var columnHeader = []string{
//"Name",
"Symbol",
"Price (USD)",
"Volume (USD)",
"Market (USD)",
"Change (1h)",
"Change (24h)",
"Change (7d)"}
var priceColumnName = "Change (1h)"
var NormalColorHeader = tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold, tablewriter.BgGreenColor}
var HighlightColorHeader = tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold, tablewriter.BgRedColor}
var NormalColorColumn = tablewriter.Colors{tablewriter.FgHiWhiteColor, tablewriter.Bold}
var HighlightColorColumn = tablewriter.Colors{tablewriter.FgCyanColor, tablewriter.Bold}
var RedColorColumn = tablewriter.Colors{tablewriter.FgRedColor, tablewriter.Bold}
func BuildTable() *tablewriter.Table {
if len(columnHeader) < 1 {
panic("column header size must > 1 , please check !")
}
//columnIndex := sort.StringSlice(columnHeader).Search(priceColumnName)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(columnHeader)
// todo auto config 👆🏻
buildHeaderColor(table, len(columnHeader), 4)
buildColumnColor(table, len(columnHeader), 4)
// init table
var data [][]string
PullByLine(func(line []string) {
data = append(data, line)
})
for _, row := range data {
if strings.ContainsRune(row[4], '-') {
table.Rich(row, []tablewriter.Colors{NormalColorColumn, NormalColorColumn, NormalColorColumn, NormalColorColumn, RedColorColumn, NormalColorColumn, NormalColorColumn})
} else {
table.Rich(row, []tablewriter.Colors{NormalColorColumn, NormalColorColumn, NormalColorColumn, NormalColorColumn, HighlightColorColumn, NormalColorColumn, NormalColorColumn})
}
}
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
//table.AppendBulk(data) // Add Bulk Data
clearFromTop()
table.Render()
return table
}
func buildHeaderColor(table *tablewriter.Table, len int, highlightIndex int) {
var headerColors []tablewriter.Colors
for i := 0; i < len; i++ {
if i == highlightIndex {
headerColors = append(headerColors, HighlightColorHeader)
} else {
headerColors = append(headerColors, NormalColorHeader)
}
}
table.SetHeaderColor(headerColors...)
}
func buildColumnColor(table *tablewriter.Table, len int, highlightIndex int) {
var columColors []tablewriter.Colors
for i := 0; i < len; i++ {
if i == highlightIndex {
columColors = append(columColors, HighlightColorColumn)
} else {
columColors = append(columColors, NormalColorColumn)
}
}
table.SetColumnColor(columColors...)
}