diff --git a/src/cli/init.go b/src/cli/init.go index 1ce3cc09..73f96ad1 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "oh-my-posh/engine" "oh-my-posh/environment" "oh-my-posh/shell" @@ -56,6 +57,10 @@ func runInit(shellName string) { } env.Init() defer env.Close() + cfg := engine.LoadConfig(env) + shell.Transient = cfg.TransientPrompt != nil + shell.ErrorLine = cfg.ErrorLine != nil || cfg.ValidLine != nil + shell.Tooltips = len(cfg.Tooltips) > 0 if print { init := shell.PrintInit(env) fmt.Print(init) diff --git a/src/engine/config.go b/src/engine/config.go index aef61da4..e160e9ce 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -123,11 +123,6 @@ func loadConfig(env environment.Environment) *Config { err = config.BindStruct("", &cfg) cfg.exitWithError(err) - // initialize default values - if cfg.TransientPrompt == nil { - cfg.TransientPrompt = &Segment{} - } - return &cfg } diff --git a/src/shell/init.go b/src/shell/init.go index 3cf160ed..61fcd6af 100644 --- a/src/shell/init.go +++ b/src/shell/init.go @@ -3,6 +3,7 @@ package shell import ( _ "embed" "path/filepath" + "strconv" "fmt" "oh-my-posh/environment" @@ -33,6 +34,12 @@ const ( noExe = "echo \"Unable to find Oh My Posh executable\"" ) +var ( + Transient bool + ErrorLine bool + Tooltips bool +) + func getExecutablePath(env environment.Environment) (string, error) { executable, err := os.Executable() if err != nil { @@ -102,6 +109,9 @@ func PrintInit(env environment.Environment) string { func getShellInitScript(executable, configFile, script string) string { script = strings.ReplaceAll(script, "::OMP::", executable) script = strings.ReplaceAll(script, "::CONFIG::", configFile) + script = strings.ReplaceAll(script, "::TRANSIENT::", strconv.FormatBool(Transient)) + script = strings.ReplaceAll(script, "::ERROR_LINE::", strconv.FormatBool(ErrorLine)) + script = strings.ReplaceAll(script, "::TOOLTIPS::", strconv.FormatBool(Tooltips)) return script } diff --git a/src/shell/scripts/omp.fish b/src/shell/scripts/omp.fish index 34368a58..dd1886dd 100644 --- a/src/shell/scripts/omp.fish +++ b/src/shell/scripts/omp.fish @@ -64,7 +64,7 @@ function _render_tooltip commandline --function repaint end -function enable_poshtooltips +if test "::TOOLTIPS::" = "true" bind \x20 _render_tooltip end @@ -76,6 +76,12 @@ function _render_transient commandline --function execute end -function enable_poshtransientprompt +if test "::TRANSIENT::" = "true" bind \r _render_transient end + +# legacy functions +function enable_poshtooltips +end +function enable_poshtransientprompt +end diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index 4ed81a34..9d6f555b 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -100,7 +100,7 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { } } - function Enable-PoshTooltips { + if ("::TOOLTIPS::" -eq "true") { Set-PSReadLineKeyHandler -Key SpaceBar -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') $position = $host.UI.RawUI.CursorPosition @@ -116,7 +116,7 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { } } - function Enable-PoshTransientPrompt { + if ("::TRANSIENT::" -eq "true") { Set-PSReadLineKeyHandler -Key Enter -ScriptBlock { $previousOutputEncoding = [Console]::OutputEncoding try { @@ -145,7 +145,7 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { } } - function Enable-PoshLineError { + if ("::ERROR_LINE::" -eq "true") { $validLine = @(Start-Utf8Process $script:OMPExecutable @("print", "valid", "--config=$env:POSH_THEME", "--shell=$script:ShellName")) -join "`n" $errorLine = @(Start-Utf8Process $script:OMPExecutable @("print", "error", "--config=$env:POSH_THEME", "--shell=$script:ShellName")) -join "`n" Set-PSReadLineOption -PromptText $validLine, $errorLine @@ -344,6 +344,11 @@ Example: # set secondary prompt Set-PSReadLineOption -ContinuationPrompt (@(Start-Utf8Process $script:OMPExecutable @("print", "secondary", "--config=$env:POSH_THEME", "--shell=$script:ShellName")) -join "`n") + # legacy functions + function Enable-PoshTooltips {} + function Enable-PoshTransientPrompt {} + function Enable-PoshLineError {} + Export-ModuleMember -Function @( "Set-PoshContext" "Enable-PoshTooltips" diff --git a/src/shell/scripts/omp.zsh b/src/shell/scripts/omp.zsh index e7cd079a..0151775f 100644 --- a/src/shell/scripts/omp.zsh +++ b/src/shell/scripts/omp.zsh @@ -60,9 +60,9 @@ function self-insert() { zle .self-insert } -function enable_poshtooltips() { +if [[ "::TOOLTIPS::" = "true" ]]; then zle -N self-insert -} +fi _posh-zle-line-init() { [[ $CONTEXT == start ]] || return 0 @@ -91,6 +91,10 @@ _posh-zle-line-init() { return ret } -function enable_poshtransientprompt() { +if [[ "::TRANSIENT::" = "true" ]]; then zle -N zle-line-init _posh-zle-line-init -} +fi + +# legacy functions for backwards compatibility +function enable_poshtooltips() {} +function enable_poshtransientprompt() {} diff --git a/website/docs/configuration/line-error.mdx b/website/docs/configuration/line-error.mdx index d56c1f94..29bcf583 100644 --- a/website/docs/configuration/line-error.mdx +++ b/website/docs/configuration/line-error.mdx @@ -20,7 +20,8 @@ There are two config settings you need to tweak: - `error_line`: displays when the line is faulty You can use go [text/template][go-text-template] templates extended with [sprig][sprig] to enrich the text. -Environment variables are available, just like the [`console_title_template`][console-title] functionality. +All [template][templates] functionality is available, even reusing [cross segment template properties][cstp] from +the previous primary prompt run. ## Configuration @@ -49,35 +50,7 @@ The configuration has the following properties: - background: `string` [color][colors] - foreground: `string` [color][colors] -- template: `string` - a go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the -properties below - defaults to ` ` - -## Template ([info][templates]) - -- `.Root`: `boolean` - is the current user root/admin or not -- `.PWD`: `string` - the current working directory -- `.Folder`: `string` - the current working folder -- `.Shell`: `string` - the current shell name -- `.UserName`: `string` - the current user name -- `.HostName`: `string` - the host name -- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name - -## Enable the feature - -Invoke Oh My Posh in your `$PROFILE` and add the following line below. - -```powershell -oh-my-posh init pwsh --config $env:POSH_THEMES_PATH/jandedobbeleer.omp.json | Invoke-Expression -// highlight-start -Enable-PoshLineError -// highlight-end -``` - -:::caution -If you import **PSReadLine** separately, make sure to import it before the `Enable-PoshLineError` command. -::: - -Restart your shell or reload your `$PROFILE` using `. $PROFILE` for the changes to take effect. +- template: `string` - a fully featured [template][templates] - defaults to ` ` [colors]: /docs/configuration/colors [go-text-template]: https://golang.org/pkg/text/template/ @@ -85,3 +58,4 @@ Restart your shell or reload your `$PROFILE` using `. $PROFILE` for the changes [console-title]: /docs/configuration/title#console-title-template [psreadline]: https://github.com/PowerShell/PSReadLine [templates]: /docs/configuration/templates +[cstp]: /docs/configuration/templates#cross-segment-template-properties diff --git a/website/docs/configuration/tooltips.mdx b/website/docs/configuration/tooltips.mdx index b72b0a57..e178e3f9 100644 --- a/website/docs/configuration/tooltips.mdx +++ b/website/docs/configuration/tooltips.mdx @@ -53,58 +53,4 @@ This configuration will render a right-aligned git segment when you type `git` o A tip should not include any leading or trailing space but an interpolated one can be used, e.g., `g s`. Keep in mind that this is a blocking call, meaning that if the segment renders slow, you can't type until it's visible. Optimizations in this space are being explored. -## Enable the feature - - - - -Import/invoke Oh My Posh in your `$PROFILE` and add the following line below: - -```powershell -Enable-PoshTooltips -``` - -For example: - -```powershell -# $PROFILE -oh-my-posh init pwsh --config ~\wildertheme.json | Invoke-Expression -Enable-PoshTooltips -``` - -Restart your shell or reload your `$PROFILE` using `. $PROFILE` for the changes to take effect. - - - - -Invoke Oh My Posh in `.zshrc` and add the following line below: - -```bash -enable_poshtooltips -``` - -Restart your shell or reload `.zshrc` using `exec zsh` for the changes to take effect. - - - - -Invoke Oh My Posh in `~/.config/fish/config.fish` and add the following line below: - -```bash -enable_poshtooltips -``` - -Restart your shell or reload fish using `exec fish` for the changes to take effect. - - - - [clink]: https://chrisant996.github.io/clink/ diff --git a/website/docs/configuration/transient.mdx b/website/docs/configuration/transient.mdx index 7b76fcb8..94b3e76a 100644 --- a/website/docs/configuration/transient.mdx +++ b/website/docs/configuration/transient.mdx @@ -13,7 +13,8 @@ This feature only works in `fish`, `zsh`, `powershell` and `cmd` for the time be Transient prompt, when enabled, replaces the prompt with a simpler one to allow more screen real estate. You can use go [text/template][go-text-template] templates extended with [sprig][sprig] to enrich the text. -Environment variables are available, just like the [`console_title_template`][console-title] functionality. +All [template][templates] functionality is available, even reusing [cross segment template properties][cstp] from +the previous primary prompt run. Typically, your prompt will simply leave the prompt on the screen when you execute a command (or press enter) like so: @@ -48,86 +49,17 @@ The configuration has the following properties: - foreground_templates: foreground [color templates][color-templates] - background: `string` [color][colors] - background_templates: background [color templates][color-templates] -- template: `string` - a go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the -properties below - defaults to `{{ .Shell }}> ` - -## Template ([info][templates]) - -- `.Root`: `boolean` - is the current user root/admin or not -- `.PWD`: `string` - the current working directory -- `.Folder`: `string` - the current working folder -- `.Shell`: `string` - the current shell name -- `.UserName`: `string` - the current user name -- `.HostName`: `string` - the host name -- `.Code`: `int` - the last exit code -- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name +- template: `string` - a fully featured [template][templates] - defaults to `{{ .Shell }}> ` ## Enable the feature - - - - -Invoke Oh My Posh in your `$PROFILE` and add the following line below. - -```powershell -oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression -// highlight-start -Enable-PoshTransientPrompt -// highlight-end -``` - -:::caution -If you import **PSReadLine** separately, make sure to import it before the `Enable-PoshTransientPrompt` command. -::: - -Restart your shell or reload your `$PROFILE` using `. $PROFILE` for the changes to take effect. - - - - -You can run the command below to enable the feature permanently: +Oh My posh handles enabling the feature automatically for all shells except `cmd` when the config contains a +transient prompt configuration. For `cmd`, you can run the command below once to enable the feature permanently: ```shell clink set prompt.transient always ``` -Restart your shell for the changes to take effect. - - - - -Invoke Oh My Posh in `.zshrc` and add the following line below: - -```bash -enable_poshtransientprompt -``` - -Restart your shell or reload `.zshrc` using `exec zsh` for the changes to take effect. - - - - -Invoke Oh My Posh in `~/.config/fish/config.fish` and add the following line below: - -```bash -enable_poshtransientprompt -``` - -Restart your shell or reload fish using `exec fish` for the changes to take effect. - - - - [colors]: /docs/configuration/colors [go-text-template]: https://golang.org/pkg/text/template/ [console-title]: /docs/configuration/title#console-title-template @@ -135,3 +67,4 @@ Restart your shell or reload fish using `exec fish` for the changes to take effe [clink]: https://chrisant996.github.io/clink/ [templates]: /docs/configuration/templates [color-templates]: /docs/configuration/colors#color-templates +[cstp]: /docs/configuration/templates#cross-segment-template-properties