From 17da877767bb778c9759c17843c80d35de423573 Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Sun, 23 Apr 2023 13:17:13 +0800 Subject: [PATCH] fix(pwsh): do not render a transient prompt when text is selected --- src/shell/scripts/omp.ps1 | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index b013b8fc..4b1d11f4 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -148,10 +148,6 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { } function Set-TransientPrompt { - param( - [string]$Key - ) - $previousOutputEncoding = [Console]::OutputEncoding try { $parseErrors = $null @@ -174,21 +170,23 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::Undo() } } - switch ($Key) { - "Ctrl+c" { [Microsoft.PowerShell.PSConsoleReadLine]::CopyOrCancelLine() } - Default { [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() } - } [Console]::OutputEncoding = $previousOutputEncoding } - } if (("::TRANSIENT::" -eq "true") -and ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage")) { Set-PSReadLineKeyHandler -Key Enter -BriefDescription 'OhMyPoshEnterKeyHandler' -ScriptBlock { - Set-TransientPrompt -Key "Enter" + Set-TransientPrompt + [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() } Set-PSReadLineKeyHandler -Key Ctrl+c -BriefDescription 'OhMyPoshCtrlCKeyHandler' -ScriptBlock { - Set-TransientPrompt -Key "Ctrl+c" + $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 + } + [Microsoft.PowerShell.PSConsoleReadLine]::CopyOrCancelLine() } }