Last active
October 30, 2022 05:55
-
-
Save Jaykul/d4fbeea122e46b79f84e9d4cbc768c8b to your computer and use it in GitHub Desktop.
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
# Disable shell integration when the language mode is restricted | |
# Prevent installing more than once per session | |
if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage" -or (Test-Path variable:global:__VSCodeOriginalPrompt)) { | |
return; | |
} | |
$Global:__VSCodeOriginalPrompt = $function:Prompt | |
function global:prompt { | |
$ExitCode = $? ? 0 : ($LASTEXITCODE -ne 0) ? $LASTEXITCODE : 1 | |
@( | |
# Prompt started | |
# OSC 633 ; A ST | |
"`e]633;A`a" | |
# Current working directory | |
# OSC 633 ; <Property>=<Value> ST | |
if ($pwd.Provider.Name -eq 'FileSystem') { "`e]633;P;Cwd=$($pwd.ProviderPath)`a" } | |
# Write original prompt | |
$Global:__VSCodeOriginalPrompt.Invoke() | |
# Prompt end | |
# OSC 633 ; B ST | |
"`e]633;B`a" | |
) -join "" | |
} | |
# If PSReadLine is loaded, wrap it to send the "command executed" sequence first | |
function global:PSConsoleHostReadLine { | |
$Command = PSReadLine\PSConsoleHostReadLine | |
$Command | |
[Console]::Write("`e]633;C`a") | |
if ($Command) { | |
[Console]::Write("`e]633;E;$($Command.Replace("\", "\\").Replace("`n", "\x0a").Replace(";", "\x3b"))`a") | |
} | |
} | |
# Set IsWindows property | |
[Console]::Write("`e]633;P;IsWindows=$($IsWindows)`a") | |
# Remap key handlers to the VS Code keybindings | |
function Copy-PSReadlineKeyHandler { | |
param ([string[]]$Current, [string[]]$Replacement) | |
if ($Handler = Get-PSReadLineKeyHandler -Chord $Current | Select-Object -First 1) { | |
Set-PSReadLineKeyHandler -Chord $Replacement -Function $Handler.Function | |
} | |
} | |
Copy-PSReadlineKeyHandler 'Ctrl+Spacebar' 'F12,a' | |
Copy-PSReadlineKeyHandler 'Alt+Spacebar' 'F12,b' | |
Copy-PSReadlineKeyHandler 'Shift+Enter' 'F12,c' | |
Copy-PSReadlineKeyHandler 'Shift+End' 'F12,d' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment