-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
97 lines (84 loc) · 2.47 KB
/
setup.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
package main
import (
"errors"
"fmt"
"strings"
)
func genWidths(row []string) []int {
var lengths []int
for _, j := range row {
lenJ := len([]rune(j)) + 2
fmt.Println(j, []rune(j), lenJ)
// Rows should not be longer than 10, nor shorter than 5
if lenJ > 10 {
lenJ = 10
} else if lenJ < 5 {
lenJ = 5
}
lengths = append(lengths, lenJ)
}
return lengths
}
func genFilters(filterString string, cols []string) ([]func([]string) bool, error) {
var filters []func([]string) bool
// Create a map of all columns, so that we can search for them
colsToInt := make(map[string]int)
for i, col := range cols {
colsToInt[col] = i
colNum := fmt.Sprintf("_%d", i+1)
colsToInt[colNum] = i
}
// Break the filterString into indivisual filters
filterStrings := strings.Split(filterString, ";")
for _, filter := range filterStrings {
// Skip empty filters
if len(filter) == 0 {
continue
}
// Break apart the filter by =. If there is an equal sign, it's an = filter
if parts := strings.Split(filter, "="); len(parts) == 2 {
// We gotta check if the column exists. If it does, we can refer to it as col
if col, ok := colsToInt[parts[0]]; ok {
filters = append(filters, func(row []string) bool {
return row[col] == parts[1]
})
} else {
// No col -> fail to parse all of the filters
// IDEA: have some character that can ignore faulty columns
return filters, errors.New("Couldn't find column " + parts[0])
}
} else {
return filters, errors.New("Couldn't parse filter " + filter)
}
}
return filters, nil
}
func genColumns(columnFlag string, cols []string) ([]bool, error) {
// Create a map of the columns, just like in genFilters
colsToInt := make(map[string]int)
for i, col := range cols {
colsToInt[col] = i
colNum := fmt.Sprintf("_%d", i+1)
colsToInt[colNum] = i
}
// Each column has a bool assocaited with it, if it should show or not
enabled := make([]bool, len(cols))
// If nothing specified, we just show all the columns (each bool in the array = true)
if len(columnFlag) == 0 {
for i := range enabled {
enabled[i] = true
}
} else {
// Otherwise we split up the flag by ; and set the bools for the columns in that list
shownCols := strings.Split(columnFlag, ";")
for _, j := range shownCols {
if col, ok := colsToInt[j]; ok {
enabled[col] = true
} else {
// If we can't find the column we error out
return enabled, errors.New("Couldn't find column " + j)
}
}
}
return enabled, nil
}