Add example infrastructure

* Add "Shared" projects for all examples
* Update "Colors" example with better TrueColor demo
* Use same namespace for all examples
This commit is contained in:
Patrik Svensson 2021-04-11 22:47:26 +02:00 committed by Phil Scott
parent 2fe2bb3c32
commit 3545e0f6b5
74 changed files with 271 additions and 355 deletions

View File

@ -1,7 +1,7 @@
using System.ComponentModel; using System.ComponentModel;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Delegates namespace Spectre.Console.Examples
{ {
public static partial class Program public static partial class Program
{ {

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@ -11,7 +11,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,6 @@
using System;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Delegates namespace Spectre.Console.Examples
{ {
public static partial class Program public static partial class Program
{ {
@ -22,7 +21,7 @@ namespace Delegates
private static int Foo(CommandContext context) private static int Foo(CommandContext context)
{ {
Console.WriteLine("Foo"); AnsiConsole.WriteLine("Foo");
return 0; return 0;
} }
@ -30,7 +29,7 @@ namespace Delegates
{ {
for (var index = 0; index < settings.Count; index++) for (var index = 0; index < settings.Count; index++)
{ {
Console.WriteLine("Bar"); AnsiConsole.WriteLine("Bar");
} }
return 0; return 0;

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@ -11,7 +11,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,7 @@
using System; using System;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Dynamic namespace Spectre.Console.Examples
{ {
public sealed class MyCommand : Command public sealed class MyCommand : Command
{ {
@ -13,7 +13,7 @@ namespace Dynamic
} }
Console.WriteLine("Value = {0}", data); AnsiConsole.WriteLine("Value = {0}", data);
return 0; return 0;
} }
} }

View File

@ -1,7 +1,7 @@
using System.Linq; using System.Linq;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Dynamic namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -2,7 +2,7 @@ using System;
using System.ComponentModel; using System.ComponentModel;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Injection.Commands namespace Spectre.Console.Examples
{ {
public sealed class DefaultCommand : Command<DefaultCommand.Settings> public sealed class DefaultCommand : Command<DefaultCommand.Settings>
{ {

View File

@ -1,6 +1,4 @@
using System; namespace Spectre.Console.Examples
namespace Injection
{ {
public interface IGreeter public interface IGreeter
{ {
@ -11,7 +9,7 @@ namespace Injection
{ {
public void Greet(string name) public void Greet(string name)
{ {
Console.WriteLine($"Hello {name}!"); AnsiConsole.WriteLine($"Hello {name}!");
} }
} }
} }

View File

@ -2,7 +2,7 @@ using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Injection namespace Spectre.Console.Examples
{ {
public sealed class TypeRegistrar : ITypeRegistrar public sealed class TypeRegistrar : ITypeRegistrar
{ {

View File

@ -2,7 +2,7 @@ using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Injection namespace Spectre.Console.Examples
{ {
public sealed class TypeResolver : ITypeResolver, IDisposable public sealed class TypeResolver : ITypeResolver, IDisposable
{ {

View File

@ -15,7 +15,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,7 @@
using Injection.Commands;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Injection namespace Spectre.Console.Examples
{ {
public class Program public class Program
{ {

View File

@ -1,8 +1,7 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Spectre.Console;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Logging.Commands namespace Spectre.Console.Examples
{ {
public class HelloCommand : Command<HelloCommand.Settings> public class HelloCommand : Command<HelloCommand.Settings>
{ {

View File

@ -1,11 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
using Serilog.Events; using Serilog.Events;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Logging.Commands namespace Spectre.Console.Examples
{ {
public class LogCommandSettings : CommandSettings public class LogCommandSettings : CommandSettings
{ {

View File

@ -1,8 +1,7 @@
using Logging.Commands;
using Serilog.Core; using Serilog.Core;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Logging namespace Spectre.Console.Examples
{ {
public class LogInterceptor : ICommandInterceptor public class LogInterceptor : ICommandInterceptor
{ {

View File

@ -1,7 +1,7 @@
using Serilog.Core; using Serilog.Core;
using Serilog.Events; using Serilog.Events;
namespace Logging namespace Spectre.Console.Examples
{ {
internal class LoggingEnricher : ILogEventEnricher internal class LoggingEnricher : ILogEventEnricher
{ {

View File

@ -1,8 +1,8 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Logging namespace Spectre.Console.Examples
{ {
public sealed class TypeRegistrar : ITypeRegistrar public sealed class TypeRegistrar : ITypeRegistrar
{ {

View File

@ -1,8 +1,8 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace Logging namespace Spectre.Console.Examples
{ {
public sealed class TypeResolver : ITypeResolver public sealed class TypeResolver : ITypeResolver
{ {

View File

@ -20,7 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,3 @@
using Logging.Commands;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Serilog; using Serilog;
using Spectre.Console.Cli; using Spectre.Console.Cli;
@ -13,7 +12,7 @@ using Spectre.Console.Cli;
* Spectre.Console CommandInterceptor * Spectre.Console CommandInterceptor
*/ */
namespace Logging namespace Spectre.Console.Examples
{ {
public class Program public class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,6 @@
using Spectre.Console;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
namespace BordersExample namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace Calendars
{ {
public static class Program public static class Program
{ {

View File

@ -9,8 +9,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\src\Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj" /> <ProjectReference Include="..\..\..\src\Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj" />
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -4,9 +4,8 @@ Licensed under GNU Free Documentation License 1.2
*/ */
using System; using System;
using Spectre.Console;
namespace CanvasExample namespace Spectre.Console.Examples
{ {
public static class Mandelbrot public static class Mandelbrot
{ {

View File

@ -1,10 +1,9 @@
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using Spectre.Console;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
namespace CanvasExample namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,6 @@
using Spectre.Console;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
namespace Charts namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,27 +1,23 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace ColorExample
{ {
public static class Program public static class Program
{ {
public static void Main() public static void Main()
{
if (AnsiConsole.Profile.ColorSystem == ColorSystem.NoColors)
{ {
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// No colors // No colors
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
if (AnsiConsole.Profile.ColorSystem == ColorSystem.NoColors)
{
AnsiConsole.WriteLine("No colors are supported."); AnsiConsole.WriteLine("No colors are supported.");
return; return;
} }
if (AnsiConsole.Profile.Supports(ColorSystem.Legacy))
{
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// 3-BIT // 3-BIT
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
if (AnsiConsole.Profile.Supports(ColorSystem.Legacy))
{
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Render(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftAligned());
@ -30,6 +26,7 @@ namespace ColorExample
for (var i = 0; i < 8; i++) for (var i = 0; i < 8; i++)
{ {
AnsiConsole.Background = Color.FromInt32(i); AnsiConsole.Background = Color.FromInt32(i);
AnsiConsole.Foreground = AnsiConsole.Background.GetInvertedColor();
AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString())); AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString()));
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
if ((i + 1) % 8 == 0) if ((i + 1) % 8 == 0)
@ -39,12 +36,11 @@ namespace ColorExample
} }
} }
if (AnsiConsole.Profile.Supports(ColorSystem.Standard))
{
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// 4-BIT // 4-BIT
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
if (AnsiConsole.Profile.Supports(ColorSystem.Standard))
{
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Render(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftAligned());
@ -53,6 +49,7 @@ namespace ColorExample
for (var i = 0; i < 16; i++) for (var i = 0; i < 16; i++)
{ {
AnsiConsole.Background = Color.FromInt32(i); AnsiConsole.Background = Color.FromInt32(i);
AnsiConsole.Foreground = AnsiConsole.Background.GetInvertedColor();
AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString())); AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString()));
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
if ((i + 1) % 8 == 0) if ((i + 1) % 8 == 0)
@ -62,12 +59,11 @@ namespace ColorExample
} }
} }
if (AnsiConsole.Profile.Supports(ColorSystem.EightBit))
{
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// 8-BIT // 8-BIT
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
if (AnsiConsole.Profile.Supports(ColorSystem.EightBit))
{
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Render(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftAligned());
@ -79,6 +75,7 @@ namespace ColorExample
{ {
var number = i * 16 + j; var number = i * 16 + j;
AnsiConsole.Background = Color.FromInt32(number); AnsiConsole.Background = Color.FromInt32(number);
AnsiConsole.Foreground = AnsiConsole.Background.GetInvertedColor();
AnsiConsole.Write(string.Format(" {0,-4}", number)); AnsiConsole.Write(string.Format(" {0,-4}", number));
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
if ((number + 1) % 16 == 0) if ((number + 1) % 16 == 0)
@ -89,31 +86,17 @@ namespace ColorExample
} }
} }
if (AnsiConsole.Profile.Supports(ColorSystem.TrueColor))
{
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// 24-BIT // 24-BIT
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
if (AnsiConsole.Profile.Supports(ColorSystem.TrueColor))
{
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Render(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
var index = 0; AnsiConsole.Render(new ColorBox(width: 80, height: 15));
for (var i = 0.0005; i < 1; i += 0.0025)
{
index++;
var color = Utilities.HSL2RGB(i, 0.5, 0.5);
AnsiConsole.Background = new Color(color.R, color.G, color.B);
AnsiConsole.Write(" ");
if (index % 50 == 0)
{
AnsiConsole.WriteLine();
}
}
} }
} }
} }

View File

@ -1,77 +0,0 @@
using System;
using Spectre.Console;
namespace ColorExample
{
public static class Utilities
{
// Borrowed from https://geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm
public static Color HSL2RGB(double h, double sl, double l)
{
double v;
double r, g, b;
r = l; // default to gray
g = l;
b = l;
v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
if (v > 0)
{
double m;
double sv;
int sextant;
double fract, vsf, mid1, mid2;
m = l + l - v;
sv = (v - m) / v;
h *= 6.0;
sextant = (int)h;
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant)
{
case 0:
r = v;
g = mid1;
b = m;
break;
case 1:
r = mid2;
g = v;
b = m;
break;
case 2:
r = m;
g = v;
b = mid1;
break;
case 3:
r = m;
g = mid2;
b = v;
break;
case 4:
r = mid1;
g = m;
b = v;
break;
case 5:
r = v;
g = m;
b = mid2;
break;
}
}
return new Color(
Convert.ToByte(r * 255.0f),
Convert.ToByte(g * 255.0f),
Convert.ToByte(b * 255.0f));
}
}
}

View File

@ -13,7 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using Spectre.Console;
namespace ColumnsExample namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace ColumnsExample namespace Spectre.Console.Examples
{ {
public sealed class User public sealed class User
{ {
@ -22,9 +22,9 @@ namespace ColumnsExample
}, },
new User new User
{ {
FirstName = "Brandon", FirstName = "Phil",
LastName = "Cole", LastName = "Scott",
City = "Washington", City = "Dayton",
Country = "United States", Country = "United States",
}, },
new User new User

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace Cursor
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace EmojiExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,7 @@
using System; using System;
using System.Security.Authentication; using System.Security.Authentication;
using Spectre.Console;
namespace Exceptions namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace EmojiExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace GridExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace InfoExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace LinkExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace PanelExample
{ {
public static class Program public static class Program
{ {

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace ProgressExample namespace Spectre.Console.Examples
{ {
public static class DescriptionGenerator public static class DescriptionGenerator
{ {

View File

@ -1,10 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using Spectre.Console;
namespace ProgressExample namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -13,7 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace Cursor
{ {
public static class Program public static class Program
{ {

View File

@ -10,7 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace EmojiExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,108 +0,0 @@
using System.Collections.Generic;
using System.Diagnostics;
using Spectre.Console;
using Spectre.Console.Rendering;
namespace Showcase
{
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

@ -1,7 +1,6 @@
using System; using System;
using Spectre.Console;
namespace Showcase namespace Spectre.Console.Examples
{ {
public static class ExceptionGenerator public static class ExceptionGenerator
{ {

View File

@ -1,10 +1,6 @@
using System;
using Spectre.Console;
using SixLabors.ImageSharp.Processing;
using System.Threading;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
namespace Showcase namespace Spectre.Console.Examples
{ {
public static partial class Program public static partial class Program
{ {
@ -102,7 +98,7 @@ namespace Showcase
"✓ [bold purple]8-bit color[/]\n" + "✓ [bold purple]8-bit color[/]\n" +
"✓ [bold yellow]Truecolor (16.7 million)[/]\n" + "✓ [bold yellow]Truecolor (16.7 million)[/]\n" +
"✓ [bold aqua]Automatic color conversion[/]"), "✓ [bold aqua]Automatic color conversion[/]"),
new ColorBox(6)); new ColorBox(height: 6));
return colorTable; return colorTable;
} }

View File

@ -1,16 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<ExampleTitle>Showcase</ExampleTitle> <ExampleTitle>Showcase</ExampleTitle>
<ExampleDescription>Demonstation of Spectre.Console.</ExampleDescription> <ExampleDescription>Demonstation of Spectre.Console.</ExampleDescription>
<ExampleGroup>Misc</ExampleGroup> <ExampleGroup>Misc</ExampleGroup>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\src\Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,6 @@
using System.Threading; using System.Threading;
using Spectre.Console;
namespace ProgressExample namespace Spectre.Console.Examples
{ {
public static class Program public static class Program
{ {

View File

@ -13,7 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,4 @@
using System.Diagnostics; namespace Spectre.Console.Examples
using Spectre.Console;
namespace TableExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,4 @@
using Spectre.Console; namespace Spectre.Console.Examples
namespace TableExample
{ {
public static class Program public static class Program
{ {

View File

@ -9,7 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> <ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

124
examples/Shared/ColorBox.cs Normal file
View File

@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console.Examples
{
public sealed class ColorBox : Renderable
{
private readonly int _height;
private int? _width;
public ColorBox(int height)
{
_height = height;
}
public ColorBox(int width, int height)
: this(height)
{
_width = width;
}
protected override Measurement Measure(RenderContext context, int maxWidth)
{
return new Measurement(1, GetWidth(maxWidth));
}
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
maxWidth = GetWidth(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 int GetWidth(int maxWidth)
{
var width = maxWidth;
if (_width != null)
{
width = Math.Min(_width.Value, width);
}
return width;
}
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,15 @@
namespace Spectre.Console.Examples
{
public static class ColorExtensions
{
public static Color GetInvertedColor(this Color color)
{
return GetLuminance(color) < 140 ? Color.White : Color.Black;
}
private static float GetLuminance(this Color color)
{
return (float)((0.2126 * color.R) + (0.7152 * color.G) + (0.0722 * color.B));
}
}
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<ExampleVisible>false</ExampleVisible>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup>
</Project>

View File

@ -1,8 +1,6 @@
using Shouldly; using Shouldly;
using Spectre.Console.Cli; using Spectre.Console.Cli;
using Spectre.Console.Testing; using Spectre.Console.Testing;
using Spectre.Console.Tests.Data;
using VerifyXunit;
using Xunit; using Xunit;
namespace Spectre.Console.Tests.Unit.Cli namespace Spectre.Console.Tests.Unit.Cli

View File

@ -82,6 +82,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logging", "..\examples\Cli\
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Showcase", "..\examples\Console\Showcase\Showcase.csproj", "{4C30C028-E97D-4B4C-AD17-C90F338A4DFF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Showcase", "..\examples\Console\Showcase\Showcase.csproj", "{4C30C028-E97D-4B4C-AD17-C90F338A4DFF}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{A0C772BA-C5F4-451D-AA7A-4045F2FA0201}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "..\examples\Shared\Shared.csproj", "{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -440,6 +444,18 @@ Global
{4C30C028-E97D-4B4C-AD17-C90F338A4DFF}.Release|x64.Build.0 = Release|Any CPU {4C30C028-E97D-4B4C-AD17-C90F338A4DFF}.Release|x64.Build.0 = Release|Any CPU
{4C30C028-E97D-4B4C-AD17-C90F338A4DFF}.Release|x86.ActiveCfg = Release|Any CPU {4C30C028-E97D-4B4C-AD17-C90F338A4DFF}.Release|x86.ActiveCfg = Release|Any CPU
{4C30C028-E97D-4B4C-AD17-C90F338A4DFF}.Release|x86.Build.0 = Release|Any CPU {4C30C028-E97D-4B4C-AD17-C90F338A4DFF}.Release|x86.Build.0 = Release|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Debug|x64.ActiveCfg = Debug|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Debug|x64.Build.0 = Debug|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Debug|x86.ActiveCfg = Debug|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Debug|x86.Build.0 = Debug|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Release|Any CPU.Build.0 = Release|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Release|x64.ActiveCfg = Release|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Release|x64.Build.0 = Release|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Release|x86.ActiveCfg = Release|Any CPU
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -473,6 +489,8 @@ Global
{CA7AF967-3FA5-4CB1-9564-740CF4527895} = {F0575243-121F-4DEE-9F6B-246E26DC0844} {CA7AF967-3FA5-4CB1-9564-740CF4527895} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{33C7075A-DF97-44FC-8AB3-0CCFBA03341A} = {42792D7F-0BB6-4EE1-A314-8889305A4C48} {33C7075A-DF97-44FC-8AB3-0CCFBA03341A} = {42792D7F-0BB6-4EE1-A314-8889305A4C48}
{4C30C028-E97D-4B4C-AD17-C90F338A4DFF} = {F0575243-121F-4DEE-9F6B-246E26DC0844} {4C30C028-E97D-4B4C-AD17-C90F338A4DFF} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{A0C772BA-C5F4-451D-AA7A-4045F2FA0201} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{8428A7DD-29FC-4417-9CA0-B90D34B26AB2} = {A0C772BA-C5F4-451D-AA7A-4045F2FA0201}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C} SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C}

View File

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace Spectre.Console namespace Spectre.Console
{ {