Skip to content

Commit

Permalink
feat: better mapped locations layout
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed May 2, 2021
1 parent 29ddd2b commit 248afec
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
30 changes: 23 additions & 7 deletions docs/docs/segment-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Display the current path.
"background": "#61AFEF",
"properties": {
"style": "folder",
"mapped_locations": [
["C:\\temp", "\ue799"]
]
"mapped_locations": {
"C:\\temp": "\ue799"
}
}
}
```
Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions src/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions src/segment_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"])
}
}

0 comments on commit 248afec

Please sign in to comment.