8000 Add minimal progress bar using ANSI rendering by SteveL-MSFT · Pull Request #14414 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Add minimal progress bar using ANSI rendering #14414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
37f0719
Add simpler progress bar using ANSI rendering
SteveL-MSFT Dec 13, 2020
bd050c6
fix rendering of progress bar
SteveL-MSFT Dec 14, 2020
e4fec68
remove unused code, limit progress to 120 chars
SteveL-MSFT Dec 14, 2020
b91b75e
fix codefactor issues
SteveL-MSFT Dec 14, 2020
3c992bc
restore cursor visibility
SteveL-MSFT Dec 14, 2020
c98e865
Fix initial rendering issue to take into account length of Reverse es…
SteveL-MSFT Dec 14, 2020
7f18605
Support configuration for progress
SteveL-MSFT Jan 9, 2021
6c3ae08
Fix rendering when output is written
SteveL-MSFT Jan 29, 2021
5e9c7bf
remove unnecessary duplicated scrolling to create space
SteveL-MSFT Jan 29, 2021
20aa59f
Fix cleanup by not unnecessarily using buffercells but instead writin…
SteveL-MSFT Jan 29, 2021
3e06cdc
remove unnecessary parenthesis
SteveL-MSFT Jan 29, 2021
a09dc88
correctly handle when Progress.MaxWidth > Console.WindowWidth
SteveL-MSFT Jan 29, 2021
f693855
fix cleanup when done rendering
SteveL-MSFT Jan 29, 2021
c0033bc
address Codefactor
SteveL-MSFT Jan 29, 2021
60c50ba
Remove unnecessary check
SteveL-MSFT Jan 29, 2021
5b34d26
address Rob's feedback
SteveL-MSFT Feb 2, 2021
e485762
Update src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs
SteveL-MSFT Feb 4, 2021
635476f
Update src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs
SteveL-MSFT Feb 4, 2021
69c36af
Update src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs
SteveL-MSFT Feb 4, 2021
13e5c29
address Ilya's feedback
SteveL-MSFT Feb 4, 2021
b05f249
address Dongbo's feedback
SteveL-MSFT Feb 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,11 @@ internal override
Visit(ProgressNode node, ArrayList unused, int unusedToo)
{
node.Age = Math.Min(node.Age + 1, Int32.MaxValue - 1);
node.Style = ProgressNode.RenderStyle.FullPlus;

node.Style = ProgressNode.IsMinimalProgressRenderingEnabled()
? ProgressNode.RenderStyle.Ansi
: node.Style = ProgressNode.RenderStyle.FullPlus;

return true;
}
}
Expand Down Expand Up @@ -582,6 +586,13 @@ internal override
}

ArrayList result = new ArrayList();

if (ProgressNode.IsMinimalProgressRenderingEnabled())
{
RenderHelper(result, _topLevelNodes, indentation: 0, maxWidth, rawUI);
return (string[])result.ToArray(typeof(string));
}

string border = StringUtil.Padding(maxWidth);

result.Add(border);
Expand Down
104 changes: 102 additions & 2 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Text;

using Microsoft.PowerShell.Commands.Internal.Format;

using Dbg = System.Management.Automation.Diagnostics;

Expand All @@ -20,7 +23,7 @@ namespace Microso 8000 ft.PowerShell
ProgressNode : ProgressRecord
{
/// <summary>
/// Indicates the various layouts for rendering a particular node. Each style is progressively less terse.
/// Indicates the various layouts for rendering a particular node.
/// </summary>
internal
enum
Expand All @@ -40,6 +43,11 @@ namespace Microsoft.PowerShell
/// The node will be displayed the same as Full, plus, the whole StatusDescription and CurrentOperation will be displayed (in multiple lines if needed).
/// </summary>
FullPlus = 4,

/// <summary>
/// The node will be displayed using ANSI escape sequences.
/// </summary>
Ansi = 5,
}

/// <summary>
Expand All @@ -56,7 +64,11 @@ namespace Microsoft.PowerShell
this.PercentComplete = Math.Min(record.PercentComplete, 100);
this.SecondsRemaining = record.SecondsRemaining;
this.RecordType = record.RecordType;
this.Style = RenderStyle.FullPlus;

this.Style = IsMinimalProgressRenderingEnabled()
? RenderStyle.Ansi
: this.Style = RenderStyle.FullPlus;

this.SourceId = sourceId;
}

Expand Down Expand Up @@ -98,6 +110,9 @@ namespace Microsoft.PowerShell
case RenderStyle.Minimal:
RenderMinimal(strCollection, indentation, maxWidth, rawUI);
break;
case RenderStyle.Ansi:
RenderAnsi(strCollection, indentation, maxWidth);
break;
case RenderStyle.Invisible:
// do nothing
break;
Expand Down Expand Up @@ -336,6 +351,88 @@ private static void RenderFullDescription(string description, string indent, int
maxWidth));
}

internal static bool IsMinimalProgressRenderingEnabled()
{
return ExperimentalFeature.IsEnabled(ExperimentalFeature.PSAnsiProgressFeatureName) && PSStyle.Instance.Progress.View == ProgressView.Minimal;
}

/// <summary>
/// Renders a node in the "ANSI" style.
/// </summary>
/// <param name="strCollection">
/// List of strings to which the node's rendering will be appended.
/// </param>
/// <param name="indentation">
/// The indentation level in chars at which the node should be rendered.
/// </param>
/// <param name="maxWidth">
/// The maximum number of chars that the rendering is allowed to consume.
/// </param>
private
void
RenderAnsi(ArrayList strCollection, int indentation, int maxWidth)
{
string indent = StringUtil.Padding(indentation);
string secRemain = string.Empty;
if (SecondsRemaining >= 0)
{
secRemain = SecondsRemaining.ToString() + "s";
}

int secRemainLength = secRemain.Length + 1;

// limit progress bar to 120 chars as no need to render full width
if (PSStyle.Instance.Progress.MaxWidth > 0 && maxWidth > PSStyle.Instance.Progress.MaxWidth)
{
maxWidth = PSStyle.Instance.Progress.MaxWidth;
}

// 4 is for the extra space and square brackets below and one extra space
int barWidth = maxWidth - Activity.Length - indentation - 4;

var sb = new StringBuilder();
int padding = maxWidth + PSStyle.Instance.Progress.Style.Length + PSStyle.Instance.Reverse.Length + PSStyle.Instance.ReverseOff.Length;
sb.Append(PSStyle.Instance.Reverse);

if (StatusDescription.Length > barWidth - secRemainLength)
{
sb.Append(StatusDescription.Substring(0, barWidth - secRemainLength - 1));
sb.Append(PSObjectHelper.Ellipsis);
}
else
{
sb.Append(StatusDescription);
}

sb.Append(string.Empty.PadRight(barWidth + PSStyle.Instance.Reverse.Length - sb.Length - secRemainLength));
sb.Append(secRemain);

if (PercentComplete > 0 && PercentComplete < 100)
{
int barLength = PercentComplete * barWidth / 100;
if (barLength >= barWidth)
{
barLength = barWidth - 1;
}

sb.Insert(barLength + PSStyle.Instance.Reverse.Length, PSStyle.Instance.ReverseOff);
}
else
{
sb.Append(PSStyle.Instance.ReverseOff);
}

strCollection.Add(
StringUtil.Format(
"{0}{1}{2} [{3}]{4}",
indent,
PSStyle.Instance.Progress.Style,
Activity,
sb.ToString(),
PSStyle.Instance.Reset)
.PadRight(padding));
}

/// <summary>
/// The nodes that have this node as their parent.
/// </summary>
Expand Down Expand Up @@ -396,6 +493,9 @@ internal int LinesRequiredMethod(PSHostRawUserInterface rawUi, int maxWidth)
case RenderStyle.Invisible:
return 0;

case RenderStyle.Ansi:
return 1;

default:
Dbg.Assert(false, "Unknown RenderStyle value");
break;
Expand Down
Loading
0