Skip to content

Commit

Permalink
Adds option to configure the Foreground Done Color (#97)
Browse files Browse the repository at this point in the history
Co-authored-by: Martijn Laarman <[email protected]>
  • Loading branch information
gfs and Mpdreamz authored Jun 7, 2022
1 parent 3f819bc commit a008b50
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class AlternateFinishedColorExample : ExampleBase
{
protected override void Start()
{
var options = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ForegroundColorDone = ConsoleColor.DarkGreen,
ForegroundColorError = ConsoleColor.Red,
BackgroundColor = ConsoleColor.DarkGray,
BackgroundCharacter = '\u2593'
};

using (var pbar = new ProgressBar(100, "100 ticks", options))
{
Task.Run(
() =>
{
for (var i = 0; i < 10; i++)
{
Task.Delay(10).Wait();
pbar.Tick($"Step {i}");
}
pbar.WriteErrorLine("The task ran into an issue!");
// OR pbar.ObservedError = true;
}).Wait();
pbar.Message= "Indicate the task is done, but the status is not Green.";
}

Task.Delay(5000).Wait();
}


}
}
6 changes: 5 additions & 1 deletion src/ShellProgressBar.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Program
{
private static readonly IList<IProgressBarExample> TestCases = new List<IProgressBarExample>
{
/*
new PersistMessageExample(),
new FixedDurationExample(),
new DeeplyNestedProgressBarTreeExample(),
Expand All @@ -28,6 +29,8 @@ class Program
new EstimatedDurationExample(),
new IndeterminateProgressExample(),
new IndeterminateChildrenNoCollapseExample(),
*/
new AlternateFinishedColorExample()
};

private static readonly IList<IProgressBarExample> Examples = new List<IProgressBarExample>
Expand All @@ -42,7 +45,8 @@ class Program
new MessageBeforeAndAfterExample(),
new DeeplyNestedProgressBarTreeExample(),
new EstimatedDurationExample(),
new DownloadProgressExample()
new DownloadProgressExample(),
new AlternateFinishedColorExample()
};

static void Main(string[] args)
Expand Down
1 change: 1 addition & 0 deletions src/ShellProgressBar/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public override void WriteLine(string message)
}
public override void WriteErrorLine(string message)
{
this.ObservedError = true;
_stickyMessages.Enqueue(new ConsoleOutLine(message, error: true));
DisplayProgress();
}
Expand Down
5 changes: 5 additions & 0 deletions src/ShellProgressBar/ProgressBarBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ protected virtual void OnDone() { }

public DateTime? EndTime { get; protected set; }

public bool ObservedError { get; set; }

private ConsoleColor? _dynamicForegroundColor = null;
public ConsoleColor ForegroundColor
{
get
{
var realColor = _dynamicForegroundColor ?? this.Options.ForegroundColor;
if (this.ObservedError && this.Options.ForegroundColorError.HasValue)
return this.Options.ForegroundColorError.Value;

return EndTime.HasValue
? this.Options.ForegroundColorDone ?? realColor
: realColor;
Expand Down
8 changes: 8 additions & 0 deletions src/ShellProgressBar/ProgressBarOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public string MessageEncodingName
/// <summary> The foreground color the progressbar has reached a 100 percent</summary>
public ConsoleColor? ForegroundColorDone { get; set; }

/// <summary>
/// The foreground color the progressbar when it has observed an error
/// <para>If set this takes priority over any other color as soon as an error is observed</para>
/// Use either <see cref="ProgressBarBase.ObservedError"/> or <see cref="ProgressBarBase.WriteErrorLine"/> to
/// put the progressbar in errored state
/// </summary>
public ConsoleColor? ForegroundColorError { get; set; }

/// <summary> The background color of the remainder of the progressbar</summary>
public ConsoleColor? BackgroundColor { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/ShellProgressBar/ShellProgressBar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageTags>console;shell;terminal;progress;progressbar</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.0">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit a008b50

Please sign in to comment.