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 ""
}
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.GetCursorForRightWrite(length, 0))
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")) {
Set-PSReadLineKeyHandler -Key Spacebar -BriefDescription 'OhMyPoshSpaceKeyHandler' -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
$position = $host.UI.RawUI.CursorPosition
$cleanPSWD = Get-CleanPSWD
$command = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null)
$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
}
$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"))
if ($standardOut -ne '') {
# 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
Write-Host "`e[K" -NoNewline
Write-Host $standardOut -NoNewline
# ignore an empty tooltip
if ($standardOut -eq '') {
return
}
Write-Host $standardOut -NoNewline
$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
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
[Microsoft.PowerShell.PSConsoleReadLine]::Undo()