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

@ -52,8 +52,7 @@ namespace Spectre.Console
/// <param name="args">An array of objects to write.</param>
public static void MarkupLine(this IAnsiConsole console, IFormatProvider provider, string format, params object[] args)
{
Markup(console, provider, format, args);
console.WriteLine();
Markup(console, provider, format + Environment.NewLine, args);
}
}
}

View File

@ -0,0 +1,25 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="IAnsiConsole"/>.
/// </summary>
public static partial class AnsiConsoleExtensions
{
/// <summary>
/// Creates a new <see cref="Progress"/> instance for the console.
/// </summary>
/// <param name="console">The console.</param>
/// <returns>A <see cref="Progress"/> instance.</returns>
public static Progress Progress(this IAnsiConsole console)
{
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}
return new Progress(console);
}
}
}

View File

@ -1,5 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
@ -26,19 +26,26 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(renderable));
}
var options = new RenderContext(console.Encoding, console.Capabilities.LegacyConsole);
var segments = renderable.Render(options, console.Width).ToArray();
segments = Segment.Merge(segments).ToArray();
var context = new RenderContext(console.Encoding, console.Capabilities.LegacyConsole);
var renderables = console.Pipeline.Process(context, new[] { renderable });
foreach (var segment in segments)
Render(console, context, renderables);
}
private static void Render(IAnsiConsole console, RenderContext options, IEnumerable<IRenderable> renderables)
{
if (renderables is null)
{
if (string.IsNullOrEmpty(segment.Text))
{
continue;
}
console.Write(segment.Text, segment.Style);
return;
}
var result = new List<Segment>();
foreach (var renderable in renderables)
{
result.AddRange(renderable.Render(options, console.Width));
}
console.Write(Segment.Merge(result));
}
}
}

View File

@ -18,6 +18,26 @@ namespace Spectre.Console
return new Recorder(console);
}
/// <summary>
/// Writes the specified string value to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="segment">The segment to write.</param>
public static void Write(this IAnsiConsole console, Segment segment)
{
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}
if (segment is null)
{
throw new ArgumentNullException(nameof(segment));
}
console.Write(new[] { segment });
}
/// <summary>
/// Writes the specified string value to the console.
/// </summary>
@ -25,7 +45,7 @@ namespace Spectre.Console
/// <param name="text">The text to write.</param>
public static void Write(this IAnsiConsole console, string text)
{
Write(console, text, Style.Plain);
Render(console, new Text(text, Style.Plain));
}
/// <summary>
@ -36,17 +56,7 @@ namespace Spectre.Console
/// <param name="style">The text style.</param>
public static void Write(this IAnsiConsole console, string text, Style style)
{
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
console.Write(new Segment(text, style));
Render(console, new Text(text, style));
}
/// <summary>
@ -60,7 +70,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(console));
}
console.Write(Environment.NewLine, Style.Plain);
Render(console, new Text(Environment.NewLine, Style.Plain));
}
/// <summary>
@ -91,8 +101,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(text));
}
console.Write(new Segment(text, style));
console.WriteLine();
console.Write(text + Environment.NewLine, style);
}
}
}

View File

@ -0,0 +1,54 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="PercentageColumn"/>.
/// </summary>
public static class PercentageColumnExtensions
{
/// <summary>
/// Sets the style for a non-complete task.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static PercentageColumn Style(this PercentageColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.Style = style;
return column;
}
/// <summary>
/// Sets the style for a completed task.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static PercentageColumn CompletedStyle(this PercentageColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.CompletedStyle = style;
return column;
}
}
}

View File

@ -0,0 +1,76 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="ProgressBarColumn"/>.
/// </summary>
public static class ProgressBarColumnExtensions
{
/// <summary>
/// Sets the style of completed portions of the progress bar.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ProgressBarColumn CompletedStyle(this ProgressBarColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.CompletedStyle = style;
return column;
}
/// <summary>
/// Sets the style of a finished progress bar.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ProgressBarColumn FinishedStyle(this ProgressBarColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.FinishedStyle = style;
return column;
}
/// <summary>
/// Sets the style of remaining portions of the progress bar.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ProgressBarColumn RemainingStyle(this ProgressBarColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.RemainingStyle = style;
return column;
}
}
}

View File

@ -0,0 +1,32 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="RemainingTimeColumn"/>.
/// </summary>
public static class RemainingTimeColumnExtensions
{
/// <summary>
/// Sets the style of the remaining time text.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static RemainingTimeColumn Style(this RemainingTimeColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.Style = style;
return column;
}
}
}

View File

@ -0,0 +1,32 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="SpinnerColumn"/>.
/// </summary>
public static class SpinnerColumnExtensions
{
/// <summary>
/// Sets the style of the spinner.
/// </summary>
/// <param name="column">The column.</param>
/// <param name="style">The style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static SpinnerColumn Style(this SpinnerColumn column, Style style)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
column.Style = style;
return column;
}
}
}

View File

@ -27,7 +27,7 @@ namespace Spectre.Console
}
alignment ??= panel.Header?.Alignment;
return Header(panel, new PanelHeader(text, alignment));
return Header(panel, new PanelHeader(text, alignment));
}
/// <summary>

View File

@ -0,0 +1,79 @@
using System;
using System.Linq;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="Progress"/>.
/// </summary>
public static class ProgressExtensions
{
/// <summary>
/// Sets the columns to be used for an <see cref="Progress"/> instance.
/// </summary>
/// <param name="progress">The <see cref="Progress"/> instance.</param>
/// <param name="columns">The columns to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Progress Columns(this Progress progress, ProgressColumn[] columns)
{
if (progress is null)
{
throw new ArgumentNullException(nameof(progress));
}
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
if (!columns.Any())
{
throw new InvalidOperationException("At least one column must be specified.");
}
progress.Columns.Clear();
progress.Columns.AddRange(columns);
return progress;
}
/// <summary>
/// Sets whether or not auto refresh is enabled.
/// If disabled, you will manually have to refresh the progress.
/// </summary>
/// <param name="progress">The <see cref="Progress"/> instance.</param>
/// <param name="enabled">Whether or not auto refresh is enabled.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Progress AutoRefresh(this Progress progress, bool enabled)
{
if (progress is null)
{
throw new ArgumentNullException(nameof(progress));
}
progress.AutoRefresh = enabled;
return progress;
}
/// <summary>
/// Sets whether or not auto clear is enabled.
/// If enabled, the task tabled will be removed once
/// all tasks have completed.
/// </summary>
/// <param name="progress">The <see cref="Progress"/> instance.</param>
/// <param name="enabled">Whether or not auto clear is enabled.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Progress AutoClear(this Progress progress, bool enabled)
{
if (progress is null)
{
throw new ArgumentNullException(nameof(progress));
}
progress.AutoClear = enabled;
return progress;
}
}
}

View File

@ -0,0 +1,44 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="ProgressTask"/>.
/// </summary>
public static class ProgressTaskExtensions
{
/// <summary>
/// Sets the task description.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="description">The description.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ProgressTask Description(this ProgressTask task, string description)
{
if (task is null)
{
throw new ArgumentNullException(nameof(task));
}
task.Description = description;
return task;
}
/// <summary>
/// Sets the max value of the task.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="value">The max value.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ProgressTask MaxValue(this ProgressTask task, double value)
{
if (task is null)
{
throw new ArgumentNullException(nameof(task));
}
task.MaxValue = value;
return task;
}
}
}

View File

@ -1,7 +1,7 @@
using System.Globalization;
using System.Text;
namespace Spectre.Console
namespace Spectre.Console.Internal
{
internal static class StringBuilderExtensions
{

View File

@ -62,10 +62,15 @@ namespace Spectre.Console
return text;
}
internal static string NormalizeLineEndings(this string? text, bool native = false)
internal static string? RemoveNewLines(this string? text)
{
return text?.ReplaceExact("\r\n", string.Empty)
?.ReplaceExact("\n", string.Empty);
}
internal static string NormalizeNewLines(this string? text, bool native = false)
{
text = text?.ReplaceExact("\r\n", "\n");
text = text?.ReplaceExact("\r", string.Empty);
text ??= string.Empty;
if (native && !_alreadyNormalized)
@ -78,7 +83,7 @@ namespace Spectre.Console
internal static string[] SplitLines(this string text)
{
var result = text?.NormalizeLineEndings()?.Split(new[] { '\n' }, StringSplitOptions.None);
var result = text?.NormalizeNewLines()?.Split(new[] { '\n' }, StringSplitOptions.None);
return result ?? Array.Empty<string>();
}