Skip to content

Instantly share code, notes, and snippets.

@mdgrs-mei
Last active November 4, 2024 22:16
Show Gist options
  • Save mdgrs-mei/1599cb07ef5bc67125ebffba9c8f1e37 to your computer and use it in GitHub Desktop.
Save mdgrs-mei/1599cb07ef5bc67125ebffba9c8f1e37 to your computer and use it in GitHub Desktop.
Adds escape codes to the prompt for the shell integration
# 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
}
@mdgrs-mei
Copy link
Author

mdgrs-mei commented Jul 15, 2023

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.

& 'PathTo/ShellIntegration.ps1'

The code adds the escape sequences to the prompt but keeps your prompt customization. It also emits FTCS_COMMAND_EXECUTED so autoMarkPrompts in the Windows Terminal setting needs to be turned off.

"profiles":
{
    "defaults": 
    {
        "experimental.autoMarkPrompts": false,
        "experimental.rightClickContextMenu": true,
        "experimental.showMarksOnScrollbar": true
    },
}

Refer to this post about the shell integration feature in the Windows Terminal itself.

ShellIntegration

@mdgrs-mei
Copy link
Author

mdgrs-mei commented Jul 16, 2023

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

@mdgrs-mei
Copy link
Author

Supported iTerm2 on macOS.

& 'PathTo/ShellIntegration.ps1' -TerminalProgram ITerm2

ITerm2ShellIntegration

@xborke
Copy link

xborke commented Sep 3, 2024

hey it looks very nice but is it possible to add something like Tmux style status bar to powershell at bottom?

@mdgrs-mei
Copy link
Author

@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