From 0895aca72eaa8bfbe18a1e2aab313eb4d549b5a1 Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Thu, 28 Jul 2022 17:49:50 +0800 Subject: [PATCH] fix(pwsh): tweak the workaround for transient prompt --- src/shell/scripts/omp.ps1 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index 1e229f9f..88a51918 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -125,12 +125,16 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() } } finally { + # If PSReadline is set to display suggestion list, this workaround is needed to clear the buffer below + # before accepting the current commandline. The max amount of items in the list is 10, so 12 lines + # are cleared (10 + 1 more for the prompt + 1 more for current commandline). if ((Get-PSReadLineOption).PredictionViewStyle -eq 'ListView') { - # If PSReadline is set to display suggestion list, this workaround is needed to clear the buffer below - # before accepting the current commandline. The max amount of items in the list is 10, so 12 lines - # are cleared (10 + 1 more for the prompt + 1 more for current commandline). - [Microsoft.PowerShell.PSConsoleReadLine]::Insert("`n" * [System.Math]::Min($Host.UI.RawUI.WindowSize.Height - $Host.UI.RawUI.CursorPosition.Y - 1, 12)) - [Microsoft.PowerShell.PSConsoleReadLine]::Undo() + $terminalHeight = $Host.UI.RawUI.WindowSize.Height + # only do this on an valid value + if ([int]$terminalHeight -gt 0) { + [Microsoft.PowerShell.PSConsoleReadLine]::Insert("`n" * [System.Math]::Min($terminalHeight - $Host.UI.RawUI.CursorPosition.Y - 1, 12)) + [Microsoft.PowerShell.PSConsoleReadLine]::Undo() + } } [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() [Console]::OutputEncoding = $previousOutputEncoding