Last active
November 4, 2024 22:16
-
-
Save mdgrs-mei/1599cb07ef5bc67125ebffba9c8f1e37 to your computer and use it in GitHub Desktop.
Adds escape codes to the prompt for the shell integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reference: | |
# https://devblogs.microsoft.com/commandline/shell-integration-in-the-windows-terminal/ | |
param | |
( | |
[ValidateSet('WindowsTerminal', 'ITerm2')] | |
[String]$TerminalProgram = 'WindowsTerminal' | |
) | |
# Restore hooked functions in case this script is executed accidentally twice | |
if ($global:shellIntegrationGlobals) { | |
$function:global:PSConsoleHostReadLine = $global:shellIntegrationGlobals.originalPSConsoleHostReadLine | |
$function:global:Prompt = $global:shellIntegrationGlobals.originalPrompt | |
} | |
$global:shellIntegrationGlobals = @{ | |
terminalProgram = $TerminalProgram | |
originalPSConsoleHostReadLine = $function:global:PSConsoleHostReadLine | |
originalPrompt = $function:global:Prompt | |
lastCommand = $null | |
getExitCode = { | |
param ($lastCommandStatus) | |
if ($lastCommandStatus -eq $true) { | |
return 0 | |
} | |
if ($Error[0]) { | |
$lastHistory = Get-History -Count 1 | |
$isPowerShellError = $Error[0].InvocationInfo.HistoryId -eq $lastHistory.Id | |
} | |
if ($isPowerShellError) { | |
return 1 | |
} | |
else { | |
return $LastExitCode | |
} | |
} | |
} | |
$function:global:PSConsoleHostReadLine = { | |
$commandExecuted = "`e]133;C`a" | |
$command = $global:shellIntegrationGlobals.originalPSConsoleHostReadLine.Invoke() | |
$commandExecuted | Write-Host -NoNewLine | |
$command | |
$global:shellIntegrationGlobals.lastCommand = $command | |
} | |
$function:global:Prompt = { | |
$lastCommandStatus = $? | |
if ($global:shellIntegrationGlobals.lastCommand) { | |
$exitCode = $global:shellIntegrationGlobals.getExitCode.Invoke($lastCommandStatus) | |
$commandFinished = "`e]133;D;$exitCode`a" | |
} | |
else { | |
$commandFinished = "`e]133;D`a" | |
} | |
$currentLocation = $ExecutionContext.SessionState.Path.CurrentLocation.Path | |
switch ($global:shellIntegrationGlobals.terminalProgram) { | |
'WindowsTerminal' { $setWorkingDirectory = "`e]9;9;`"$currentLocation`"`a" } | |
'ITerm2' { $setWorkingDirectory = "`e]1337;CurrentDir=$currentLocation`a" } | |
} | |
$promptStarted = "`e]133;A`a" | |
$commandStarted = "`e]133;B`a" | |
$prompt = $global:shellIntegrationGlobals.originalPrompt.Invoke() | |
$commandFinished + $promptStarted + $setWorkingDirectory + $prompt + $commandStarted | |
} |
In the VSCode terminal, you don't need to add this code. The shell integration is activated by default by dot-sourcing a similar builtin script.
. "$(code --locate-shell-integration-path pwsh)"
https://code.visualstudio.com/docs/terminal/shell-integration
hey it looks very nice but is it possible to add something like Tmux style status bar to powershell at bottom?
@xborke I don't think there is any way to add a status bar in PowerShell but I tried to make a similar thing using the title bar a while ago (DynamicTitle). There is also a discussion about supporting the status bar in the Windows Terminal here.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can leverage the "shell integration" feature in the Windows Terminal by adding the following code to your profile after the definition of your prompt function.
The code adds the escape sequences to the prompt but keeps your prompt customization. It also emits
FTCS_COMMAND_EXECUTED
soautoMarkPrompts
in the Windows Terminal setting needs to be turned off.Refer to this post about the shell integration feature in the Windows Terminal itself.