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