From 0e056cd50d8d8d4628b6b5631b4409352dd6059a Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 28 Jan 2022 13:42:34 +0100 Subject: [PATCH] feat(template): hyperlink functions --- .vscode/launch.json | 2 +- docs/docs/config-segment.md | 2 +- .../{config-text.md => config-templates.md} | 38 ++++------- docs/docs/config-title.md | 2 +- docs/docs/segment-angular.md | 2 +- docs/docs/segment-aws.md | 2 +- docs/docs/segment-az.md | 2 +- docs/docs/segment-azfunc.md | 2 +- docs/docs/segment-battery.md | 2 +- docs/docs/segment-brewfather.md | 2 +- docs/docs/segment-command.md | 2 +- docs/docs/segment-crystal.md | 2 +- docs/docs/segment-dart.md | 2 +- docs/docs/segment-dotnet.md | 2 +- docs/docs/segment-executiontime.md | 2 +- docs/docs/segment-exit.md | 2 +- docs/docs/segment-git.mdx | 6 +- docs/docs/segment-golang.md | 2 +- docs/docs/segment-ipify.md | 2 +- docs/docs/segment-java.md | 2 +- docs/docs/segment-julia.md | 2 +- docs/docs/segment-kubectl.md | 2 +- docs/docs/segment-nbgv.mdx | 2 +- docs/docs/segment-nightscout.md | 2 +- docs/docs/segment-node.md | 2 +- docs/docs/segment-os.md | 2 +- docs/docs/segment-owm.md | 2 +- docs/docs/segment-path.md | 2 +- docs/docs/segment-php.md | 2 +- docs/docs/segment-plastic.md | 2 +- docs/docs/segment-posh-git.mdx | 2 +- docs/docs/segment-python.md | 2 +- docs/docs/segment-ruby.md | 2 +- docs/docs/segment-rust.md | 2 +- docs/docs/segment-session.md | 2 +- docs/docs/segment-shell.md | 2 +- docs/docs/segment-spotify.md | 2 +- docs/docs/segment-strava.mdx | 2 +- docs/docs/segment-sysinfo.md | 2 +- docs/docs/segment-terraform.md | 2 +- docs/docs/segment-text.md | 2 +- docs/docs/segment-time.md | 2 +- docs/docs/segment-wakatime.md | 2 +- docs/docs/segment-wifi.md | 2 +- docs/docs/segment-winreg.md | 2 +- docs/docs/segment-ytm.md | 4 +- docs/sidebars.js | 2 +- src/template/func_map.go | 2 + src/template/link.go | 18 ++++++ src/template/link_test.go | 63 +++++++++++++++++++ themes/schema.json | 2 +- 51 files changed, 146 insertions(+), 75 deletions(-) rename docs/docs/{config-text.md => config-templates.md} (69%) create mode 100644 src/template/link.go create mode 100644 src/template/link_test.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 42b3febb..078d9db8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "mode": "debug", "program": "${workspaceRoot}/src", "args": [ - "--config=${workspaceRoot}/themes/jandedobbeleer.omp.json", + "--config=/Users/jan/.jandedobbeleer.omp.json", "--shell=pwsh" ] }, diff --git a/docs/docs/config-segment.md b/docs/docs/config-segment.md index af709a2b..4fad2813 100644 --- a/docs/docs/config-segment.md +++ b/docs/docs/config-segment.md @@ -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 diff --git a/docs/docs/config-text.md b/docs/docs/config-templates.md similarity index 69% rename from docs/docs/config-text.md rename to docs/docs/config-templates.md index d260ae2a..24717127 100644 --- a/docs/docs/config-text.md +++ b/docs/docs/config-templates.md @@ -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 diff --git a/docs/docs/config-title.md b/docs/docs/config-title.md index 1eb8321a..a60624ea 100644 --- a/docs/docs/config-title.md +++ b/docs/docs/config-title.md @@ -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 diff --git a/docs/docs/segment-angular.md b/docs/docs/segment-angular.md index 4a12cb28..7db12427 100644 --- a/docs/docs/segment-angular.md +++ b/docs/docs/segment-angular.md @@ -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 diff --git a/docs/docs/segment-aws.md b/docs/docs/segment-aws.md index 55992b3a..bfad42bf 100644 --- a/docs/docs/segment-aws.md +++ b/docs/docs/segment-aws.md @@ -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 diff --git a/docs/docs/segment-az.md b/docs/docs/segment-az.md index f8d73f02..0c0ff5d8 100644 --- a/docs/docs/segment-az.md +++ b/docs/docs/segment-az.md @@ -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 diff --git a/docs/docs/segment-azfunc.md b/docs/docs/segment-azfunc.md index 3db64957..596aa654 100644 --- a/docs/docs/segment-azfunc.md +++ b/docs/docs/segment-azfunc.md @@ -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 diff --git a/docs/docs/segment-battery.md b/docs/docs/segment-battery.md index 75f89743..3575e683 100644 --- a/docs/docs/segment-battery.md +++ b/docs/docs/segment-battery.md @@ -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 diff --git a/docs/docs/segment-brewfather.md b/docs/docs/segment-brewfather.md index f71cd7e1..77eb229a 100644 --- a/docs/docs/segment-brewfather.md +++ b/docs/docs/segment-brewfather.md @@ -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 diff --git a/docs/docs/segment-command.md b/docs/docs/segment-command.md index 41e66ac8..ddcbab44 100644 --- a/docs/docs/segment-command.md +++ b/docs/docs/segment-command.md @@ -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 diff --git a/docs/docs/segment-crystal.md b/docs/docs/segment-crystal.md index 54efb5e0..20138841 100644 --- a/docs/docs/segment-crystal.md +++ b/docs/docs/segment-crystal.md @@ -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 diff --git a/docs/docs/segment-dart.md b/docs/docs/segment-dart.md index 4ef1acb1..8980b5a0 100644 --- a/docs/docs/segment-dart.md +++ b/docs/docs/segment-dart.md @@ -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 diff --git a/docs/docs/segment-dotnet.md b/docs/docs/segment-dotnet.md index 3596147c..4246f88f 100644 --- a/docs/docs/segment-dotnet.md +++ b/docs/docs/segment-dotnet.md @@ -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 diff --git a/docs/docs/segment-executiontime.md b/docs/docs/segment-executiontime.md index 0996c286..d0a2f8d6 100644 --- a/docs/docs/segment-executiontime.md +++ b/docs/docs/segment-executiontime.md @@ -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 diff --git a/docs/docs/segment-exit.md b/docs/docs/segment-exit.md index a80f3ad1..338d9451 100644 --- a/docs/docs/segment-exit.md +++ b/docs/docs/segment-exit.md @@ -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 diff --git a/docs/docs/segment-git.mdx b/docs/docs/segment-git.mdx index f848abf2..ecfcaf96 100644 --- a/docs/docs/segment-git.mdx +++ b/docs/docs/segment-git.mdx @@ -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 diff --git a/docs/docs/segment-golang.md b/docs/docs/segment-golang.md index 2dfb348d..118265ba 100644 --- a/docs/docs/segment-golang.md +++ b/docs/docs/segment-golang.md @@ -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 diff --git a/docs/docs/segment-ipify.md b/docs/docs/segment-ipify.md index 194d0cc2..9ef780ea 100644 --- a/docs/docs/segment-ipify.md +++ b/docs/docs/segment-ipify.md @@ -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/ diff --git a/docs/docs/segment-java.md b/docs/docs/segment-java.md index 793fc037..69a252ab 100644 --- a/docs/docs/segment-java.md +++ b/docs/docs/segment-java.md @@ -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 diff --git a/docs/docs/segment-julia.md b/docs/docs/segment-julia.md index 4ffaff3e..dc80aa5e 100644 --- a/docs/docs/segment-julia.md +++ b/docs/docs/segment-julia.md @@ -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 diff --git a/docs/docs/segment-kubectl.md b/docs/docs/segment-kubectl.md index 870705c8..95f78822 100644 --- a/docs/docs/segment-kubectl.md +++ b/docs/docs/segment-kubectl.md @@ -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 diff --git a/docs/docs/segment-nbgv.mdx b/docs/docs/segment-nbgv.mdx index 7c12da2c..f2699d39 100644 --- a/docs/docs/segment-nbgv.mdx +++ b/docs/docs/segment-nbgv.mdx @@ -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 diff --git a/docs/docs/segment-nightscout.md b/docs/docs/segment-nightscout.md index 969979fb..38c13f02 100644 --- a/docs/docs/segment-nightscout.md +++ b/docs/docs/segment-nightscout.md @@ -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/ diff --git a/docs/docs/segment-node.md b/docs/docs/segment-node.md index 853f09a6..242f83cf 100644 --- a/docs/docs/segment-node.md +++ b/docs/docs/segment-node.md @@ -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 diff --git a/docs/docs/segment-os.md b/docs/docs/segment-os.md index f18d5453..cbf5e267 100644 --- a/docs/docs/segment-os.md +++ b/docs/docs/segment-os.md @@ -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 diff --git a/docs/docs/segment-owm.md b/docs/docs/segment-owm.md index b74fac2e..7eceb94d 100644 --- a/docs/docs/segment-owm.md +++ b/docs/docs/segment-owm.md @@ -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 diff --git a/docs/docs/segment-path.md b/docs/docs/segment-path.md index f6aeb8ea..afa8b91d 100644 --- a/docs/docs/segment-path.md +++ b/docs/docs/segment-path.md @@ -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 diff --git a/docs/docs/segment-php.md b/docs/docs/segment-php.md index 6bc0f1f4..40b48482 100644 --- a/docs/docs/segment-php.md +++ b/docs/docs/segment-php.md @@ -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 diff --git a/docs/docs/segment-plastic.md b/docs/docs/segment-plastic.md index f33afa7f..5aaaed2d 100644 --- a/docs/docs/segment-plastic.md +++ b/docs/docs/segment-plastic.md @@ -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 diff --git a/docs/docs/segment-posh-git.mdx b/docs/docs/segment-posh-git.mdx index aa398cab..ff0c40e7 100644 --- a/docs/docs/segment-posh-git.mdx +++ b/docs/docs/segment-posh-git.mdx @@ -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 diff --git a/docs/docs/segment-python.md b/docs/docs/segment-python.md index cd179016..2563a06f 100644 --- a/docs/docs/segment-python.md +++ b/docs/docs/segment-python.md @@ -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 diff --git a/docs/docs/segment-ruby.md b/docs/docs/segment-ruby.md index fe5d5395..23a514ab 100644 --- a/docs/docs/segment-ruby.md +++ b/docs/docs/segment-ruby.md @@ -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 diff --git a/docs/docs/segment-rust.md b/docs/docs/segment-rust.md index ee44b1ed..eebed922 100644 --- a/docs/docs/segment-rust.md +++ b/docs/docs/segment-rust.md @@ -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 diff --git a/docs/docs/segment-session.md b/docs/docs/segment-session.md index 9548affb..f82d400a 100644 --- a/docs/docs/segment-session.md +++ b/docs/docs/segment-session.md @@ -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 diff --git a/docs/docs/segment-shell.md b/docs/docs/segment-shell.md index 79850725..8c13f622 100644 --- a/docs/docs/segment-shell.md +++ b/docs/docs/segment-shell.md @@ -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 diff --git a/docs/docs/segment-spotify.md b/docs/docs/segment-spotify.md index c8c1b301..7cba0c44 100644 --- a/docs/docs/segment-spotify.md +++ b/docs/docs/segment-spotify.md @@ -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 diff --git a/docs/docs/segment-strava.mdx b/docs/docs/segment-strava.mdx index 1a27b72f..22c03a05 100644 --- a/docs/docs/segment-strava.mdx +++ b/docs/docs/segment-strava.mdx @@ -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 diff --git a/docs/docs/segment-sysinfo.md b/docs/docs/segment-sysinfo.md index c268744b..4b5f5514 100644 --- a/docs/docs/segment-sysinfo.md +++ b/docs/docs/segment-sysinfo.md @@ -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 diff --git a/docs/docs/segment-terraform.md b/docs/docs/segment-terraform.md index eaf08245..35bd1773 100644 --- a/docs/docs/segment-terraform.md +++ b/docs/docs/segment-terraform.md @@ -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 diff --git a/docs/docs/segment-text.md b/docs/docs/segment-text.md index 21552f38..17ccbd0d 100644 --- a/docs/docs/segment-text.md +++ b/docs/docs/segment-text.md @@ -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 diff --git a/docs/docs/segment-time.md b/docs/docs/segment-time.md index 9f4fb14e..27439a51 100644 --- a/docs/docs/segment-time.md +++ b/docs/docs/segment-time.md @@ -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/ diff --git a/docs/docs/segment-wakatime.md b/docs/docs/segment-wakatime.md index b863af32..b8496550 100644 --- a/docs/docs/segment-wakatime.md +++ b/docs/docs/segment-wakatime.md @@ -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 diff --git a/docs/docs/segment-wifi.md b/docs/docs/segment-wifi.md index 4d9f59f4..7ed49520 100644 --- a/docs/docs/segment-wifi.md +++ b/docs/docs/segment-wifi.md @@ -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 diff --git a/docs/docs/segment-winreg.md b/docs/docs/segment-winreg.md index 6e4c870f..a459453b 100644 --- a/docs/docs/segment-winreg.md +++ b/docs/docs/segment-winreg.md @@ -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 diff --git a/docs/docs/segment-ytm.md b/docs/docs/segment-ytm.md index e77685be..5e326be9 100644 --- a/docs/docs/segment-ytm.md +++ b/docs/docs/segment-ytm.md @@ -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 diff --git a/docs/sidebars.js b/docs/sidebars.js index c9316326..4fb09b2e 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -25,7 +25,7 @@ module.exports = { "config-sample", "config-title", "config-colors", - "config-text", + "config-templates", "config-transient", "config-tooltips", "config-fonts" diff --git a/src/template/func_map.go b/src/template/func_map.go index f6a786d2..163a09a1 100644 --- a/src/template/func_map.go +++ b/src/template/func_map.go @@ -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 { diff --git a/src/template/link.go b/src/template/link.go new file mode 100644 index 00000000..ab159880 --- /dev/null +++ b/src/template/link.go @@ -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 +} diff --git a/src/template/link_test.go b/src/template/link_test.go new file mode 100644 index 00000000..6551bbb5 --- /dev/null +++ b/src/template/link_test.go @@ -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) + } +} diff --git a/themes/schema.json b/themes/schema.json index 08e0d9b5..8dac0159 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -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": {