forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
69 lines (57 loc) · 1.36 KB
/
headers.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
package table
import (
"fmt"
"strings"
"unicode"
)
const UNKNOWN_HEADER_MAPPING rune = '_'
func NewHeader(title string) Header {
return Header{
Key: KeyifyHeader(title),
Title: title,
Hidden: false,
}
}
func (t *Table) SetColumnVisibility(headers []Header) error {
for tableHeaderIdx, _ := range t.Header {
t.Header[tableHeaderIdx].Hidden = true
}
for _, header := range headers {
foundHeader := false
for tableHeaderIdx, tableHeader := range t.Header {
if tableHeader.Key == header.Key || tableHeader.Title == header.Title {
t.Header[tableHeaderIdx].Hidden = false
foundHeader = true
break
}
}
if !foundHeader {
// key may be empty; if title is present
return fmt.Errorf("Failed to find header: %s", header.Key)
}
}
return nil
}
func KeyifyHeader(header string) string {
splittedStrings := strings.Split(cleanHeader(header), " ")
splittedTrimmedStrings := []string{}
for _, s := range splittedStrings {
if s != "" {
splittedTrimmedStrings = append(splittedTrimmedStrings, s)
}
}
join := strings.Join(splittedTrimmedStrings, "_")
if len(join) == 0 {
return string(UNKNOWN_HEADER_MAPPING)
}
return join
}
func cleanHeader(header string) string {
return strings.Map(func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return unicode.ToLower(r)
} else {
return ' '
}
}, header)
}