mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
parent
b64e016e8c
commit
705cf745ea
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<ExampleTitle>Canvas</ExampleTitle>
|
<ExampleTitle>Canvas</ExampleTitle>
|
||||||
<ExampleDescription>Demonstrates how to render pixels and images.</ExampleDescription>
|
<ExampleDescription>Demonstrates how to render pixels and images.</ExampleDescription>
|
||||||
<ExampleGroup>Widgets</ExampleGroup>
|
<ExampleGroup>Widgets</ExampleGroup>
|
||||||
|
@ -22,7 +22,7 @@ namespace Charts
|
|||||||
Render("Languages", new BreakdownChart()
|
Render("Languages", new BreakdownChart()
|
||||||
.FullSize()
|
.FullSize()
|
||||||
.Width(60)
|
.Width(60)
|
||||||
.ShowAsPercentages()
|
.TagValueFormat("{0}%")
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
.AddItem("SCSS", 37, Color.Red)
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
.AddItem("HTML", 28.3, Color.Blue)
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
.AddItem("C#", 22.6, Color.Green)
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<LangVersion>9</LangVersion>
|
<LangVersion>9</LangVersion>
|
||||||
<ExampleTitle>Prompt</ExampleTitle>
|
<ExampleTitle>Prompt</ExampleTitle>
|
||||||
<ExampleDescription>Demonstrates how to get input from a user.</ExampleDescription>
|
<ExampleDescription>Demonstrates how to get input from a user.</ExampleDescription>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
<Description>A library that extends Spectre.Console with ImageSharp superpowers.</Description>
|
<Description>A library that extends Spectre.Console with ImageSharp superpowers.</Description>
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
████████████████████████████████████████████████████████████
|
████████████████████████████████████████████████████████████
|
||||||
■ SCSS 37% ■ HTML 28.3% ■ C# 22.6% ■ JavaScript 6%
|
■ SCSS 37% ■ HTML 28,3% ■ C# 22,6% ■ JavaScript 6%
|
||||||
■ Ruby 6% ■ Shell 0.1%
|
■ Ruby 6% ■ Shell 0,1%
|
@ -16,15 +16,10 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart();
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
@ -36,38 +31,28 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart().Width(60);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
[Expectation("ShowAsPercentages")]
|
[Expectation("TagFormat")]
|
||||||
public async Task Should_Render_Correctly_With_Specific_Width()
|
public async Task Should_Render_Correctly_With_Specific_Tag_Formatter()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart()
|
||||||
|
.Width(60)
|
||||||
|
.Culture("sv-SE")
|
||||||
|
.TagValueFormat("{0}%");
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.ShowAsPercentages()
|
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
@ -79,17 +64,10 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart().Width(60).HideTags();
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.HideTags()
|
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
@ -101,17 +79,10 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart().Width(60).HideTagValues();
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.HideTagValues()
|
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
@ -123,17 +94,10 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart().Width(60).Culture("sv-SE");
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.Culture("sv-SE")
|
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
@ -145,17 +109,10 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeConsole(width: 80);
|
var console = new FakeConsole(width: 80);
|
||||||
|
var chart = Fixture.GetChart().Width(60).FullSize();
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.FullSize()
|
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
await Verifier.Verify(console.Output);
|
||||||
@ -167,20 +124,27 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var console = new FakeAnsiConsole(ColorSystem.EightBit, width: 80);
|
var console = new FakeAnsiConsole(ColorSystem.EightBit, width: 80);
|
||||||
|
var chart = Fixture.GetChart().Width(60).FullSize();
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(new BreakdownChart()
|
console.Render(chart);
|
||||||
.Width(60)
|
|
||||||
.FullSize()
|
// Then
|
||||||
|
await Verifier.Verify(console.Output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Fixture
|
||||||
|
{
|
||||||
|
public static BreakdownChart GetChart()
|
||||||
|
{
|
||||||
|
return new BreakdownChart()
|
||||||
.AddItem("SCSS", 37, Color.Red)
|
.AddItem("SCSS", 37, Color.Red)
|
||||||
.AddItem("HTML", 28.3, Color.Blue)
|
.AddItem("HTML", 28.3, Color.Blue)
|
||||||
.AddItem("C#", 22.6, Color.Green)
|
.AddItem("C#", 22.6, Color.Green)
|
||||||
.AddItem("JavaScript", 6, Color.Yellow)
|
.AddItem("JavaScript", 6, Color.Yellow)
|
||||||
.AddItem("Ruby", 6, Color.LightGreen)
|
.AddItem("Ruby", 6, Color.LightGreen)
|
||||||
.AddItem("Shell", 0.1, Color.Aqua));
|
.AddItem("Shell", 0.1, Color.Aqua);
|
||||||
|
}
|
||||||
// Then
|
|
||||||
await Verifier.Verify(console.Output);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace Spectre.Console
|
namespace Spectre.Console
|
||||||
{
|
{
|
||||||
@ -137,18 +138,19 @@ namespace Spectre.Console
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All values will be shown as percentages.
|
/// Tags will be shown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="chart">The breakdown chart.</param>
|
/// <param name="chart">The breakdown chart.</param>
|
||||||
|
/// <param name="format">The tag value format.</param>
|
||||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||||
public static BreakdownChart ShowAsPercentages(this BreakdownChart chart)
|
public static BreakdownChart TagValueFormat(this BreakdownChart chart, string? format)
|
||||||
{
|
{
|
||||||
if (chart is null)
|
if (chart is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(chart));
|
throw new ArgumentNullException(nameof(chart));
|
||||||
}
|
}
|
||||||
|
|
||||||
chart.ShowAsPercentages = true;
|
chart.TagValueFormat = format;
|
||||||
return chart;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,12 +20,6 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int? Width { get; set; }
|
public int? Width { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets a value indicating whether or not
|
|
||||||
/// to show values as percentages or not.
|
|
||||||
/// </summary>
|
|
||||||
public bool ShowAsPercentages { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not to show tags.
|
/// Gets or sets a value indicating whether or not to show tags.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -36,6 +30,11 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ShowTagValues { get; set; } = true;
|
public bool ShowTagValues { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the tag value format.
|
||||||
|
/// </summary>
|
||||||
|
public string? TagValueFormat { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether or not the
|
/// Gets or sets a value indicating whether or not the
|
||||||
/// chart and tags should be rendered in compact mode.
|
/// chart and tags should be rendered in compact mode.
|
||||||
@ -91,8 +90,8 @@ namespace Spectre.Console
|
|||||||
{
|
{
|
||||||
Width = width,
|
Width = width,
|
||||||
Culture = Culture,
|
Culture = Culture,
|
||||||
ShowPercentages = ShowAsPercentages,
|
|
||||||
ShowTagValues = ShowTagValues,
|
ShowTagValues = ShowTagValues,
|
||||||
|
TagValueFormat = TagValueFormat,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,8 +11,8 @@ namespace Spectre.Console
|
|||||||
|
|
||||||
public int? Width { get; set; }
|
public int? Width { get; set; }
|
||||||
public CultureInfo? Culture { get; set; }
|
public CultureInfo? Culture { get; set; }
|
||||||
public bool ShowPercentages { get; set; }
|
|
||||||
public bool ShowTagValues { get; set; } = true;
|
public bool ShowTagValues { get; set; } = true;
|
||||||
|
public string? TagValueFormat { get; set; }
|
||||||
|
|
||||||
public BreakdownTags(List<IBreakdownChartItem> data)
|
public BreakdownTags(List<IBreakdownChartItem> data)
|
||||||
{
|
{
|
||||||
@ -56,11 +56,13 @@ namespace Spectre.Console
|
|||||||
|
|
||||||
private string FormatValue(IBreakdownChartItem item, CultureInfo culture)
|
private string FormatValue(IBreakdownChartItem item, CultureInfo culture)
|
||||||
{
|
{
|
||||||
|
var formatter = TagValueFormat ?? "{0}";
|
||||||
|
|
||||||
if (ShowTagValues)
|
if (ShowTagValues)
|
||||||
{
|
{
|
||||||
return string.Format(culture, "{0} [grey]{1}{2}[/]",
|
return string.Format(culture, "{0} [grey]{1}[/]",
|
||||||
item.Label.EscapeMarkup(), item.Value,
|
item.Label.EscapeMarkup(),
|
||||||
ShowPercentages ? "%" : string.Empty);
|
string.Format(culture, formatter, item.Value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return item.Label.EscapeMarkup();
|
return item.Label.EscapeMarkup();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user