From 248afec9fa9aca9dd64e719da1cdcdf402853bee Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 2 May 2021 14:32:48 +0200 Subject: [PATCH] feat: better mapped locations layout --- docs/docs/segment-path.md | 30 +++++++++++++++++++++++------- src/properties.go | 7 +++++++ src/segment_path_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/docs/docs/segment-path.md b/docs/docs/segment-path.md index fd8969eeeb0e..749b8c4c9f05 100644 --- a/docs/docs/segment-path.md +++ b/docs/docs/segment-path.md @@ -19,9 +19,9 @@ Display the current path. "background": "#61AFEF", "properties": { "style": "folder", - "mapped_locations": [ - ["C:\\temp", "\ue799"] - ] + "mapped_locations": { + "C:\\temp": "\ue799" + } } } ``` @@ -33,15 +33,31 @@ Display the current path. - folder_icon: `string` - the icon to use as a folder indication - defaults to `..` - windows_registry_icon: `string` - the icon to display when in the Windows registry - defaults to `\uE0B1` - style: `enum` - how to display the current path -- mapped_locations: `map[string]string` - custom glyph/text for specific paths (only when `mapped_locations_enabled` -is set to `true`) -- mapped_locations_enabled: `boolean` - replace known locations in the path with the replacements before applying the -style. defaults to `true` + - enable_hyperlink: `boolean` - displays an hyperlink for the path - defaults to `false` - mixed_threshold: `number` - the maximum length of a path segment that will be displayed when using `Mixed` - defaults to `4` - stack_count_enabled: `boolean` - displays the stack count when using pushd/popd - defaults to `false` +## Mapped Locations + +Allows you to override a location with an icon. It validates if the current path **starts with** the value and replaces +it with the icon if there's a match. To avoid issues with nested overrides, Oh my posh will sort the list of mapped +locations before doing a replacement. + +- mapped_locations_enabled: `boolean` - replace known locations in the path with the replacements before applying the +style. defaults to `true` +- mapped_locations: `map[string]string` - custom glyph/text for specific paths (only when `mapped_locations_enabled` +is set to `true`) + +For example, to swap out `C:\Users\Leet\GitHub` with a GitHub icon, you can do the following: + +```json +"mapped_locations": { + "C:\\Users\\Leet\\GitHub": "\uF09B" +} +``` + ## Style Style sets the way the path is displayed. Based on previous experience and popular themes, there are 5 flavors. diff --git a/src/properties.go b/src/properties.go index 44f7cf772824..50c45eda2f9e 100644 --- a/src/properties.go +++ b/src/properties.go @@ -145,6 +145,13 @@ func parseKeyValueArray(param interface{}) map[string]string { switch v := param.(type) { default: return map[string]string{} + 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 { diff --git a/src/segment_path_test.go b/src/segment_path_test.go index f5aa03856e6c..d81ea2f8144e 100644 --- a/src/segment_path_test.go +++ b/src/segment_path_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/distatus/battery" + "github.com/gookit/config/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -591,3 +592,29 @@ func TestGetPwd(t *testing.T) { assert.Equal(t, tc.Expected, got) } } + +func TestParseMappedLocations(t *testing.T) { + cases := []struct { + Case string + JSON string + }{ + {Case: "new format", JSON: `{ "properties": { "mapped_locations": {"folder1": "one","folder2": "two"} } }`}, + {Case: "old format", JSON: `{ "properties": { "mapped_locations": [["folder1", "one"], ["folder2", "two"]] } }`}, + } + for _, tc := range cases { + config.ClearAll() + config.WithOptions(func(opt *config.Options) { + opt.TagName = "config" + }) + err := config.LoadStrings(config.JSON, tc.JSON) + assert.NoError(t, err) + var segment Segment + err = config.BindStruct("", &segment) + assert.NoError(t, err) + props := &properties{ + values: segment.Properties, + } + mappedLocations := props.getKeyValueMap(MappedLocations, make(map[string]string)) + assert.Equal(t, "two", mappedLocations["folder2"]) + } +}