Add progress task list support

This commit is contained in:
Patrik Svensson
2020-11-27 08:12:29 +01:00
committed by Patrik Svensson
parent c61e386440
commit ae32785f21
71 changed files with 2350 additions and 106 deletions

View File

@@ -0,0 +1 @@
━━━━━━━━━━━━━━━━━━━━

View File

@@ -18,6 +18,7 @@ namespace Spectre.Console.Tests
public int Height { get; }
IAnsiConsoleInput IAnsiConsole.Input => Input;
public RenderPipeline Pipeline { get; }
public Decoration Decoration { get; set; }
public Color Foreground { get; set; }
@@ -31,14 +32,15 @@ namespace Spectre.Console.Tests
public PlainConsole(
int width = 80, int height = 9000, Encoding encoding = null,
bool supportsAnsi = true, ColorSystem colorSystem = ColorSystem.Standard,
bool legacyConsole = false)
bool legacyConsole = false, bool interactive = true)
{
Capabilities = new Capabilities(supportsAnsi, colorSystem, legacyConsole);
Capabilities = new Capabilities(supportsAnsi, colorSystem, legacyConsole, interactive);
Encoding = encoding ?? Encoding.UTF8;
Width = width;
Height = height;
Writer = new StringWriter();
Input = new TestableConsoleInput();
Pipeline = new RenderPipeline();
}
public void Dispose()
@@ -50,14 +52,17 @@ namespace Spectre.Console.Tests
{
}
public void Write(Segment segment)
public void Write(IEnumerable<Segment> segments)
{
if (segment is null)
if (segments is null)
{
throw new ArgumentNullException(nameof(segment));
return;
}
Writer.Write(segment.Text);
foreach (var segment in segments)
{
Writer.Write(segment.Text);
}
}
public string WriteNormalizedException(Exception ex, ExceptionFormats formats = ExceptionFormats.Default)

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Spectre.Console.Rendering;
@@ -19,16 +20,21 @@ namespace Spectre.Console.Tests
public int Height => _console.Height;
public IAnsiConsoleCursor Cursor => _console.Cursor;
public TestableConsoleInput Input { get; }
public RenderPipeline Pipeline => _console.Pipeline;
IAnsiConsoleInput IAnsiConsole.Input => Input;
public TestableAnsiConsole(ColorSystem system, AnsiSupport ansi = AnsiSupport.Yes, int width = 80)
public TestableAnsiConsole(
ColorSystem system, AnsiSupport ansi = AnsiSupport.Yes,
InteractionSupport interaction = InteractionSupport.Yes,
int width = 80)
{
_writer = new StringWriter();
_console = AnsiConsole.Create(new AnsiConsoleSettings
{
Ansi = ansi,
ColorSystem = (ColorSystemSupport)system,
Interactive = interaction,
Out = _writer,
LinkIdentityGenerator = new TestLinkIdentityGenerator(),
});
@@ -47,9 +53,17 @@ namespace Spectre.Console.Tests
_console.Clear(home);
}
public void Write(Segment segment)
public void Write(IEnumerable<Segment> segments)
{
_console.Write(segment);
if (segments is null)
{
return;
}
foreach (var segment in segments)
{
_console.Write(segment);
}
}
}
}

View File

@@ -0,0 +1,58 @@
using Shouldly;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
public sealed class ProgressTests
{
[Fact]
public void Should_Render_Task_Correctly()
{
// Given
var console = new TestableAnsiConsole(ColorSystem.TrueColor, width: 10);
var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false)
.AutoClear(true);
// When
progress.Start(ctx => ctx.AddTask("foo"));
// Then
console.Output
.NormalizeLineEndings()
.ShouldBe(
"[?25l" + // Hide cursor
" \n" + // Top padding
"━━━━━━━━━━\n" + // Task
" " + // Bottom padding
"[?25h"); // Clear + show cursor
}
[Fact]
public void Should_Not_Auto_Clear_If_Specified()
{
// Given
var console = new TestableAnsiConsole(ColorSystem.TrueColor, width: 10);
var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false)
.AutoClear(false);
// When
progress.Start(ctx => ctx.AddTask("foo"));
// Then
console.Output
.NormalizeLineEndings()
.ShouldBe(
"[?25l" + // Hide cursor
" \n" + // Top padding
"━━━━━━━━━━\n" + // Task
" \n" + // Bottom padding
"[?25h"); // show cursor
}
}
}

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using Shouldly;
using Spectre.Console.Rendering;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
public sealed class RenderHookTests
{
private sealed class HelloRenderHook : IRenderHook
{
public IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
{
return new IRenderable[] { new Text("Hello\n") }.Concat(renderables);
}
}
[Fact]
public void Should_Inject_Renderable_Before_Writing_To_Console()
{
// Given
var console = new PlainConsole();
console.Pipeline.Attach(new HelloRenderHook());
// When
console.Render(new Text("World"));
// Then
console.Lines[0].ShouldBe("Hello");
console.Lines[1].ShouldBe("World");
}
}
}