From 1f5d3ea90a49f6d1e76de75482cc8da529520076 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 1 Nov 2023 12:23:37 +0100 Subject: [PATCH] fix(pwsh): wrap keyhandlers in try/finally --- src/shell/scripts/omp.ps1 | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index 719dd8bd356f..e76e457060fc 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -182,20 +182,27 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { if (("::TRANSIENT::" -eq "true") -and ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage")) { Set-PSReadLineKeyHandler -Key Enter -BriefDescription 'OhMyPoshEnterKeyHandler' -ScriptBlock { - $executingCommand = Set-TransientPrompt - [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() - # Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution - if (("::FTCS_MARKS::" -eq "true") -and $executingCommand) { - Write-Host "$([char]0x1b)]133;C`a" -NoNewline + try { + $executingCommand = Set-TransientPrompt + [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() + # Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution + if (("::FTCS_MARKS::" -eq "true") -and $executingCommand) { + Write-Host "$([char]0x1b)]133;C`a" -NoNewline + } } + finally {} } Set-PSReadLineKeyHandler -Key Ctrl+c -BriefDescription 'OhMyPoshCtrlCKeyHandler' -ScriptBlock { - $start = $null - [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$start, [ref]$null) - # only render a transient prompt when no text is selected - if ($start -eq -1) { - Set-TransientPrompt + try { + $start = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$start, [ref]$null) + # only render a transient prompt when no text is selected + if ($start -eq -1) { + Set-TransientPrompt + } } + finally {} + [Microsoft.PowerShell.PSConsoleReadLine]::CopyOrCancelLine() } } @@ -209,7 +216,9 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { $executingCommand = $parseErrors.Count -eq 0 } finally {} + [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() + # Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution if ($executingCommand) { Write-Host "$([char]0x1b)]133;C`a" -NoNewline