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