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,25 @@
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class ControlSequence : Renderable
{
private readonly Segment _segment;
public ControlSequence(string control)
{
_segment = Segment.Control(control);
}
protected override Measurement Measure(RenderContext context, int maxWidth)
{
return new Measurement(0, 0);
}
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
yield return _segment;
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
internal sealed class ProgressBar : Renderable
{
public double Value { get; set; }
public double MaxValue { get; set; } = 100;
public int? Width { get; set; }
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
protected override Measurement Measure(RenderContext context, int maxWidth)
{
var width = Math.Min(Width ?? maxWidth, maxWidth);
return new Measurement(4, width);
}
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
var width = Math.Min(Width ?? maxWidth, maxWidth);
var completed = Math.Min(MaxValue, Math.Max(0, Value));
var token = !context.Unicode || context.LegacyConsole ? '-' : '━';
var style = completed >= MaxValue ? FinishedStyle : CompletedStyle;
var bars = Math.Max(0, (int)(width * (completed / MaxValue)));
yield return new Segment(new string(token, bars), style);
if (bars < width)
{
yield return new Segment(new string(token, width - bars), RemainingStyle);
}
}
}
}

View File

@ -95,7 +95,7 @@ namespace Spectre.Console
private IEnumerable<Segment> GetTitleSegments(RenderContext context, string title, int width)
{
title = title.NormalizeLineEndings().ReplaceExact("\n", " ").Trim();
title = title.NormalizeNewLines().ReplaceExact("\n", " ").Trim();
var markup = new Markup(title, Style);
return ((IRenderable)markup).Render(context.WithSingleLine(), width);
}

View File

@ -16,8 +16,8 @@ namespace Spectre.Console
private readonly List<TableColumn> _columns;
private readonly List<TableRow> _rows;
private static Style _defaultHeadingStyle = new Style(Color.Silver);
private static Style _defaultCaptionStyle = new Style(Color.Grey);
private static readonly Style _defaultHeadingStyle = new Style(Color.Silver);
private static readonly Style _defaultCaptionStyle = new Style(Color.Grey);
/// <summary>
/// Gets the table columns.
@ -447,12 +447,10 @@ namespace Spectre.Console
private (int Min, int Max) MeasureColumn(TableColumn column, RenderContext options, int maxWidth)
{
var padding = column.Padding?.GetWidth() ?? 0;
// Predetermined width?
if (column.Width != null)
{
return (column.Width.Value + padding, column.Width.Value + padding);
return (column.Width.Value, column.Width.Value);
}
var columnIndex = _columns.IndexOf(column);
@ -474,6 +472,8 @@ namespace Spectre.Console
maxWidths.Add(rowMeasure.Max);
}
var padding = column.Padding?.GetWidth() ?? 0;
return (minWidths.Count > 0 ? minWidths.Max() : padding,
maxWidths.Count > 0 ? maxWidths.Max() : maxWidth);
}