feat: better mapped locations layout

This commit is contained in:
Jan De Dobbeleer 2021-05-02 14:32:48 +02:00 committed by Jan De Dobbeleer
parent 29ddd2b819
commit 248afec9fa
3 changed files with 57 additions and 7 deletions

View file

@ -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.

View file

@ -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 {

View file

@ -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"])
}
}