2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
2020-10-02 07:58:25 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2019-03-13 04:14:30 -07:00
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// Property defines one property of a segment for context
|
2019-03-13 04:14:30 -07:00
|
|
|
type Property string
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// general Properties used across Segments
|
2019-03-13 04:14:30 -07:00
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// Style indicates with style to use
|
2019-03-13 04:14:30 -07:00
|
|
|
Style Property = "style"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Prefix adds a text prefix to the segment
|
2019-03-13 04:14:30 -07:00
|
|
|
Prefix Property = "prefix"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Postfix adds a text postfix to the segment
|
2019-03-13 04:14:30 -07:00
|
|
|
Postfix Property = "postfix"
|
2020-11-12 00:43:32 -08:00
|
|
|
// ColorBackground color the background or foreground when a specific color is set
|
2019-03-13 04:14:30 -07:00
|
|
|
ColorBackground Property = "color_background"
|
2020-11-12 00:43:32 -08:00
|
|
|
// IgnoreFolders folders to ignore and not run the segment logic
|
2020-10-02 07:58:25 -07:00
|
|
|
IgnoreFolders Property = "ignore_folders"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DisplayVersion show the version number or not
|
2020-10-16 08:43:02 -07:00
|
|
|
DisplayVersion Property = "display_version"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2020-11-15 05:32:07 -08:00
|
|
|
_, err := getColorFromName(colorString, false)
|
|
|
|
if err == nil {
|
|
|
|
return colorString
|
|
|
|
}
|
2020-11-22 06:02:27 -08:00
|
|
|
values := findNamedRegexMatch(`(?P<color>#[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})`, colorString)
|
2020-11-15 05:32:07 -08:00
|
|
|
if values != nil && values["color"] != "" {
|
|
|
|
return values["color"]
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-10-02 07:58:25 -07:00
|
|
|
|
2020-12-06 13:03:40 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:30:29 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-06 10:34:42 -08:00
|
|
|
func parseStringArray(param interface{}) []string {
|
|
|
|
switch v := param.(type) {
|
|
|
|
default:
|
2020-10-02 07:58:25 -07:00
|
|
|
return []string{}
|
2020-12-06 10:34:42 -08:00
|
|
|
case []interface{}:
|
|
|
|
list := make([]string, len(v))
|
|
|
|
for i, v := range v {
|
|
|
|
list[i] = fmt.Sprint(v)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
case []string:
|
|
|
|
return v
|
2020-10-02 07:58:25 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-19 11:30:29 -08:00
|
|
|
|
2020-12-06 10:34:42 -08:00
|
|
|
func parseKeyValueArray(param interface{}) map[string]string {
|
|
|
|
switch v := param.(type) {
|
|
|
|
default:
|
2020-11-19 11:30:29 -08:00
|
|
|
return map[string]string{}
|
2020-12-06 10:34:42 -08:00
|
|
|
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
|
|
|
|
}
|
2020-11-19 11:30:29 -08:00
|
|
|
}
|
2020-12-06 10:34:42 -08:00
|
|
|
return keyValueArray
|
|
|
|
case map[string]string:
|
|
|
|
return v
|
2020-11-19 11:30:29 -08:00
|
|
|
}
|
|
|
|
}
|