forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
properties.go
150 lines (134 loc) · 3.25 KB
/
properties.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
)
// Property defines one property of a segment for context
type Property string
// general Properties used across Segments
const (
// Style indicates with style to use
Style Property = "style"
// Prefix adds a text prefix to the segment
Prefix Property = "prefix"
// Postfix adds a text postfix to the segment
Postfix Property = "postfix"
// ColorBackground color the background or foreground when a specific color is set
ColorBackground Property = "color_background"
// IgnoreFolders folders to ignore and not run the segment logic
IgnoreFolders Property = "ignore_folders"
// DisplayVersion show the version number or not
DisplayVersion Property = "display_version"
)
type properties struct {
values map[Property]interface{}
foreground string
background string
}
func (p *properties) getString(property Property, defaultValue string) string {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}
return parseString(val, defaultValue)
}
func parseString(value interface{}, defaultValue string) string {
stringValue, ok := value.(string)
if !ok {
return defaultValue
}
return stringValue
}
func (p *properties) getColor(property Property, defaultValue string) string {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}
colorString := parseString(val, defaultValue)
_, err := getColorFromName(colorString, false)
if err == nil {
return colorString
}
values := findNamedRegexMatch(`(?P<color>#[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})`, colorString)
if values != nil && values["color"] != "" {
return values["color"]
}
return defaultValue
}
func (p *properties) getBool(property Property, defaultValue bool) bool {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}
boolValue, ok := val.(bool)
if !ok {
return defaultValue
}
return boolValue
}
func (p *properties) getFloat64(property Property, defaultValue float64) float64 {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}
floatValue, ok := val.(float64)
if !ok {
return defaultValue
}
return floatValue
}
func (p *properties) getKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}
keyValues := parseKeyValueArray(val)
return keyValues
}
func parseStringArray(param interface{}) []string {
switch v := param.(type) {
default:
return []string{}
case []interface{}:
list := make([]string, len(v))
for i, v := range v {
list[i] = fmt.Sprint(v)
}
return list
case []string:
return v
}
}
func parseKeyValueArray(param interface{}) map[string]string {
switch v := param.(type) {
default:
return map[string]string{}
case []interface{}:
keyValueArray := make(map[string]string)
for _, s := range v {
l := parseStringArray(s)
if len(l) == 2 {
key := l[0]
val := l[1]
keyValueArray[key] = val
}
}
return keyValueArray
case map[string]string:
return v
}
}