feat(template): hyperlink functions

This commit is contained in:
Jan De Dobbeleer 2022-01-28 13:42:34 +01:00 committed by Jan De Dobbeleer
parent e95e0d6b1b
commit 0e056cd50d
51 changed files with 146 additions and 75 deletions

2
.vscode/launch.json vendored
View file

@ -8,7 +8,7 @@
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": [
"--config=${workspaceRoot}/themes/jandedobbeleer.omp.json",
"--config=/Users/jan/.jandedobbeleer.omp.json",
"--shell=pwsh"
]
},

View file

@ -218,4 +218,4 @@ This means that for user Bill, who has a user account `Bill` on Windows and `bil
[fg-templ]: /docs/config-overview#foreground-templates
[regex]: https://www.regular-expressions.info/tutorial.html
[aws]: /docs/aws
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -1,15 +1,15 @@
---
id: config-text
title: Text
sidebar_label: Text
id: config-templates
title: Templates
sidebar_label: Templates
---
## Templates
Every segment has a `template` property to tweak the text that is displayed.
Under the hood, this uses go's [text/template][go-text-template] feature extended with [sprig][sprig] and
offers a few standard properties to work with.
## Global properties
- `.Root`: `boolean` - is the current user root/admin or not
- `.PWD`: `string` - the current working directory
- `.Folder`: `string` - the current working folder
@ -18,6 +18,14 @@ offers a few standard properties to work with.
- `.HostName`: `string` - the host name
- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name
## Helper functions
- url: create a hyperlink to a website to open your default browser `{{ url .UpstreamIcon .UpstreamURL }}`
(needs terminal [support][terminal-list-hyperlinks])
- path: create a link to a folder to open your file explorer `{{ path .Path .Location }}`
(needs terminal [support][terminal-list-hyperlinks])
- secondsRound: round seconds to a time indication `{{ secondsRound 3600 }}` -> 1h
## Text decoration
You can make use of the following syntax to decorate text:
@ -29,26 +37,6 @@ You can make use of the following syntax to decorate text:
This can be used in templates and icons/text inside your config.
## Hyperlinks
The engine has the ability to render hyperlinks. Your terminal has to support it and the option
has to be enabled at the segment level. Hyperlink generation is disabled by default.
When using in a template, the syntax is like markdown:
- url: `[text](https://link)`
- file: `[file](file://path)`
### Supported segments
- [Path][path-segment]
- [Git][git-segment]
- Most languages (version hyperlink)
### Supported terminals
- [Terminal list][terminal-list-hyperlinks]
[terminal-list-hyperlinks]: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
[path-segment]: /docs/path
[git-segment]: /docs/git

View file

@ -51,4 +51,4 @@ To learn more about templates and their possibilities, have a look at the [templ
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -40,4 +40,4 @@ Display the currently active Angular CLI version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -34,4 +34,4 @@ to `true`
- `.Profile`: `string` - the currently active profile
- `.Region`: `string` - the currently active region
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -36,4 +36,4 @@ Display the currently active Azure subscription information.
- `.UserName`: `string` - the user name
- `.Origin`: `string` - where we received the information from, can be `CLI` or `PWSH`
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -44,4 +44,4 @@ Display the currently active Azure functions CLI version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -58,4 +58,4 @@ the same value as `Voltage`, for convenience
[colors]: /docs/config-colors
[battery]: https://github.com/distatus/battery/blob/master/battery.go#L78
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -164,6 +164,6 @@ To display gravity as SG in XXXX format (e.g. "1020" instead of "1.020"), use th
}
````
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[brewfather]: http://brewfather.app
[wikipedia_gravity_page]:https://en.wikipedia.org/wiki/Brix#Specific_gravity_2

View file

@ -50,4 +50,4 @@ error). The `&&` functionality will join the output of the commands when success
- `.Output`: `string` - the output of the command.
[env]: /docs/environment
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -43,4 +43,4 @@ Display the currently active crystal version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -44,4 +44,4 @@ folder are present (default)
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -46,4 +46,4 @@ Display the currently active .NET SDK version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -54,4 +54,4 @@ Style specifies the format in which the time will be displayed. The table below
- `.Ms`: `number` - the execution time in milliseconds
- `.FormattedMs`: `string` - the formatted value based on the `style` above.
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -40,4 +40,4 @@ Displays the last exit code or that the last command failed based on the configu
- `.Code`: `number` - the last known exit code
- `.Text`: `string` - the textual meaning linked to exit code (if applicable, otherwise identical to `.Code`)
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -103,7 +103,7 @@ You can set the following properties to `true` to enable fetching additional inf
- `.BranchStatus`: `string` - the current branch context (ahead/behind string representation)
- `.Upstream`: `string` - the upstream name (remote)
- `.UpstreamIcon`: `string` - the upstream icon (based on the icons above)
- `.UpstreamURL`: `string` - the upstream URL for use in [hyperlinks][hyperlinks] in templates: `[{{ .UpstreamIcon }} {{ .HEAD }}]({{ .UpstreamURL }})`
- `.UpstreamURL`: `string` - the upstream URL for use in [hyperlinks][hyperlinks] in templates: `{{ url .UpstreamIcon .UpstreamURL }}`
- `.StashCount`: `int` - the stash count
- `.WorktreeCount`: `int` - the worktree count
- `.IsWorkTree`: `boolean` - if in a worktree repo or not
@ -118,5 +118,5 @@ You can set the following properties to `true` to enable fetching additional inf
- `.String`: `string` - a string representation of the changes above
[poshgit]: /docs/poshgit
[templates]: /docs/config-text#templates
[hyperlinks]: /docs/config-text#hyperlinks
[templates]: /docs/config-templates
[hyperlinks]: /docs/config-templates#helper-functions

View file

@ -44,4 +44,4 @@ Display the currently active golang version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -38,5 +38,5 @@ sidebar_label: Ipify
- .IP: `string` - Your external IP address
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[ipify]: https://www.ipify.org/

View file

@ -56,4 +56,4 @@ Display the currently active java version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -43,4 +43,4 @@ Display the currently active julia version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -44,4 +44,4 @@ It is common for the Kubernetes "default" namespace to be used when no namespace
`{{.Context}} :: {{if .Namespace}}{{.Namespace}}{{else}}default{{end}}`
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -38,4 +38,4 @@ The Nerdbank.GitVersioning CLI can be a bit slow causing the prompt to feel slow
- `.SimpleVersion`: `string` - the current simple version
[nbgv]: https://github.com/dotnet/Nerdbank.GitVersioning
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -94,5 +94,5 @@ Or display in mmol/l (instead of the default mg/dl) with the following template:
- .TrendIcon: `string` - By default, this will be something like ↑↑ or ↘ etc but you can
override them with any glpyh as seen above
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[nightscout]: http://www.nightscout.info/

View file

@ -46,4 +46,4 @@ Display the currently active node version.
- `.PackageManagerIcon`: `string` - the Yarn on NPM icon when setting `fetch_package_manager` to `true`
- `.Mismatch`: `boolean` - if the version in `.nvmrc` matches with `.Full`
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -53,4 +53,4 @@ Display OS specific info - defaults to Icon.
- `.OS`: `string` - the OS platform
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -54,4 +54,4 @@ properties below - defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})`
- `.UnitIcon`: `string` - the current unit icon(based on units property)
- `.URL`: `string` - the url of the current api call
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -126,4 +126,4 @@ starts with a symbol or icon.
- `.Path`: `string` - the current directory (styled)
- `.StackCount`: `int` - the stack count
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -43,4 +43,4 @@ Display the currently active php version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -89,5 +89,5 @@ You can set the following property to `true` to enable fetching additional infor
- `.Changed`: `boolean` - if the status contains changes or not
- `.String`: `string` - a string representation of the changes above
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[fa-issue]: https://github.com/FortAwesome/Font-Awesome/issues/18504

View file

@ -30,4 +30,4 @@ To enable the `posh-git` module, set `$env:POSH_GIT_ENABLED = $true` in your `$P
- `.Status`: `string` - the status reported from posh-git
[posh-git]: https://github.com/dahlbyk/posh-git
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -51,4 +51,4 @@ or not - defaults to `true`
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -43,4 +43,4 @@ Display the currently active ruby version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -43,4 +43,4 @@ Display the currently active rust version.
- `.BuildMetadata`: `string` - build metadata
- `.Error`: `string` - when fetching the version string errors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -31,4 +31,4 @@ Show the current user and host name.
- `.SSHSession`: `boolean` - active SSH session or not
- `.Root`: `boolean` - are you a root/admin user or not
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -33,4 +33,4 @@ Show the current shell name (ZSH, powershell, bash, ...).
- `.Name`: `string` - the shell name
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -42,4 +42,4 @@ On macOS, all states are supported (playing/paused/stopped).
- `.Track`: `string` - current track
- `.Icon`: `string` - icon (based on `.Status`)
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -91,6 +91,6 @@ The properties below are available for use in your template
Now, go out and have a fun ride or run!
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[strava]: http://www.strava.com/
[strava-connect]: https://www.strava.com/oauth/authorize?client_id=76033&response_type=code&redirect_uri=https://ohmyposh.dev/api/auth&approval_prompt=force&scope=read,activity:read

View file

@ -45,4 +45,4 @@ Display SysInfo.
- `.CPU`: `[]struct` - an array of [InfoStat][cpuinfo] object, you can use any property it has e.g. `(index .CPU 0).Cores`
[cpuinfo]: https://github.com/shirou/gopsutil/blob/78065a7ce2021f6a78c8d6f586a2683ba501dcec/cpu/cpu.go#L32
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -31,4 +31,4 @@ This requires a terraform binary in your PATH and will only show in directories
- `.WorkspaceName`: `string` - is the current workspace name
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -46,4 +46,4 @@ New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force
- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name
[coloring]: /docs/config-colors
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -65,5 +65,5 @@ Show the current timestamp.
- StampMicro = "Jan _2 15:04:05.000000"
- StampNano = "Jan _2 15:04:05.000000000"
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[format]: https://yourbasic.org/golang/format-parse-string-time-date-example/

View file

@ -51,4 +51,4 @@ works if you can curl it yourself and a result. - defaults to ``
[wt]: https://wakatime.com
[wk-summaries]: https://wakatime.com/developers#summaries
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -40,4 +40,4 @@ Currently only supports Windows and WSL. Pull requests for Darwin and Linux supp
- `.TransmitRate`: `int` - the transmit rate (Mbps)
- `.Signal`: `int` - the signal strength (%)
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -42,4 +42,4 @@ Supported registry key types:
- .Value: `string` - The result of your query, or fallback if not found.
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -45,6 +45,6 @@ Remote Control password.
- `.Track`: `string` - current track
- `.Icon`: `string` - icon (based on `.Status`)
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates
[templates]: /docs/config-text#templates
[templates]: /docs/config-templates

View file

@ -25,7 +25,7 @@ module.exports = {
"config-sample",
"config-title",
"config-colors",
"config-text",
"config-templates",
"config-transient",
"config-tooltips",
"config-fonts"

View file

@ -9,6 +9,8 @@ import (
func funcMap() template.FuncMap {
funcMap := map[string]interface{}{
"secondsRound": secondsRound,
"url": url,
"path": path,
}
for key, fun := range sprig.TxtFuncMap() {
if _, ok := funcMap[key]; !ok {

18
src/template/link.go Normal file
View file

@ -0,0 +1,18 @@
package template
import (
"fmt"
link "net/url"
)
func url(text, url string) (string, error) {
_, err := link.ParseRequestURI(url)
if err != nil {
return "", err
}
return fmt.Sprintf("[%s](%s)", text, url), nil
}
func path(text, path string) (string, error) {
return fmt.Sprintf("[%s](file:%s)", text, path), nil
}

63
src/template/link_test.go Normal file
View file

@ -0,0 +1,63 @@
package template
import (
"oh-my-posh/environment"
"oh-my-posh/mock"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUrl(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
ShouldError bool
}{
{Case: "valid url", Expected: "[link](https://ohmyposh.dev)", Template: `{{ url "link" "https://ohmyposh.dev" }}`},
{Case: "invalid url", Expected: "", Template: `{{ url "link" "Foo" }}`, ShouldError: true},
}
env := &mock.MockedEnvironment{}
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string),
})
for _, tc := range cases {
tmpl := &Text{
Template: tc.Template,
Context: nil,
Env: env,
}
text, err := tmpl.Render()
if tc.ShouldError {
assert.Error(t, err)
continue
}
assert.Equal(t, tc.Expected, text, tc.Case)
}
}
func TestPath(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
}{
{Case: "valid path", Expected: "[link](file:/test/test)", Template: `{{ path "link" "/test/test" }}`},
}
env := &mock.MockedEnvironment{}
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string),
})
for _, tc := range cases {
tmpl := &Text{
Template: tc.Template,
Context: nil,
Env: env,
}
text, _ := tmpl.Render()
assert.Equal(t, tc.Expected, text, tc.Case)
}
}

View file

@ -61,7 +61,7 @@
"template": {
"type": "string",
"title": "Template text",
"description": "https://ohmyposh.dev/docs/config-text#templates",
"description": "https://ohmyposh.dev/docs/config-templates",
"default": ""
},
"block": {