From 935e74b53b4964f7792c076a85c4b457c8774593 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Fri, 31 May 2024 20:40:33 -0500 Subject: [PATCH] Add notes for how to do shell integration with oh-my-posh --- TerminalDocs/tutorials/shell-integration.md | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/TerminalDocs/tutorials/shell-integration.md b/TerminalDocs/tutorials/shell-integration.md index c6dac713..8a8066a6 100644 --- a/TerminalDocs/tutorials/shell-integration.md +++ b/TerminalDocs/tutorials/shell-integration.md @@ -150,6 +150,48 @@ function prompt { } ``` +#### Oh My Posh setup + +Using oh-my-posh? You'll want to slightly modify the above, to stash away the +original prompt, then add it back in the middle of the shell integration escape +sequences. + +```pwsh +# initialize oh-my-posh at the top of your profile.ps1 +oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\gruvbox.omp.json" | Invoke-Expression +# then stash away the prompt() that oh-my-posh sets +$Global:__OriginalPrompt = $function:Prompt + +function Global:__Terminal-Get-LastExitCode { + if ($? -eq $True) { return 0 } + $LastHistoryEntry = $(Get-History -Count 1) + $IsPowerShellError = $Error[0].InvocationInfo.HistoryId -eq $LastHistoryEntry.Id + if ($IsPowerShellError) { return -1 } + return $LastExitCode +} + +function prompt { + $gle = $(__Terminal-Get-LastExitCode); + $LastHistoryEntry = $(Get-History -Count 1) + if ($Global:__LastHistoryId -ne -1) { + if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) { + $out += "`e]133;D`a" + } else { + $out += "`e]133;D;$gle`a" + } + } + $loc = $($executionContext.SessionState.Path.CurrentLocation); + $out += "`e]133;A$([char]07)"; + $out += "`e]9;9;`"$loc`"$([char]07)"; + + $out += $Global:__OriginalPrompt.Invoke(); # <-- This line adds the original prompt back + + $out += "`e]133;B$([char]07)"; + $Global:__LastHistoryId = $LastHistoryEntry.Id + return $out +} +``` + ### Command Prompt Command Prompt sources it's prompt from the `PROMPT` environment variable. CMD.exe reads `$e` as the `ESC` character. Unfortunately, CMD.exe doesn't have a way to get the return code of the previous command in the prompt, so we're not able to provide success / error information in CMD prompts.