-
Notifications
You must be signed in to change notification settings - Fork 14
/
variables.go
54 lines (49 loc) · 1.23 KB
/
variables.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
package myqlib
import (
"fmt"
"regexp"
)
// Given a variable list (potentially with regexes) and a sample, expand the variables to all possible matches
func expand_variables(variables []string, sample MyqSample) (expanded []string) {
hash := map[string]int{}
for _, variable := range variables {
re, err := regexp.Compile(variable)
if err != nil {
// Just pass it through as-is
hash[variable] = 1
} else {
// Got a regex, loop through all keys to try to find matches
for key, _ := range sample {
if re.MatchString(key) {
hash[key] = 1
}
}
}
}
for key, _ := range hash {
expanded = append(expanded, key)
}
return
}
// Fit a given string into a width
func fit_string(val string, width int64) string {
if len(val) > int(width) {
return val[0:width] // First width characters
} else {
return fmt.Sprintf(fmt.Sprint(`%`, width, `s`), val)
}
}
// Fit a given string into a width
func right_fit_string(val string, width int64) string {
if len(val) > int(width) {
return val[len(val)-int(width):]
} else {
return fmt.Sprintf(fmt.Sprint(`%`, width, `s`), val)
}
}
func column_filler(c Col) string {
return fit_string("-", c.Width())
}
func column_blank(c Col) string {
return fit_string(" ", c.Width())
}