feat(properties): log properties information

This commit is contained in:
Jan De Dobbeleer 2023-04-05 20:38:05 +02:00 committed by Jan De Dobbeleer
parent b4b3698ae6
commit 1f59e3d420
4 changed files with 244 additions and 186 deletions

View file

@ -405,7 +405,11 @@ func (segment *Segment) mapSegmentWithWriter(env platform.Environment) error {
if f, ok := Segments[segment.Type]; ok {
writer := f()
writer.Init(segment.Properties, env)
wrapper := &properties.Wrapper{
Properties: segment.Properties,
Env: env,
}
writer.Init(wrapper, env)
segment.writer = writer
return nil
}

207
src/properties/map.go Normal file
View file

@ -0,0 +1,207 @@
package properties
import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
)
type Properties interface {
GetColor(property Property, defaultColor string) string
GetBool(property Property, defaultValue bool) bool
GetString(property Property, defaultValue string) string
GetFloat64(property Property, defaultValue float64) float64
GetInt(property Property, defaultValue int) int
GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string
GetStringArray(property Property, defaultValue []string) []string
}
// Property defines one property of a segment for context
type Property string
// general Properties used across Segments
const (
// Style indicates the style to use
Style Property = "style"
// IncludeFolders indicates folders to be included for the segment logic
IncludeFolders Property = "include_folders"
// ExcludeFolders indicates folders to be excluded for the segment logic
ExcludeFolders Property = "exclude_folders"
// IgnoreFolders is a duplicate of ExcludeFolders
IgnoreFolders Property = "ignore_folders"
// FetchVersion decides whether to fetch the version number or not
FetchVersion Property = "fetch_version"
// AlwaysEnabled decides whether or not to always display the info
AlwaysEnabled Property = "always_enabled"
// VersionURLTemplate is the template to use when building language segment hyperlink
VersionURLTemplate Property = "version_url_template"
// DisplayError decides whether to display when an error occurs or not
DisplayError Property = "display_error"
// DisplayDefault hides or shows the default
DisplayDefault Property = "display_default"
// AccessToken is the access token to use for an API
AccessToken Property = "access_token"
// RefreshToken is the refresh token to use for an API
RefreshToken Property = "refresh_token"
// HTTPTimeout timeout used when executing http request
HTTPTimeout Property = "http_timeout"
// DefaultHTTPTimeout default timeout used when executing http request
DefaultHTTPTimeout = 20
// DefaultCacheTimeout default timeout used when caching data
DefaultCacheTimeout = 10
// CacheTimeout cache timeout
CacheTimeout Property = "cache_timeout"
)
type Map map[Property]interface{}
func (m Map) GetString(property Property, defaultValue string) string {
val, found := m[property]
if !found {
return defaultValue
}
return fmt.Sprint(val)
}
func (m Map) GetColor(property Property, defaultValue string) string {
val, found := m[property]
if !found {
return defaultValue
}
colorString := fmt.Sprint(val)
if ansi.IsAnsiColorName(colorString) {
return colorString
}
values := regex.FindNamedRegexMatch(`(?P<color>#[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|p:.*)`, colorString)
if values != nil && values["color"] != "" {
return values["color"]
}
return defaultValue
}
func (m Map) GetBool(property Property, defaultValue bool) bool {
val, found := m[property]
if !found {
return defaultValue
}
boolValue, ok := val.(bool)
if !ok {
return defaultValue
}
return boolValue
}
func (m Map) GetFloat64(property Property, defaultValue float64) float64 {
val, found := m[property]
if !found {
return defaultValue
}
switch v := val.(type) {
case int:
return float64(v)
case int64:
return float64(v)
case uint64:
return float64(v)
case float64:
return v
default:
return defaultValue
}
}
func (m Map) GetInt(property Property, defaultValue int) int {
val, found := m[property]
if !found {
return defaultValue
}
switch v := val.(type) {
case int:
return v
case int64:
return int(v)
case float64:
intValue, ok := val.(float64)
if !ok {
return defaultValue
}
return int(intValue)
default:
return defaultValue
}
}
func (m Map) GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
val, found := m[property]
if !found {
return defaultValue
}
keyValues := parseKeyValueArray(val)
return keyValues
}
func (m Map) GetStringArray(property Property, defaultValue []string) []string {
val, found := m[property]
if !found {
return defaultValue
}
keyValues := ParseStringArray(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 map[interface{}]interface{}:
keyValueArray := make(map[string]string)
for key, value := range v {
val := value.(string)
keyString := fmt.Sprintf("%v", key)
keyValueArray[keyString] = val
}
return keyValueArray
case map[string]interface{}:
keyValueArray := make(map[string]string)
for key, value := range v {
val := value.(string)
keyValueArray[key] = val
}
return keyValueArray
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
}
}

View file

@ -3,205 +3,52 @@ package properties
import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
type Properties interface {
GetColor(property Property, defaultColor string) string
GetBool(property Property, defaultValue bool) bool
GetString(property Property, defaultValue string) string
GetFloat64(property Property, defaultValue float64) float64
GetInt(property Property, defaultValue int) int
GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string
GetStringArray(property Property, defaultValue []string) []string
type Wrapper struct {
Properties Map
Env platform.Environment
}
// Property defines one property of a segment for context
type Property string
// general Properties used across Segments
const (
// Style indicates the style to use
Style Property = "style"
// IncludeFolders indicates folders to be included for the segment logic
IncludeFolders Property = "include_folders"
// ExcludeFolders indicates folders to be excluded for the segment logic
ExcludeFolders Property = "exclude_folders"
// IgnoreFolders is a duplicate of ExcludeFolders
IgnoreFolders Property = "ignore_folders"
// FetchVersion decides whether to fetch the version number or not
FetchVersion Property = "fetch_version"
// AlwaysEnabled decides whether or not to always display the info
AlwaysEnabled Property = "always_enabled"
// VersionURLTemplate is the template to use when building language segment hyperlink
VersionURLTemplate Property = "version_url_template"
// DisplayError decides whether to display when an error occurs or not
DisplayError Property = "display_error"
// DisplayDefault hides or shows the default
DisplayDefault Property = "display_default"
// AccessToken is the access token to use for an API
AccessToken Property = "access_token"
// RefreshToken is the refresh token to use for an API
RefreshToken Property = "refresh_token"
// HTTPTimeout timeout used when executing http request
HTTPTimeout Property = "http_timeout"
// DefaultHTTPTimeout default timeout used when executing http request
DefaultHTTPTimeout = 20
// DefaultCacheTimeout default timeout used when caching data
DefaultCacheTimeout = 10
// CacheTimeout cache timeout
CacheTimeout Property = "cache_timeout"
)
type Map map[Property]interface{}
func (m Map) GetString(property Property, defaultValue string) string {
val, found := m[property]
if !found {
return defaultValue
}
return fmt.Sprint(val)
func (w *Wrapper) GetColor(property Property, defaultColor string) string {
value := w.Properties.GetColor(property, defaultColor)
w.Env.Debug(fmt.Sprintf("%s: %s", property, value))
return value
}
func (m Map) GetColor(property Property, defaultValue string) string {
val, found := m[property]
if !found {
return defaultValue
}
colorString := fmt.Sprint(val)
if ansi.IsAnsiColorName(colorString) {
return colorString
}
values := regex.FindNamedRegexMatch(`(?P<color>#[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|p:.*)`, colorString)
if values != nil && values["color"] != "" {
return values["color"]
}
return defaultValue
func (w *Wrapper) GetBool(property Property, defaultValue bool) bool {
value := w.Properties.GetBool(property, defaultValue)
w.Env.Debug(fmt.Sprintf("%s: %t", property, value))
return value
}
func (m Map) GetBool(property Property, defaultValue bool) bool {
val, found := m[property]
if !found {
return defaultValue
}
boolValue, ok := val.(bool)
if !ok {
return defaultValue
}
return boolValue
func (w *Wrapper) GetString(property Property, defaultValue string) string {
value := w.Properties.GetString(property, defaultValue)
w.Env.Debug(value)
return value
}
func (m Map) GetFloat64(property Property, defaultValue float64) float64 {
val, found := m[property]
if !found {
return defaultValue
}
switch v := val.(type) {
case int:
return float64(v)
case int64:
return float64(v)
case uint64:
return float64(v)
case float64:
return v
default:
return defaultValue
}
func (w *Wrapper) GetFloat64(property Property, defaultValue float64) float64 {
value := w.Properties.GetFloat64(property, defaultValue)
w.Env.Debug(fmt.Sprintf("%s: %f", property, value))
return value
}
func (m Map) GetInt(property Property, defaultValue int) int {
val, found := m[property]
if !found {
return defaultValue
}
switch v := val.(type) {
case int:
return v
case int64:
return int(v)
case float64:
intValue, ok := val.(float64)
if !ok {
return defaultValue
}
return int(intValue)
default:
return defaultValue
}
func (w *Wrapper) GetInt(property Property, defaultValue int) int {
value := w.Properties.GetInt(property, defaultValue)
w.Env.Debug(fmt.Sprintf("%s: %d", property, value))
return value
}
func (m Map) GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
val, found := m[property]
if !found {
return defaultValue
}
keyValues := parseKeyValueArray(val)
return keyValues
func (w *Wrapper) GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
value := w.Properties.GetKeyValueMap(property, defaultValue)
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
return value
}
func (m Map) GetStringArray(property Property, defaultValue []string) []string {
val, found := m[property]
if !found {
return defaultValue
}
keyValues := ParseStringArray(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 map[interface{}]interface{}:
keyValueArray := make(map[string]string)
for key, value := range v {
val := value.(string)
keyString := fmt.Sprintf("%v", key)
keyValueArray[keyString] = val
}
return keyValueArray
case map[string]interface{}:
keyValueArray := make(map[string]string)
for key, value := range v {
val := value.(string)
keyValueArray[key] = val
}
return keyValueArray
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
}
func (w *Wrapper) GetStringArray(property Property, defaultValue []string) []string {
value := w.Properties.GetStringArray(property, defaultValue)
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
return value
}