From 6c262d82eb8d510f8756486d4cc292f7bd1f1882 Mon Sep 17 00:00:00 2001 From: Jianyun Tao Date: Thu, 7 Jun 2018 16:22:30 -0700 Subject: [PATCH 1/2] Set the cursor back to the original cmdline once the write-progress completes. --- .../host/msh/ConsoleHostUserInterfaceProgress.cs | 6 ++++++ .../host/msh/ProgressPane.cs | 16 ++++++++++++---- .../engine/MshCommandRuntime.cs | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index d8b19a68812..d0819f6ea2a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -97,6 +97,12 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt // Update the progress pane only when the timer set up the update flag or WriteProgress is completed. // As a result, we do not block WriteProgress and whole script and eliminate unnecessary console locks and updates. _progPane.Show(_pendingProgress); + + // Reset the cursor back to where it started + if(record.RecordType == ProgressRecordType.Completed) + { + _progPane.Hide(); + } } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs index 922aad8f174..d8346b0cbd6 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs @@ -99,12 +99,20 @@ class ProgressPane //if the cursor is at the bottom, create screen buffer space by scrolling int scrollRows = rows - ((_rawui.BufferSize.Height - 1) - _location.Y); - for (int i = 0; i < rows; i++) - { - Console.Out.Write('\n'); - } if (scrollRows > 0) { + // The following can be possibly replaced by Console.Write ("\x1b[" + scrollRows + "S"); + // For details, see https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences + + // Scroll the console screen up by 'scrollRows' + var bottomLocation = _location; + bottomLocation.Y = _rawui.BufferSize.Height; + _rawui.CursorPosition = bottomLocation; + for (int i = 0; i < scrollRows; i++) + { + Console.Out.Write('\n'); + } + _location.Y -= scrollRows; _savedCursor.Y -= scrollRows; } diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index 90ddd5927a3..54a98fc0ce9 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -2252,7 +2252,7 @@ internal void ThrowIfWriteNotPermitted(bool needsToWriteToPipeline) { // Only generate these exceptions if a pipeline has already been declared as the 'writing' pipeline. // Otherwise, these are probably infrastructure messages and can be ignored. - if (this.PipelineProcessor._permittedToWrite != null) + if (this.PipelineProcessor?._permittedToWrite != null) { throw PSTraceSource.NewInvalidOperationException( PipelineStrings.WriteNotPermitted); From 74ee9c8ba0382269607d9cfb01a4bea2db774b57 Mon Sep 17 00:00:00 2001 From: Jianyun Tao Date: Fri, 8 Jun 2018 11:54:09 -0700 Subject: [PATCH 2/2] add a space after if --- .../host/msh/ConsoleHostUserInterfaceProgress.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index d0819f6ea2a..5e182b3e582 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -99,7 +99,7 @@ class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInt _progPane.Show(_pendingProgress); // Reset the cursor back to where it started - if(record.RecordType == ProgressRecordType.Completed) + if (record.RecordType == ProgressRecordType.Completed) { _progPane.Hide(); }