Add Progress.HideCompleted

This commit is contained in:
stf 2021-03-03 21:58:38 +01:00 committed by Phil Scott
parent 1cd335e785
commit 1c769c6610
4 changed files with 63 additions and 3 deletions

View File

@ -184,5 +184,36 @@ namespace Spectre.Console.Tests.Unit
// Then
task.Value.ShouldBe(60);
}
[Fact]
public void Should_Hide_Completed_Tasks()
{
// Given
var task = default(ProgressTask);
var console = new FakeAnsiConsole(ColorSystem.TrueColor, width: 10);
var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false)
.AutoClear(false)
.HideCompleted(true);
// When
progress.Start(ctx =>
{
task = ctx.AddTask("foo");
task.Value = task.MaxValue;
});
// Then
console.Output
.NormalizeLineEndings()
.ShouldBe(
"[?25l" + // Hide cursor
" \n" + // top padding
"…\n" + // hidden task
" \n" + // bottom padding
"[?25h"); // show cursor
}
}
}

View File

@ -75,5 +75,25 @@ namespace Spectre.Console
return progress;
}
/// <summary>
/// Sets whether or not hide completed is enabled.
/// If enabled, the task tabled will be removed once it is
/// completed.
/// </summary>
/// <param name="progress">The <see cref="Progress"/> instance.</param>
/// <param name="enabled">Whether or not hide completed is enabled.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Progress HideCompleted(this Progress progress, bool enabled)
{
if (progress is null)
{
throw new ArgumentNullException(nameof(progress));
}
progress.HideCompleted = enabled;
return progress;
}
}
}

View File

@ -25,6 +25,13 @@ namespace Spectre.Console
/// </summary>
public bool AutoClear { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not the task list should
/// only include tasks not completed
/// Defaults to <c>false</c>.
/// </summary>
public bool HideCompleted { get; set; }
/// <summary>
/// Gets or sets the refresh rate if <c>AutoRefresh</c> is enabled.
/// Defaults to 10 times/second.
@ -153,7 +160,7 @@ namespace Spectre.Console
if (interactive)
{
var columns = new List<ProgressColumn>(Columns);
return new DefaultProgressRenderer(_console, columns, RefreshRate);
return new DefaultProgressRenderer(_console, columns, RefreshRate, HideCompleted);
}
else
{

View File

@ -14,10 +14,11 @@ namespace Spectre.Console
private readonly object _lock;
private readonly Stopwatch _stopwatch;
private TimeSpan _lastUpdate;
private bool _hideCompleted;
public override TimeSpan RefreshRate { get; }
public DefaultProgressRenderer(IAnsiConsole console, List<ProgressColumn> columns, TimeSpan refreshRate)
public DefaultProgressRenderer(IAnsiConsole console, List<ProgressColumn> columns, TimeSpan refreshRate, bool hideCompleted)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_columns = columns ?? throw new ArgumentNullException(nameof(columns));
@ -25,6 +26,7 @@ namespace Spectre.Console
_lock = new object();
_stopwatch = new Stopwatch();
_lastUpdate = TimeSpan.Zero;
_hideCompleted = hideCompleted;
RefreshRate = refreshRate;
}
@ -91,7 +93,7 @@ namespace Spectre.Console
}
// Add rows
foreach (var task in context.GetTasks())
foreach (var task in context.GetTasks().Where(tsk => !(_hideCompleted && tsk.IsFinished)))
{
var columns = _columns.Select(column => column.Render(renderContext, task, delta));
grid.AddRow(columns.ToArray());