From 063b95da2543bad38bd1d3875a1960995cb91e6b Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Fri, 13 Sep 2024 02:01:07 +0800 Subject: [PATCH] fix(pwsh): prevent a command from eating tail of a transient prompt --- src/prompt/engine.go | 6 ++++-- src/shell/scripts/omp.ps1 | 13 ++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/prompt/engine.go b/src/prompt/engine.go index 40ee8961..01754626 100644 --- a/src/prompt/engine.go +++ b/src/prompt/engine.go @@ -157,7 +157,9 @@ func (e *Engine) shouldFill(filler string, padLength int) (string, bool) { } repeat := padLength / lenFiller - return strings.Repeat(filler, repeat), true + unfilled := padLength % lenFiller + text := strings.Repeat(filler, repeat) + strings.Repeat(" ", unfilled) + return text, true } func (e *Engine) getTitleTemplateText() string { @@ -531,7 +533,7 @@ func New(flags *runtime.Flags) *Engine { switch env.Shell() { case shell.ELVISH: - // In Elvish, continuous text is always cut off before the right-most cell on the terminal screen. + // In Elvish, a prompt line will always wrap when the cursor reaches the rightmost cell on the terminal screen. // We have to reduce the terminal width by 1 so a right-aligned block will not be broken. eng.rectifyTerminalWidth(-1) case shell.PWSH, shell.PWSH5: diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index 66291dd6..3fe9c461 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -458,7 +458,18 @@ Example: Set-PSReadLineOption -ExtraPromptLineCount (($standardOut | Measure-Object -Line).Lines - 1) # The output can be multi-line, joining them ensures proper rendering. - $standardOut -join "`n" + $output = $standardOut -join "`n" + + if ($script:PromptType -eq 'transient') { + # Workaround to prevent a command from eating the tail of a transient prompt, when we're at the end of the line. + $command = '' + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null) + if ($command) { + $output += " `b`b" + } + } + + $output # remove any posh-git status $env:POSH_GIT_STATUS = $null