From 8bc89532fa9de028e2eabc35f3ad7fbb69f00033 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 25 Jun 2026 06:02:08 -0700 Subject: [PATCH] Fix #18: re-anchor prompt after F7 history under Terminal.Gui v2 Port the Terminal.Gui v2 prompt-rendering fix from tui-cs/F7History#25 into PSTui's folded-in command-history handler. Under TG v2, Out-ConsoleGridView renders inline by default, so by the time the picker exits the screen has scrolled and PSReadLine's saved prompt row (_initialY) is stale. DeleteLine()/Render() then repaint the prompt in the wrong place. Call InvokePrompt($null, [Console]::CursorTop) first to re-anchor PSReadLine on the current cursor row, then DeleteLine + Insert as before. PSTui keeps DeleteLine (so the typed filter prefix is replaced, not appended) and drops the now-pointless SetCursorPosition dance, matching the upstream intent. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/PSTui/PSTui.History.psm1 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/PSTui/PSTui.History.psm1 b/src/PSTui/PSTui.History.psm1 index f1427a7..fab8754 100644 --- a/src/PSTui/PSTui.History.psm1 +++ b/src/PSTui/PSTui.History.psm1 @@ -68,17 +68,18 @@ function Show-PSTuiHistory { $selection = $history | Out-ConsoleGridView -OutputMode Single -Title $title -Filter $line - # Replace the current line with the selection (if any). + # Re-anchor PSReadLine's prompt at the *current* cursor row before touching + # the buffer. Under Terminal.Gui v2, Out-ConsoleGridView renders inline by + # default, so the screen has scrolled and PSReadLine's saved prompt row + # (_initialY) is now stale; a plain DeleteLine/Render would repaint the + # prompt in the wrong place (the F7History bug fixed in tui-cs/F7History#25). + # Passing CursorTop as the arg makes InvokePrompt re-anchor on this row. + [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt($null, [Console]::CursorTop) + + # Replace the typed prefix (used above as the filter) with the selection. [Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine() if ($selection) { - $command = $selection.CommandLine - [Microsoft.PowerShell.PSConsoleReadLine]::Insert($command) - if ($command.StartsWith($line)) { - [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor) - } - else { - [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($command.Length) - } + [Microsoft.PowerShell.PSConsoleReadLine]::Insert($selection.CommandLine) } }