Fix progress rendering bug

This commit is contained in:
Patrik Svensson 2020-12-04 10:11:12 +01:00 committed by Patrik Svensson
parent ae32785f21
commit 3c504155bc
12 changed files with 89 additions and 4 deletions

View File

@ -0,0 +1,5 @@
foo ━━━ 0% -:--:-- ⣷
bar ━━━ 0% -:--:-- ⣷
baz ━━━ 0% -:--:-- ⣷

View File

@ -0,0 +1,17 @@
namespace Spectre.Console.Tests
{
public sealed class DummyCursor : IAnsiConsoleCursor
{
public void Move(CursorDirection direction, int steps)
{
}
public void SetPosition(int column, int line)
{
}
public void Show(bool show)
{
}
}
}

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Tests
{ {
public Capabilities Capabilities { get; } public Capabilities Capabilities { get; }
public Encoding Encoding { get; } public Encoding Encoding { get; }
public IAnsiConsoleCursor Cursor => throw new NotSupportedException(); public IAnsiConsoleCursor Cursor => new DummyCursor();
public TestableConsoleInput Input { get; } public TestableConsoleInput Input { get; }
public int Width { get; } public int Width { get; }

View File

@ -1,4 +1,4 @@
namespace Spectre.Console.Tests.Tools namespace Spectre.Console.Tests
{ {
public sealed class TestLinkIdentityGenerator : ILinkIdentityGenerator public sealed class TestLinkIdentityGenerator : ILinkIdentityGenerator
{ {

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
using Spectre.Console.Tests.Tools;
namespace Spectre.Console.Tests namespace Spectre.Console.Tests
{ {

View File

@ -1,8 +1,11 @@
using System.Threading.Tasks;
using Shouldly; using Shouldly;
using VerifyXunit;
using Xunit; using Xunit;
namespace Spectre.Console.Tests.Unit namespace Spectre.Console.Tests.Unit
{ {
[UsesVerify]
public sealed class ProgressTests public sealed class ProgressTests
{ {
[Fact] [Fact]
@ -54,5 +57,35 @@ namespace Spectre.Console.Tests.Unit
" \n" + // Bottom padding " \n" + // Bottom padding
"[?25h"); // show cursor "[?25h"); // show cursor
} }
[Fact]
public Task Foo()
{
// Given
var console = new PlainConsole(width: 20);
var progress = new Progress(console)
.Columns(new ProgressColumn[]
{
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
new RemainingTimeColumn(),
new SpinnerColumn(),
})
.AutoRefresh(false)
.AutoClear(false);
// When
progress.Start(ctx =>
{
ctx.AddTask("foo");
ctx.AddTask("bar");
ctx.AddTask("baz");
});
// Then
return Verifier.Verify(console.Output);
}
} }
} }

View File

@ -8,6 +8,12 @@ namespace Spectre.Console
/// </summary> /// </summary>
public sealed class RemainingTimeColumn : ProgressColumn public sealed class RemainingTimeColumn : ProgressColumn
{ {
/// <inheritdoc/>
protected internal override int? ColumnWidth => 7;
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <summary> /// <summary>
/// Gets or sets the style of the remaining time text. /// Gets or sets the style of the remaining time text.
/// </summary> /// </summary>

View File

@ -14,6 +14,12 @@ namespace Spectre.Console
private readonly string _ansiSequence = "⣷⣯⣟⡿⢿⣻⣽⣾"; private readonly string _ansiSequence = "⣷⣯⣟⡿⢿⣻⣽⣾";
private readonly string _asciiSequence = "-\\|/-\\|/"; private readonly string _asciiSequence = "-\\|/-\\|/";
/// <inheritdoc/>
protected internal override int? ColumnWidth => 1;
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <summary> /// <summary>
/// Gets or sets the style of the spinner. /// Gets or sets the style of the spinner.
/// </summary> /// </summary>

View File

@ -8,11 +8,14 @@ namespace Spectre.Console
/// </summary> /// </summary>
public sealed class TaskDescriptionColumn : ProgressColumn public sealed class TaskDescriptionColumn : ProgressColumn
{ {
/// <inheritdoc/>
protected internal override bool NoWrap => true;
/// <inheritdoc/> /// <inheritdoc/>
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime) public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{ {
var text = task.Description?.RemoveNewLines()?.Trim(); var text = task.Description?.RemoveNewLines()?.Trim();
return new Markup(text ?? string.Empty).RightAligned(); return new Markup(text ?? string.Empty).Overflow(Overflow.Ellipsis).RightAligned();
} }
} }
} }

View File

@ -8,6 +8,11 @@ namespace Spectre.Console
/// </summary> /// </summary>
public abstract class ProgressColumn public abstract class ProgressColumn
{ {
/// <summary>
/// Gets a value indicating whether or not content should not wrap.
/// </summary>
protected internal virtual bool NoWrap { get; }
/// <summary> /// <summary>
/// Gets the requested column width for the column. /// Gets the requested column width for the column.
/// </summary> /// </summary>

View File

@ -65,11 +65,17 @@ namespace Spectre.Console.Internal
for (var columnIndex = 0; columnIndex < _columns.Count; columnIndex++) for (var columnIndex = 0; columnIndex < _columns.Count; columnIndex++)
{ {
var column = new GridColumn().PadRight(1); var column = new GridColumn().PadRight(1);
if (_columns[columnIndex].ColumnWidth != null) if (_columns[columnIndex].ColumnWidth != null)
{ {
column.Width = _columns[columnIndex].ColumnWidth; column.Width = _columns[columnIndex].ColumnWidth;
} }
if (_columns[columnIndex].NoWrap)
{
column.NoWrap();
}
// Last column? // Last column?
if (columnIndex == _columns.Count - 1) if (columnIndex == _columns.Count - 1)
{ {

View File

@ -58,6 +58,11 @@ namespace Spectre.Console
var width = childWidth + paddingWidth; var width = childWidth + paddingWidth;
var result = new List<Segment>(); var result = new List<Segment>();
if (width > maxWidth)
{
width = maxWidth;
}
// Top padding // Top padding
for (var i = 0; i < Padding.GetTopSafe(); i++) for (var i = 0; i < Padding.GetTopSafe(); i++)
{ {