fix(pwsh): wrap keyhandlers in try/finally

This commit is contained in:
Jan De Dobbeleer 2023-11-01 12:23:37 +01:00 committed by Jan De Dobbeleer
parent 25cc389a33
commit 1f5d3ea90a

View file

@ -182,20 +182,27 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
if (("::TRANSIENT::" -eq "true") -and ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage")) { if (("::TRANSIENT::" -eq "true") -and ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage")) {
Set-PSReadLineKeyHandler -Key Enter -BriefDescription 'OhMyPoshEnterKeyHandler' -ScriptBlock { Set-PSReadLineKeyHandler -Key Enter -BriefDescription 'OhMyPoshEnterKeyHandler' -ScriptBlock {
$executingCommand = Set-TransientPrompt try {
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() $executingCommand = Set-TransientPrompt
# Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
if (("::FTCS_MARKS::" -eq "true") -and $executingCommand) { # Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution
Write-Host "$([char]0x1b)]133;C`a" -NoNewline if (("::FTCS_MARKS::" -eq "true") -and $executingCommand) {
Write-Host "$([char]0x1b)]133;C`a" -NoNewline
}
} }
finally {}
} }
Set-PSReadLineKeyHandler -Key Ctrl+c -BriefDescription 'OhMyPoshCtrlCKeyHandler' -ScriptBlock { Set-PSReadLineKeyHandler -Key Ctrl+c -BriefDescription 'OhMyPoshCtrlCKeyHandler' -ScriptBlock {
$start = $null try {
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$start, [ref]$null) $start = $null
# only render a transient prompt when no text is selected [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$start, [ref]$null)
if ($start -eq -1) { # only render a transient prompt when no text is selected
Set-TransientPrompt if ($start -eq -1) {
Set-TransientPrompt
}
} }
finally {}
[Microsoft.PowerShell.PSConsoleReadLine]::CopyOrCancelLine() [Microsoft.PowerShell.PSConsoleReadLine]::CopyOrCancelLine()
} }
} }
@ -209,7 +216,9 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
$executingCommand = $parseErrors.Count -eq 0 $executingCommand = $parseErrors.Count -eq 0
} }
finally {} finally {}
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
# Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution # Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution
if ($executingCommand) { if ($executingCommand) {
Write-Host "$([char]0x1b)]133;C`a" -NoNewline Write-Host "$([char]0x1b)]133;C`a" -NoNewline