Add demo example

This commit is contained in:
Patrik Svensson 2021-03-08 19:36:04 +01:00 committed by Phil Scott
parent 36ec3d1fd3
commit ca036f6543
6 changed files with 340 additions and 1 deletions

View File

@ -0,0 +1,108 @@
using System.Collections.Generic;
using System.Diagnostics;
using Spectre.Console;
using Spectre.Console.Rendering;
namespace Demo
{
public static partial class Program
{
public sealed class ColorBox : Renderable
{
private readonly int _height;
public ColorBox(int height)
{
_height = height;
}
protected override Measurement Measure(RenderContext context, int maxWidth)
{
return new Measurement(1, maxWidth);
}
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
for (var y = 0; y < _height; y++)
{
for (var x = 0; x < maxWidth; x++)
{
var h = x / (float)maxWidth;
var l = 0.1f + ((y / (float)_height) * 0.7f);
var (r1, g1, b1) = ColorFromHSL(h, l, 1.0f);
var (r2, g2, b2) = ColorFromHSL(h, l + (0.7f / 10), 1.0f);
var background = new Color((byte)(r1 * 255), (byte)(g1 * 255), (byte)(b1 * 255));
var foreground = new Color((byte)(r2 * 255), (byte)(g2 * 255), (byte)(b2 * 255));
yield return new Segment("▄", new Style(foreground, background));
}
yield return Segment.LineBreak;
}
}
private static (float, float, float) ColorFromHSL(double h, double l, double s)
{
double r = 0, g = 0, b = 0;
if (l != 0)
{
if (s == 0)
{
r = g = b = l;
}
else
{
double temp2;
if (l < 0.5)
{
temp2 = l * (1.0 + s);
}
else
{
temp2 = l + s - (l * s);
}
var temp1 = 2.0 * l - temp2;
r = GetColorComponent(temp1, temp2, h + 1.0 / 3.0);
g = GetColorComponent(temp1, temp2, h);
b = GetColorComponent(temp1, temp2, h - 1.0 / 3.0);
}
}
return ((float)r, (float)g, (float)b);
}
private static double GetColorComponent(double temp1, double temp2, double temp3)
{
if (temp3 < 0.0)
{
temp3 += 1.0;
}
else if (temp3 > 1.0)
{
temp3 -= 1.0;
}
if (temp3 < 1.0 / 6.0)
{
return temp1 + (temp2 - temp1) * 6.0 * temp3;
}
else if (temp3 < 0.5)
{
return temp2;
}
else if (temp3 < 2.0 / 3.0)
{
return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0);
}
else
{
return temp1;
}
}
}
}
}

View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ExampleTitle>Demo</ExampleTitle>
<ExampleDescription>Demonstation of Spectre.Console.</ExampleDescription>
<ExampleGroup>Misc</ExampleGroup>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" />
<ProjectReference Include="..\..\..\src\Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="linux.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="smiley.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,31 @@
using System;
using Spectre.Console;
namespace Demo
{
public static class ExceptionGenerator
{
public static Exception GenerateException()
{
try
{
SomeOperation();
throw new InvalidOperationException();
}
catch (Exception ex)
{
return ex;
}
}
private static void SomeOperation()
{
SomeOperationGoingWrong();
}
private static void SomeOperationGoingWrong()
{
throw new InvalidOperationException("Something went very wrong!");
}
}
}

View File

@ -0,0 +1,157 @@
using System;
using Spectre.Console;
using SixLabors.ImageSharp.Processing;
using System.Threading;
using Spectre.Console.Rendering;
namespace Demo
{
public static partial class Program
{
public static void Main()
{
var table = new Table().HideHeaders().NoBorder();
table.Title("[u][yellow]Spectre.Console[/] [b]Features[/][/]");
table.AddColumn("Feature", c => c.NoWrap().RightAligned().Width(10).PadRight(3));
table.AddColumn("Demonstration", c => c.PadRight(0));
table.AddEmptyRow();
// Colors
table.AddRow(
new Markup("[red]Colors[/]"),
GetColorTable());
// Styles
table.AddEmptyRow();
table.AddRow(
new Markup("[red]OS[/]"),
new Grid().Expand().AddColumns(3)
.AddRow(
"[bold green]Windows[/]",
"[bold blue]macOS[/]",
"[bold yellow]Linux[/]"));
// Styles
table.AddEmptyRow();
table.AddRow(
"[red]Styles[/]",
"All ansi styles: [bold]bold[/], [dim]dim[/], [italic]italic[/], [underline]underline[/], "
+ "[strikethrough]strikethrough[/], [reverse]reverse[/], and even [blink]blink[/].");
// Text
table.AddEmptyRow();
table.AddRow(
new Markup("[red]Text[/]"),
new Markup("Word wrap text. Justify [green]left[/], [yellow]center[/] or [blue]right[/]."));
table.AddEmptyRow();
table.AddRow(
Text.Empty,
GetTextGrid());
// Markup
table.AddEmptyRow();
table.AddRow(
"[red]Markup[/]",
"[bold purple]Spectre.Console[/] supports a simple [i]bbcode[/] like "
+ "[b]markup[/] for [yellow]color[/], [underline]style[/], and emoji! "
+ ":thumbs_up: :red_apple: :ant: :bear: :baguette_bread: :bus:");
// Trees and tables
table.AddEmptyRow();
table.AddRow(
new Markup("[red]Tables and Trees[/]"),
GetTreeTable());
// Charts
table.AddRow(
new Markup("[red]Charts[/]"),
new Grid().Collapse().AddColumns(2).AddRow(
new Panel(GetBreakdownChart()).BorderColor(Color.Grey),
new Panel(GetBarChart()).BorderColor(Color.Grey)));
// Exceptions
table.AddEmptyRow();
table.AddRow(
new Markup("[red]Exceptions[/]"),
ExceptionGenerator.GenerateException().GetRenderable());
// Much more
table.AddEmptyRow();
table.AddRow(
"[red]+ Much more![/]",
"Tables, Grids, Trees, Progress bars, Status, Bar charts, Calendars, Figlet, Images, Text prompts, "
+ "List boxes, Separators, Pretty exceptions, Canvas, CLI parsing");
table.AddEmptyRow();
// Render the table
AnsiConsole.WriteLine();
AnsiConsole.Render(table);
}
private static IRenderable GetColorTable()
{
var colorTable = new Table().Collapse().HideHeaders().NoBorder();
colorTable.AddColumn("Desc", c => c.PadRight(3)).AddColumn("Colors", c => c.PadRight(0));
colorTable.AddRow(
new Markup(
"✓ [bold grey]NO_COLOR support[/]\n" +
"✓ [bold green]3-bit color[/]\n" +
"✓ [bold blue]4-bit color[/]\n" +
"✓ [bold purple]8-bit color[/]\n" +
"✓ [bold yellow]Truecolor (16.7 million)[/]\n" +
"✓ [bold aqua]Automatic color conversion[/]"),
new ColorBox(6));
return colorTable;
}
private static IRenderable GetTextGrid()
{
var loremTable = new Grid();
var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque in metus sed sapien ultricies pretium a at justo. Maecenas luctus velit et auctor maximus.";
loremTable.AddColumn(new GridColumn().LeftAligned());
loremTable.AddColumn(new GridColumn().Centered());
loremTable.AddColumn(new GridColumn().RightAligned());
loremTable.AddRow($"[green]{lorem}[/]", $"[yellow]{lorem}[/]", $"[blue]{lorem}[/]");
return loremTable;
}
private static IRenderable GetTreeTable()
{
var tree = new Tree("📁 src");
tree.AddNode("📁 foo").AddNode("📄 bar.cs");
tree.AddNode("📁 baz").AddNode("📁 qux").AddNode("📄 corgi.txt");
tree.AddNode("📄 waldo.xml");
var table = new Table().SimpleBorder().BorderColor(Color.Grey);
table.AddColumn(new TableColumn("Overview"));
table.AddColumn(new TableColumn("").Footer("[grey]3 Files, 225 KiB[/]"));
table.AddRow(new Markup("[yellow]Files[/]"), tree);
return new Table().RoundedBorder().Collapse().BorderColor(Color.Yellow)
.AddColumn("Foo").AddColumn("Bar")
.AddRow(new Text("Baz"), table)
.AddRow("Qux", "Corgi");
}
private static IRenderable GetBarChart()
{
return new BarChart()
.AddItem("Apple", 32, Color.Green)
.AddItem("Oranges", 13, Color.Orange1)
.AddItem("Bananas", 22, Color.Yellow);
}
private static IRenderable GetBreakdownChart()
{
return new BreakdownChart()
.ShowPercentage()
.FullSize()
.AddItem("C#", 82, Color.Green)
.AddItem("PowerShell", 13, Color.Red)
.AddItem("Bash", 5, Color.Blue);
}
}
}

View File

@ -80,7 +80,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugins", "Plugins", "{E0E4
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Trees", "..\examples\Console\Trees\Trees.csproj", "{CA7AF967-3FA5-4CB1-9564-740CF4527895}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging", "..\examples\Cli\Logging\Logging.csproj", "{33C7075A-DF97-44FC-8AB3-0CCFBA03341A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logging", "..\examples\Cli\Logging\Logging.csproj", "{33C7075A-DF97-44FC-8AB3-0CCFBA03341A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo", "..\examples\Console\Demo\Demo.csproj", "{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -440,6 +442,18 @@ Global
{33C7075A-DF97-44FC-8AB3-0CCFBA03341A}.Release|x64.Build.0 = Release|Any CPU
{33C7075A-DF97-44FC-8AB3-0CCFBA03341A}.Release|x86.ActiveCfg = Release|Any CPU
{33C7075A-DF97-44FC-8AB3-0CCFBA03341A}.Release|x86.Build.0 = Release|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Debug|x64.ActiveCfg = Debug|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Debug|x64.Build.0 = Debug|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Debug|x86.ActiveCfg = Debug|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Debug|x86.Build.0 = Debug|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Release|Any CPU.Build.0 = Release|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Release|x64.ActiveCfg = Release|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Release|x64.Build.0 = Release|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Release|x86.ActiveCfg = Release|Any CPU
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -473,6 +487,7 @@ Global
{F83CB4F1-95B8-45A4-A415-6DB5F8CA1E12} = {42792D7F-0BB6-4EE1-A314-8889305A4C48}
{CA7AF967-3FA5-4CB1-9564-740CF4527895} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{33C7075A-DF97-44FC-8AB3-0CCFBA03341A} = {42792D7F-0BB6-4EE1-A314-8889305A4C48}
{A6B621B4-24D6-461D-9104-A94C9B5D9EB4} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C}

View File

@ -22,9 +22,12 @@ namespace Spectre.Console
{ "underline", Decoration.Underline },
{ "u", Decoration.Underline },
{ "invert", Decoration.Invert },
{ "reverse", Decoration.Invert },
{ "conceal", Decoration.Conceal },
{ "blink", Decoration.SlowBlink },
{ "slowblink", Decoration.SlowBlink },
{ "rapidblink", Decoration.RapidBlink },
{ "strike", Decoration.Strikethrough },
{ "strikethrough", Decoration.Strikethrough },
{ "s", Decoration.Strikethrough },
};