mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
feat(path): add template
This commit is contained in:
parent
a2e36f6ec0
commit
7805ee1a27
|
@ -37,8 +37,9 @@ Display the current path.
|
|||
- 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`
|
||||
- max_depth: `number` - maximum path depth to display before shortening when using `Agnoster Short` - defaults to `1`
|
||||
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
|
||||
properties below. Defaults to `{{ .Path }}`
|
||||
|
||||
## Mapped Locations
|
||||
|
||||
|
@ -123,3 +124,12 @@ starts with a symbol or icon.
|
|||
- `.config` will be shortened to `.c`
|
||||
- `__pycache__` will be shortened to `__p`
|
||||
- `➼ folder` will be shortened to `➼ f`
|
||||
|
||||
## Template Properties
|
||||
|
||||
- `.PWD`: `string` - the current directory (non styled)
|
||||
- `.Path`: `string` - the current directory (styled)
|
||||
- `.StackCount`: `int` - the stack count
|
||||
|
||||
[go-text-template]: https://golang.org/pkg/text/template/
|
||||
[sprig]: https://masterminds.github.io/sprig/
|
||||
|
|
|
@ -10,6 +10,10 @@ import (
|
|||
type path struct {
|
||||
props properties
|
||||
env environmentInfo
|
||||
|
||||
PWD string
|
||||
Path string
|
||||
StackCount int
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -45,8 +49,6 @@ const (
|
|||
MappedLocations Property = "mapped_locations"
|
||||
// MappedLocationsEnabled enables overriding certain locations with an icon
|
||||
MappedLocationsEnabled Property = "mapped_locations_enabled"
|
||||
// StackCountEnabled enables the stack count display
|
||||
StackCountEnabled Property = "stack_count_enabled"
|
||||
// MaxDepth Maximum path depth to display whithout shortening
|
||||
MaxDepth Property = "max_depth"
|
||||
)
|
||||
|
@ -56,45 +58,51 @@ func (pt *path) enabled() bool {
|
|||
}
|
||||
|
||||
func (pt *path) string() string {
|
||||
cwd := pt.env.getcwd()
|
||||
var formattedPath string
|
||||
pt.PWD = pt.env.getcwd()
|
||||
switch style := pt.props.getString(Style, Agnoster); style {
|
||||
case Agnoster:
|
||||
formattedPath = pt.getAgnosterPath()
|
||||
pt.Path = pt.getAgnosterPath()
|
||||
case AgnosterFull:
|
||||
formattedPath = pt.getAgnosterFullPath()
|
||||
pt.Path = pt.getAgnosterFullPath()
|
||||
case AgnosterShort:
|
||||
formattedPath = pt.getAgnosterShortPath()
|
||||
pt.Path = pt.getAgnosterShortPath()
|
||||
case Mixed:
|
||||
formattedPath = pt.getMixedPath()
|
||||
pt.Path = pt.getMixedPath()
|
||||
case Letter:
|
||||
formattedPath = pt.getLetterPath()
|
||||
pt.Path = pt.getLetterPath()
|
||||
case AgnosterLeft:
|
||||
formattedPath = pt.getAgnosterLeftPath()
|
||||
pt.Path = pt.getAgnosterLeftPath()
|
||||
case Short:
|
||||
// "short" is a duplicate of "full", just here for backwards compatibility
|
||||
fallthrough
|
||||
case Full:
|
||||
formattedPath = pt.getFullPath()
|
||||
pt.Path = pt.getFullPath()
|
||||
case Folder:
|
||||
formattedPath = pt.getFolderPath()
|
||||
pt.Path = pt.getFolderPath()
|
||||
default:
|
||||
return fmt.Sprintf("Path style: %s is not available", style)
|
||||
}
|
||||
formattedPath = pt.formatWindowsDrive(formattedPath)
|
||||
pt.Path = pt.formatWindowsDrive(pt.Path)
|
||||
if pt.props.getBool(EnableHyperlink, false) {
|
||||
// wsl check
|
||||
if pt.env.isWsl() {
|
||||
cwd, _ = pt.env.runCommand("wslpath", "-m", cwd)
|
||||
pt.PWD, _ = pt.env.runCommand("wslpath", "-m", pt.PWD)
|
||||
}
|
||||
return fmt.Sprintf("[%s](file://%s)", formattedPath, cwd)
|
||||
pt.Path = fmt.Sprintf("[%s](file://%s)", pt.Path, pt.PWD)
|
||||
}
|
||||
|
||||
if pt.props.getBool(StackCountEnabled, false) && pt.env.stackCount() > 0 {
|
||||
return fmt.Sprintf("%d %s", pt.env.stackCount(), formattedPath)
|
||||
pt.StackCount = pt.env.stackCount()
|
||||
segmentTemplate := pt.props.getString(SegmentTemplate, "{{ .Path }}")
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: pt,
|
||||
Env: pt.env,
|
||||
}
|
||||
|
||||
return formattedPath
|
||||
text, err := template.render()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (pt *path) formatWindowsDrive(pwd string) string {
|
||||
|
|
|
@ -387,6 +387,7 @@ func TestAgnosterPathStyles(t *testing.T) {
|
|||
env.On("homeDir", nil).Return(tc.HomePath)
|
||||
env.On("getcwd", nil).Return(tc.Pwd)
|
||||
env.On("getRuntimeGOOS", nil).Return(tc.GOOS)
|
||||
env.On("stackCount", nil).Return(0)
|
||||
args := &args{
|
||||
PSWD: &tc.Pswd,
|
||||
}
|
||||
|
@ -397,6 +398,7 @@ func TestAgnosterPathStyles(t *testing.T) {
|
|||
FolderSeparatorIcon: tc.FolderSeparatorIcon,
|
||||
Style: tc.Style,
|
||||
MaxDepth: tc.MaxDepth,
|
||||
SegmentTemplate: "{{ .Path }}",
|
||||
},
|
||||
}
|
||||
got := path.string()
|
||||
|
@ -415,8 +417,10 @@ func TestGetFullPath(t *testing.T) {
|
|||
GOOS string
|
||||
PathSeparator string
|
||||
StackCount int
|
||||
StackCountEnabled bool
|
||||
Template string
|
||||
}{
|
||||
{Style: Full, Pwd: "/usr/home/abc", Template: "{{ .Path }}", StackCount: 2, Expected: "~/abc"},
|
||||
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", Expected: "/"},
|
||||
{Style: Full, Pwd: "", Expected: ""},
|
||||
{Style: Full, Pwd: "/", Expected: "/"},
|
||||
|
@ -449,49 +453,49 @@ func TestGetFullPath(t *testing.T) {
|
|||
{Style: Full, FolderSeparatorIcon: "\\", Pwd: "C:\\Users\\Jan", Expected: "C:\\Users\\Jan", PathSeparator: "\\", GOOS: windowsPlatform},
|
||||
|
||||
// StackCountEnabled=true and StackCount=2
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCountEnabled: true, StackCount: 2, Expected: "2 /"},
|
||||
{Style: Full, Pwd: "", StackCountEnabled: true, StackCount: 2, Expected: "2 "},
|
||||
{Style: Full, Pwd: "/", StackCountEnabled: true, StackCount: 2, Expected: "2 /"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCountEnabled: true, StackCount: 2, Expected: "2 ~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, StackCount: 2, Expected: "2 ~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, StackCount: 2, Expected: "2 /usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCountEnabled: true, StackCount: 2, Expected: "2 /a/b/c/d"},
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCount: 2, Expected: "2 /"},
|
||||
{Style: Full, Pwd: "", StackCount: 2, Expected: "2 "},
|
||||
{Style: Full, Pwd: "/", StackCount: 2, Expected: "2 /"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCount: 2, Expected: "2 ~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCount: 2, Expected: "2 ~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCount: 2, Expected: "2 /usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCount: 2, Expected: "2 /a/b/c/d"},
|
||||
|
||||
// StackCountEnabled=false and StackCount=2
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCountEnabled: false, StackCount: 2, Expected: "/"},
|
||||
{Style: Full, Pwd: "", StackCountEnabled: false, StackCount: 2, Expected: ""},
|
||||
{Style: Full, Pwd: "/", StackCountEnabled: false, StackCount: 2, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCountEnabled: false, StackCount: 2, Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: false, StackCount: 2, Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: false, StackCount: 2, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCountEnabled: false, StackCount: 2, Expected: "/a/b/c/d"},
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", Template: "{{ .Path }}", StackCount: 2, Expected: "/"},
|
||||
{Style: Full, Pwd: "", Template: "{{ .Path }}", StackCount: 2, Expected: ""},
|
||||
{Style: Full, Pwd: "/", Template: "{{ .Path }}", StackCount: 2, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", Template: "{{ .Path }}", StackCount: 2, Expected: "~"},
|
||||
|
||||
{Style: Full, Pwd: "/usr/home/abc", Template: "{{ .Path }}", StackCount: 2, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", Template: "{{ .Path }}", StackCount: 2, Expected: "/a/b/c/d"},
|
||||
|
||||
// StackCountEnabled=true and StackCount=0
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCountEnabled: true, StackCount: 0, Expected: "/"},
|
||||
{Style: Full, Pwd: "", StackCountEnabled: true, StackCount: 0, Expected: ""},
|
||||
{Style: Full, Pwd: "/", StackCountEnabled: true, StackCount: 0, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCountEnabled: true, StackCount: 0, Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, StackCount: 0, Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, StackCount: 0, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCountEnabled: true, StackCount: 0, Expected: "/a/b/c/d"},
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCount: 0, Expected: "/"},
|
||||
{Style: Full, Pwd: "", StackCount: 0, Expected: ""},
|
||||
{Style: Full, Pwd: "/", StackCount: 0, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCount: 0, Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCount: 0, Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCount: 0, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCount: 0, Expected: "/a/b/c/d"},
|
||||
|
||||
// StackCountEnabled=true and StackCount<0
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCountEnabled: true, StackCount: -1, Expected: "/"},
|
||||
{Style: Full, Pwd: "", StackCountEnabled: true, StackCount: -1, Expected: ""},
|
||||
{Style: Full, Pwd: "/", StackCountEnabled: true, StackCount: -1, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCountEnabled: true, StackCount: -1, Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, StackCount: -1, Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, StackCount: -1, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCountEnabled: true, StackCount: -1, Expected: "/a/b/c/d"},
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCount: -1, Expected: "/"},
|
||||
{Style: Full, Pwd: "", StackCount: -1, Expected: ""},
|
||||
{Style: Full, Pwd: "/", StackCount: -1, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCount: -1, Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCount: -1, Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCount: -1, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCount: -1, Expected: "/a/b/c/d"},
|
||||
|
||||
// StackCountEnabled=true and StackCount not set
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", StackCountEnabled: true, Expected: "/"},
|
||||
{Style: Full, Pwd: "", StackCountEnabled: true, Expected: ""},
|
||||
{Style: Full, Pwd: "/", StackCountEnabled: true, Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", StackCountEnabled: true, Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", StackCountEnabled: true, Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", StackCountEnabled: true, Expected: "/a/b/c/d"},
|
||||
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", Expected: "/"},
|
||||
{Style: Full, Pwd: "", Expected: ""},
|
||||
{Style: Full, Pwd: "/", Expected: "/"},
|
||||
{Style: Full, Pwd: "/usr/home", Expected: "~"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", Expected: "~/abc"},
|
||||
{Style: Full, Pwd: "/usr/home/abc", Expected: "/usr/home/abc", DisableMappedLocations: true},
|
||||
{Style: Full, Pwd: "/a/b/c/d", Expected: "/a/b/c/d"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
@ -508,9 +512,12 @@ func TestGetFullPath(t *testing.T) {
|
|||
PSWD: &tc.Pswd,
|
||||
}
|
||||
env.On("getArgs", nil).Return(args)
|
||||
if len(tc.Template) == 0 {
|
||||
tc.Template = "{{ if gt .StackCount 0 }}{{ .StackCount }} {{ end }}{{ .Path }}"
|
||||
}
|
||||
var props properties = map[Property]interface{}{
|
||||
Style: tc.Style,
|
||||
StackCountEnabled: tc.StackCountEnabled,
|
||||
Style: tc.Style,
|
||||
SegmentTemplate: tc.Template,
|
||||
}
|
||||
if tc.FolderSeparatorIcon != "" {
|
||||
props[FolderSeparatorIcon] = tc.FolderSeparatorIcon
|
||||
|
|
|
@ -1219,22 +1219,19 @@
|
|||
"title": "Maximum Depth",
|
||||
"description": "Maximum path depth to display whithout shortening",
|
||||
"default": 1
|
||||
}
|
||||
},
|
||||
"mapped_locations_enabled": {
|
||||
"type": "boolean",
|
||||
"title": "Enable the Mapped Locations feature",
|
||||
"description": "Replace known locations in the path with the replacements before applying the style.",
|
||||
"default": true
|
||||
},
|
||||
"stack_count_enabled": {
|
||||
"type": "boolean",
|
||||
"title": "Show/hide stack count",
|
||||
"description": "Displays the stack count when using pushd/popd",
|
||||
"default": false
|
||||
},
|
||||
"enable_hyperlink": {
|
||||
"$ref": "#/definitions/enable_hyperlink"
|
||||
},
|
||||
"mapped_locations_enabled": {
|
||||
"type": "boolean",
|
||||
"title": "Enable the Mapped Locations feature",
|
||||
"description": "Replace known locations in the path with the replacements before applying the style.",
|
||||
"default": true
|
||||
},
|
||||
"enable_hyperlink": {
|
||||
"$ref": "#/definitions/enable_hyperlink"
|
||||
},
|
||||
"template": {
|
||||
"$ref": "#/definitions/template"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue