refactor(pwsh): move tooltip erasure logic to engine

This commit is contained in:
L. Yeung 2023-04-22 23:29:08 +08:00 committed by Jan De Dobbeleer
parent 287f183244
commit 6c181c1120
2 changed files with 13 additions and 9 deletions

View file

@ -398,6 +398,9 @@ func (e *Engine) PrintTooltip(tip string) string {
return "" return ""
} }
text, length := block.RenderSegments() text, length := block.RenderSegments()
// clear from cursor to the end of the line in case a previous tooltip is cut off and partially preserved,
// if the new one is shorter
e.write(e.Writer.ClearAfter())
e.write(e.Writer.CarriageForward()) e.write(e.Writer.CarriageForward())
e.write(e.Writer.GetCursorForRightWrite(length, 0)) e.write(e.Writer.GetCursorForRightWrite(length, 0))
e.write(text) e.write(text)

View file

@ -123,23 +123,24 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
if (("::TOOLTIPS::" -eq "true") -and ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage")) { if (("::TOOLTIPS::" -eq "true") -and ($ExecutionContext.SessionState.LanguageMode -ne "ConstrainedLanguage")) {
Set-PSReadLineKeyHandler -Key Spacebar -BriefDescription 'OhMyPoshSpaceKeyHandler' -ScriptBlock { Set-PSReadLineKeyHandler -Key Spacebar -BriefDescription 'OhMyPoshSpaceKeyHandler' -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
$position = $host.UI.RawUI.CursorPosition
$cleanPSWD = Get-CleanPSWD
$command = $null $command = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null) [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null)
$command = $command.TrimStart().Split(" ", 2) | Select-Object -First 1 $command = $command.TrimStart().Split(" ", 2) | Select-Object -First 1
if ($command -eq $script:ToolTipCommand) { # ignore an empty/repeated tip
if ($command -eq '' -or $command -eq $script:ToolTipCommand) {
return return
} }
$script:ToolTipCommand = $command $position = $host.UI.RawUI.CursorPosition
$cleanPSWD = Get-CleanPSWD
$standardOut = @(Start-Utf8Process $script:OMPExecutable @("print", "tooltip", "--error=$script:ErrorCode", "--shell=$script:ShellName", "--pswd=$cleanPSWD", "--config=$env:POSH_THEME", "--command=$command", "--shell-version=$script:PSVersion")) $standardOut = @(Start-Utf8Process $script:OMPExecutable @("print", "tooltip", "--error=$script:ErrorCode", "--shell=$script:ShellName", "--pswd=$cleanPSWD", "--config=$env:POSH_THEME", "--command=$command", "--shell-version=$script:PSVersion"))
if ($standardOut -ne '') { # ignore an empty tooltip
# clear from cursor to the end of the line in case a previous tooltip is cut off and partially preserved, if ($standardOut -eq '') {
# if the new one is shorter return
Write-Host "`e[K" -NoNewline
Write-Host $standardOut -NoNewline
} }
Write-Host $standardOut -NoNewline
$host.UI.RawUI.CursorPosition = $position $host.UI.RawUI.CursorPosition = $position
# cache the tip command
$script:ToolTipCommand = $command
# we need this workaround to prevent the text after cursor from disappearing when the tooltip is rendered # we need this workaround to prevent the text after cursor from disappearing when the tooltip is rendered
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
[Microsoft.PowerShell.PSConsoleReadLine]::Undo() [Microsoft.PowerShell.PSConsoleReadLine]::Undo()