From e02b0da57a2bf1b4a27f9e2fad2017480dff7af5 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 4 Nov 2021 15:44:16 -0700 Subject: [PATCH] Handle screen buffer scrolling correctly for inline dynamic help (#2951) --- PSReadLine/DynamicHelp.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/PSReadLine/DynamicHelp.cs b/PSReadLine/DynamicHelp.cs index 814a6d0..9465768 100644 --- a/PSReadLine/DynamicHelp.cs +++ b/PSReadLine/DynamicHelp.cs @@ -233,13 +233,14 @@ namespace Microsoft.PowerShell { internal Collection ItemsToDisplay; - private int multilineItems = 0; + // Keep track of the number of extra physical lines due to multi-line text. + private int extraPhysicalLines = 0; public void DrawMultilineBlock() { IConsole console = Singleton._console; - multilineItems = 0; + extraPhysicalLines = 0; SaveCursor(); MoveCursorToStartDrawingPosition(console); @@ -251,16 +252,23 @@ namespace Microsoft.PowerShell { var itemLength = LengthInBufferCells(items[index]); + int extra = 0; if (itemLength > bufferWidth) { - multilineItems += itemLength / bufferWidth; - + extra = itemLength / bufferWidth; if (itemLength % bufferWidth == 0) { - multilineItems--; + extra--; } } + if (extra > 0) + { + // Extra physical lines may cause buffer to scroll up. + AdjustForPossibleScroll(extra); + extraPhysicalLines += extra; + } + console.Write(items[index]); // Explicit newline so consoles see each row as distinct lines, but skip the @@ -277,7 +285,7 @@ namespace Microsoft.PowerShell public void Clear() { - _singleton.WriteBlankLines(Top, ItemsToDisplay.Count + multilineItems); + _singleton.WriteBlankLines(Top, ItemsToDisplay.Count + extraPhysicalLines); } } }