Add FIGlet text support

Closes #97
This commit is contained in:
Patrik Svensson 2020-11-20 00:15:13 +01:00 committed by Patrik Svensson
parent bde61cc6ff
commit a59e0dcb21
24 changed files with 3718 additions and 15 deletions

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Figlet</Title>
<Description>Demonstrates how to render FIGlet text.</Description>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
using Spectre.Console;
namespace EmojiExample
{
public static class Program
{
public static void Main(string[] args)
{
AnsiConsole.Render(new FigletText("Left aligned").LeftAligned().Color(Color.Red));
AnsiConsole.Render(new FigletText("Centered").Centered().Color(Color.Green));
AnsiConsole.Render(new FigletText("Right aligned").RightAligned().Color(Color.Blue));
}
}
}

View File

@ -0,0 +1,719 @@
flf2a$ 7 6 22 15 4
starwars.flf by Ryan Youck (youck@cs.uregina.ca) Dec 25/1994
I am not responsible for use of this font
Based on Big.flf by Glenn Chappell
$ $@
$ $@
$ $@
$ $@
$ $@
$ $@
$ $@@
__ $@
| |$@
| |$@
| |$@
|__|$@
(__)$@
$@@
_ _ @
( | )@
V V @
$ @
$ @
$ @
@@
_ _ @
_| || |_$@
|_ __ _|@
_| || |_ @
|_ __ _|@
|_||_| $@
@@
__,--,_.@
/ |@
| (----`@
\ \ $@
.----) | $@
|_ __/ $@
'--' $@@
_ ___$ @
/ \ / /$ @
( o ) / / $ @
\_/ / / _$ @
/ / / \ @
/ / ( o )@
/__/ \_/ @@
@
___ @
( _ ) $@
/ _ \/\@
| (_> <@
\___/\/@
$@@
__ @
(_ )@
|/ @
$ @
$ @
$ @
@@
___@
/ /@
| |$@
| |$@
| |$@
| |$@
\__\@@
___ @
\ \ @
| |@
| |@
| |@
| |@
/__/ @@
_ @
/\| |/\ @
\ ` ' /$@
|_ _|@
/ , . \$@
\/|_|\/ @
@@
@
_ @
_| |_$@
|_ _|@
|_| $@
$ @
@@
@
@
$ @
$ @
__ @
(_ )@
|/ @@
@
@
______ @
|______|@
$ @
$ @
@@
@
@
@
$ @
__ @
(__)@
@@
___@
/ /@
/ / @
/ /$ @
/ /$ @
/__/$ @
@@
___ $@
/ _ \ $@
| | | |$@
| | | |$@
| |_| |$@
\___/ $@
$@@
__ $@
/_ |$@
| |$@
| |$@
| |$@
|_|$@
$@@
___ $@
|__ \ $@
$) |$@
/ / $@
/ /_ $@
|____|$@
$@@
____ $@
|___ \ $@
__) |$@
|__ < $@
___) |$@
|____/ $@
$@@
_ _ $@
| || | $@
| || |_ $@
|__ _|$@
| | $@
|_| $@
$@@
_____ $@
| ____|$@
| |__ $@
|___ \ $@
___) |$@
|____/ $@
$@@
__ $@
/ / $@
/ /_ $@
| '_ \ $@
| (_) |$@
\___/ $@
$@@
______ $@
|____ |$@
$/ / $@
/ / $@
/ / $@
/_/ $@
$@@
___ $@
/ _ \ $@
| (_) |$@
> _ < $@
| (_) |$@
\___/ $@
$@@
___ $@
/ _ \ $@
| (_) |$@
\__, |$@
/ / $@
/_/ $@
$@@
@
_ @
(_)@
$ @
_ @
(_)@
@@
@
_ @
(_)@
$ @
_ @
( )@
|/ @@
___@
/ /@
/ /$@
< <$ @
\ \$@
\__\@
@@
@
______ @
|______|@
______ @
|______|@
@
@@
___ @
\ \$ @
\ \ @
> >@
/ / @
/__/$ @
@@
______ $@
| \ $@
`----) |$@
/ / $@
|__| $@
__ $@
(__) $@@
____ @
/ __ \ @
/ / _` |@
| | (_| |@
\ \__,_|@
\____/ @
@@
___ $ @
/ \ $ @
/ ^ \$ @
/ /_\ \$ @
/ _____ \$ @
/__/ \__\$@
$@@
.______ $@
| _ \ $@
| |_) |$@
| _ < $@
| |_) |$@
|______/ $@
$@@
______$@
/ |@
| ,----'@
| | $@
| `----.@
\______|@
$@@
_______ $@
| \$@
| .--. |@
| | | |@
| '--' |@
|_______/$@
$@@
_______ @
| ____|@
| |__ $@
| __| $@
| |____ @
|_______|@
@@
_______ @
| ____|@
| |__ $@
| __| $@
| | $ @
|__| @
@@
_______ @
/ _____|@
| | __ $@
| | |_ |$@
| |__| |$@
\______|$@
$@@
__ __ $@
| | | |$@
| |__| |$@
| __ |$@
| | | |$@
|__| |__|$@
$@@
__ $@
| |$@
| |$@
| |$@
| |$@
|__|$@
$@@
__ $@
| |$@
| |$@
.--. | |$@
| `--' |$@
\______/ $@
$@@
__ ___$@
| |/ /$@
| ' / $@
| < $@
| . \ $@
|__|\__\$@
$@@
__ $@
| | $@
| | $@
| | $@
| `----.@
|_______|@
$@@
.___ ___.$@
| \/ |$@
| \ / |$@
| |\/| |$@
| | | |$@
|__| |__|$@
$@@
.__ __.$@
| \ | |$@
| \| |$@
| . ` |$@
| |\ |$@
|__| \__|$@
$@@
______ $@
/ __ \ $@
| | | |$@
| | | |$@
| `--' |$@
\______/ $@
$@@
.______ $@
| _ \ $@
| |_) |$@
| ___/ $@
| | $ @
| _| $ @
$ @@
______ $ @
/ __ \ $ @
| | | | $ @
| | | | $ @
| `--' '--. @
\_____\_____\@
$ @@
.______ $ @
| _ \ $ @
| |_) | $ @
| / $ @
| |\ \----.@
| _| `._____|@
$@@
_______.@
/ |@
| (----`@
\ \ $@
.----) | $@
|_______/ $@
$@@
.___________.@
| |@
`---| |----`@
| | $ @
| | $ @
|__| $ @
$ @@
__ __ $@
| | | |$@
| | | |$@
| | | |$@
| `--' |$@
\______/ $@
$@@
____ ____$@
\ \ / /$@
\ \/ /$ @
\ /$ @
\ /$ @
\__/$ @
$ @@
____ __ ____$@
\ \ / \ / /$@
\ \/ \/ /$ @
\ /$ @
\ /\ /$ @
\__/ \__/$ @
$ @@
___ ___$@
\ \ / /$@
\ V / $@
> < $@
/ . \ $@
/__/ \__\$@
$@@
____ ____$@
\ \ / /$@
\ \/ /$ @
\_ _/$ @
| |$ @
|__|$ @
$ @@
________ $@
| / $@
`---/ / $@
/ / $@
/ /----.@
/________|@
$@@
____ @
| |@
| |-`@
| | $@
| | $@
| |-.@
|____|@@
___ @
\ \ $ @
\ \$ @
\ \$ @
\ \$@
\__\@
@@
____ @
| |@
`-| |@
| |@
| |@
.-| |@
|____|@@
___ @
/ \ @
/--^--\@
$@
$@
$@
$@@
@
@
@
$ @
$ @
______ @
|______|@@
__ @
( _)@
\| @
$ @
$ @
$ @
@@
___ $ @
/ \ $ @
/ ^ \$ @
/ /_\ \$ @
/ _____ \$ @
/__/ \__\$@
$@@
.______ $@
| _ \ $@
| |_) |$@
| _ < $@
| |_) |$@
|______/ $@
$@@
______$@
/ |@
| ,----'@
| | $@
| `----.@
\______|@
$@@
_______ $@
| \$@
| .--. |@
| | | |@
| '--' |@
|_______/$@
$@@
_______ @
| ____|@
| |__ $@
| __| $@
| |____ @
|_______|@
@@
_______ @
| ____|@
| |__ $@
| __| $@
| | $ @
|__| @
@@
_______ @
/ _____|@
| | __ $@
| | |_ |$@
| |__| |$@
\______|$@
$@@
__ __ $@
| | | |$@
| |__| |$@
| __ |$@
| | | |$@
|__| |__|$@
$@@
__ $@
| |$@
| |$@
| |$@
| |$@
|__|$@
$@@
__ $@
| |$@
| |$@
.--. | |$@
| `--' |$@
\______/ $@
$@@
__ ___$@
| |/ /$@
| ' / $@
| < $@
| . \ $@
|__|\__\$@
$@@
__ $@
| | $@
| | $@
| | $@
| `----.@
|_______|@
$@@
.___ ___.$@
| \/ |$@
| \ / |$@
| |\/| |$@
| | | |$@
|__| |__|$@
$@@
.__ __.$@
| \ | |$@
| \| |$@
| . ` |$@
| |\ |$@
|__| \__|$@
$@@
______ $@
/ __ \ $@
| | | |$@
| | | |$@
| `--' |$@
\______/ $@
$@@
.______ $@
| _ \ $@
| |_) |$@
| ___/ $@
| | $ @
| _| $ @
$ @@
______ $ @
/ __ \ $ @
| | | | $ @
| | | | $ @
| `--' '--. @
\_____\_____\@
$ @@
.______ $ @
| _ \ $ @
| |_) | $ @
| / $ @
| |\ \----.@
| _| `._____|@
$@@
_______.@
/ |@
| (----`@
\ \ $@
.----) | $@
|_______/ $@
$@@
.___________.@
| |@
`---| |----`@
| | $ @
| | $ @
|__| $ @
$ @@
__ __ $@
| | | |$@
| | | |$@
| | | |$@
| `--' |$@
\______/ $@
$@@
____ ____$@
\ \ / /$@
\ \/ /$ @
\ /$ @
\ /$ @
\__/$ @
$ @@
____ __ ____$@
\ \ / \ / /$@
\ \/ \/ /$ @
\ /$ @
\ /\ /$ @
\__/ \__/$ @
$ @@
___ ___$@
\ \ / /$@
\ V / $@
> < $@
/ . \ $@
/__/ \__\$@
$@@
____ ____$@
\ \ / /$@
\ \/ /$ @
\_ _/$ @
| |$ @
|__|$ @
$ @@
________ $@
| / $@
`---/ / $@
/ / $@
/ /----.@
/________|@
$@@
___@
/ /@
| |$@
/ /$ @
\ \$ @
| |$@
\__\@@
__ $@
| |$@
| |$@
| |$@
| |$@
| |$@
|__|$@@
___ @
\ \$ @
| | @
\ \@
/ /@
| | @
/__/$ @@
__ _ @
/ \/ |@
|_/\__/ @
$ @
$ @
$ @
@@
_ _ @
(_)_(_) @
/ \ @
/ _ \ @
/ ___ \ @
/_/ \_\@
@@
_ _ @
(_)_(_)@
/ _ \ @
| | | |@
| |_| |@
\___/ @
@@
_ _ @
(_) (_)@
| | | |@
| | | |@
| |_| |@
\___/ @
@@
_ _ @
(_) (_)@
__ _ @
/ _` |@
| (_| |@
\__,_|@
@@
_ _ @
(_) (_)@
___ @
/ _ \ @
| (_) |@
\___/ @
@@
_ _ @
(_) (_)@
_ _ @
| | | |@
| |_| |@
\__,_|@
@@
___ @
/ _ \ @
| | ) |@
| |< < @
| | ) |@
| ||_/ @
|_| @@

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Xunit.Sdk;
namespace Spectre.Console.Tests
{
public sealed class EmbeddedResourceDataAttribute : DataAttribute
{
private readonly string _args;
public EmbeddedResourceDataAttribute(string args)
{
_args = args ?? throw new ArgumentNullException(nameof(args));
}
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
var result = new object[1];
result[0] = ReadManifestData(_args);
return new[] { result };
}
public static string ReadManifestData(string resourceName)
{
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
using (var stream = ResourceReader.LoadResourceStream(resourceName))
{
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd().NormalizeLineEndings();
}
}
}
}
}

View File

@ -0,0 +1,7 @@
.______ ___ .___________..______ __ __ ___ ____ __ ____ ___ _______. __ __ _______ .______ _______
| _ \ / \ | || _ \ | | | |/ / \ \ / \ / / / \ / | | | | | | ____|| _ \ | ____|
| |_) | / ^ \ `---| |----`| |_) | | | | ' / \ \/ \/ / / ^ \ | (----` | |__| | | |__ | |_) | | |__
| ___/ / /_\ \ | | | / | | | < \ / / /_\ \ \ \ | __ | | __| | / | __|
| | / _____ \ | | | |\ \----.| | | . \ \ /\ / / _____ \ .----) | | | | | | |____ | |\ \----.| |____
| _| /__/ \__\ |__| | _| `._____||__| |__|\__\ \__/ \__/ /__/ \__\ |_______/ |__| |__| |_______|| _| `._____||_______|

View File

@ -0,0 +1,6 @@
____ _ ____ _
/ ___| _ __ ___ ___ | |_ _ __ ___ / ___| ___ _ __ ___ ___ | | ___
\___ \ | '_ \ / _ \ / __| | __| | '__| / _ \ | | / _ \ | '_ \ / __| / _ \ | | / _ \
___) | | |_) | | __/ | (__ | |_ | | | __/ _ | |___ | (_) | | | | | \__ \ | (_) | | | | __/
|____/ | .__/ \___| \___| \__| |_| \___| (_) \____| \___/ |_| |_| |___/ \___/ |_| \___|
|_|

View File

@ -0,0 +1,6 @@
____ _ ____ _
/ ___| _ __ ___ ___ | |_ _ __ ___ / ___| ___ _ __ ___ ___ | | ___
\___ \ | '_ \ / _ \ / __| | __| | '__| / _ \ | | / _ \ | '_ \ / __| / _ \ | | / _ \
___) | | |_) | | __/ | (__ | |_ | | | __/ _ | |___ | (_) | | | | | \__ \ | (_) | | | | __/
|____/ | .__/ \___| \___| \__| |_| \___| (_) \____| \___/ |_| |_| |___/ \___/ |_| \___|
|_|

View File

@ -0,0 +1,6 @@
____ _ ____ _
/ ___| _ __ ___ ___ | |_ _ __ ___ / ___| ___ _ __ ___ ___ | | ___
\___ \ | '_ \ / _ \ / __| | __| | '__| / _ \ | | / _ \ | '_ \ / __| / _ \ | | / _ \
___) | | |_) | | __/ | (__ | |_ | | | __/ _ | |___ | (_) | | | | | \__ \ | (_) | | | | __/
|____/ | .__/ \___| \___| \__| |_| \___| (_) \____| \___/ |_| |_| |___/ \___/ |_| \___|
|_|

View File

@ -0,0 +1,12 @@
____ _ _ _
| _ \ __ _ | |_ _ __ (_) | | __ __ __ __ _ ___
| |_) | / _` | | __| | '__| | | | |/ / \ \ /\ / / / _` | / __|
| __/ | (_| | | |_ | | | | | < \ V V / | (_| | \__ \
|_| \__,_| \__| |_| |_| |_|\_\ \_/\_/ \__,_| |___/
_
| |__ ___ _ __ ___
| '_ \ / _ \ | '__| / _ \
| | | | | __/ | | | __/
|_| |_| \___| |_| \___|

View File

@ -0,0 +1,12 @@
____ _ ____
/ ___| _ __ ___ ___ | |_ _ __ ___ / ___| ___
\___ \ | '_ \ / _ \ / __| | __| | '__| / _ \ | | / _ \
___) | | |_) | | __/ | (__ | |_ | | | __/ _ | |___ | (_) |
|____/ | .__/ \___| \___| \__| |_| \___| (_) \____| \___/
|_|
_
_ __ ___ ___ | | ___
| '_ \ / __| / _ \ | | / _ \
| | | | \__ \ | (_) | | | | __/
|_| |_| |___/ \___/ |_| \___|

View File

@ -5,6 +5,14 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Remove="Data\starwars.flf" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Data\starwars.flf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="Shouldly" Version="4.0.0-beta0002" />

View File

@ -0,0 +1,21 @@
using System;
using System.IO;
namespace Spectre.Console.Tests
{
public static class ResourceReader
{
public static Stream LoadResourceStream(string resourceName)
{
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
var assembly = typeof(EmbeddedResourceDataAttribute).Assembly;
resourceName = resourceName.Replace("/", ".", StringComparison.Ordinal);
return assembly.GetManifestResourceStream(resourceName);
}
}
}

View File

@ -0,0 +1,98 @@
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
[UsesVerify]
public sealed class FigletTests
{
[Fact]
public async Task Should_Load_Font_From_Stream()
{
// Given
var console = new PlainConsole(width: 180);
var font = FigletFont.Load(ResourceReader.LoadResourceStream("Spectre.Console.Tests/Data/starwars.flf"));
var text = new FigletText(font, "Patrik was here");
// When
console.Render(text);
// Then
await Verifier.Verify(console.Output);
}
[Fact]
public async Task Should_Render_Text_Correctly()
{
// Given
var console = new PlainConsole(width: 70);
var text = new FigletText(FigletFont.Default, "Patrik was here");
// When
console.Render(text);
// Then
await Verifier.Verify(console.Output);
}
[Fact]
public async Task Should_Render_Wrapped_Text_Correctly()
{
// Given
var console = new PlainConsole(width: 70);
var text = new FigletText(FigletFont.Default, "Spectre.Console");
// When
console.Render(text);
// Then
await Verifier.Verify(console.Output);
}
[Fact]
public async Task Should_Render_Left_Aligned_Text_Correctly()
{
// Given
var console = new PlainConsole(width: 120);
var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Left);
// When
console.Render(text);
// Then
await Verifier.Verify(console.Output);
}
[Fact]
public async Task Should_Render_Centered_Text_Correctly()
{
// Given
var console = new PlainConsole(width: 120);
var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Center);
// When
console.Render(text);
// Then
await Verifier.Verify(console.Output);
}
[Fact]
public async Task Should_Render_Right_Aligned_Text_Correctly()
{
// Given
var console = new PlainConsole(width: 120);
var text = new FigletText(FigletFont.Default, "Spectre.Console")
.Alignment(Justify.Right);
// When
console.Render(text);
// Then
await Verifier.Verify(console.Output);
}
}
}

View File

@ -3,7 +3,7 @@ using System.Runtime.CompilerServices;
namespace Spectre.Console.Tests
{
public static class TestConfig
public static class VerifyConfiguration
{
[ModuleInitializer]
public static void Init()

View File

@ -52,6 +52,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cursor", "..\examples\Curso
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prompt", "..\examples\Prompt\Prompt.csproj", "{6351C70F-F368-46DB-BAED-9B87CCD69353}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Figlet", "..\examples\Figlet\Figlet.csproj", "{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -254,6 +256,18 @@ Global
{6351C70F-F368-46DB-BAED-9B87CCD69353}.Release|x64.Build.0 = Release|Any CPU
{6351C70F-F368-46DB-BAED-9B87CCD69353}.Release|x86.ActiveCfg = Release|Any CPU
{6351C70F-F368-46DB-BAED-9B87CCD69353}.Release|x86.Build.0 = Release|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Debug|x64.ActiveCfg = Debug|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Debug|x64.Build.0 = Debug|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Debug|x86.ActiveCfg = Debug|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Debug|x86.Build.0 = Debug|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Release|Any CPU.Build.0 = Release|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Release|x64.ActiveCfg = Release|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Release|x64.Build.0 = Release|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Release|x86.ActiveCfg = Release|Any CPU
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -274,6 +288,7 @@ Global
{8622A261-02C6-40CA-9797-E3F01ED87D6B} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{75C608C3-ABB4-4168-A229-7F8250B946D1} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{6351C70F-F368-46DB-BAED-9B87CCD69353} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C}

View File

@ -0,0 +1,27 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="FigletText"/>.
/// </summary>
public static class FigletTextExtensions
{
/// <summary>
/// Sets the color of the FIGlet text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static FigletText Color(this FigletText text, Color? color)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
text.Color = color ?? Console.Color.Default;
return text;
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Spectre.Console
{
internal sealed class FigletCharacter
{
public int Code { get; }
public int Width { get; }
public int Height { get; }
public IReadOnlyList<string> Lines { get; }
public FigletCharacter(int code, IEnumerable<string> lines)
{
Code = code;
Lines = new List<string>(lines ?? throw new ArgumentNullException(nameof(lines)));
var min = Lines.Min(x => x.Length);
var max = Lines.Max(x => x.Length);
if (min != max)
{
throw new InvalidOperationException($"Figlet character #{code} has varying width");
}
Width = max;
Height = Lines.Count;
}
}
}

View File

@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.IO;
using Spectre.Console.Internal;
namespace Spectre.Console
{
/// <summary>
/// Represents a FIGlet font.
/// </summary>
public sealed class FigletFont
{
private readonly Dictionary<int, FigletCharacter> _characters;
private static readonly Lazy<FigletFont> _standard;
/// <summary>
/// Gets the number of characters in the font.
/// </summary>
public int Count => _characters.Count;
/// <summary>
/// Gets the height of the font.
/// </summary>
public int Height { get; }
/// <summary>
/// Gets the font's baseline.
/// </summary>
public int Baseline { get; }
/// <summary>
/// Gets the font's maximum width.
/// </summary>
public int MaxWidth { get; }
/// <summary>
/// Gets the default FIGlet font.
/// </summary>
public static FigletFont Default => _standard.Value;
static FigletFont()
{
_standard = new Lazy<FigletFont>(() => Parse(ResourceReader.ReadManifestData("Spectre.Console/Figlet/Fonts/Standard.flf")));
}
internal FigletFont(IEnumerable<FigletCharacter> characters, FigletHeader header)
{
_characters = new Dictionary<int, FigletCharacter>();
foreach (var character in characters)
{
if (_characters.ContainsKey(character.Code))
{
throw new InvalidOperationException("Character already exist");
}
_characters[character.Code] = character;
}
Height = header.Height;
Baseline = header.Baseline;
MaxWidth = header.MaxLength;
}
/// <summary>
/// Loads a FIGlet font from the specified stream.
/// </summary>
/// <param name="stream">The stream to load the FIGlet font from.</param>
/// <returns>The loaded FIGlet font.</returns>
public static FigletFont Load(Stream stream)
{
using (var reader = new StreamReader(stream))
{
return Parse(reader.ReadToEnd());
}
}
/// <summary>
/// Loads a FIGlet font from disk.
/// </summary>
/// <param name="path">The path of the FIGlet font to load.</param>
/// <returns>The loaded FIGlet font.</returns>
public static FigletFont Load(string path)
{
return Parse(File.ReadAllText(path));
}
/// <summary>
/// Parses a FIGlet font from the specified <see cref="string"/>.
/// </summary>
/// <param name="source">The FIGlet font source.</param>
/// <returns>The parsed FIGlet font.</returns>
public static FigletFont Parse(string source)
{
return FigletFontParser.Parse(source);
}
internal int GetWidth(string text)
{
var width = 0;
foreach (var character in text)
{
width += GetCharacter(character)?.Width ?? 0;
}
return width;
}
internal FigletCharacter? GetCharacter(char character)
{
_characters.TryGetValue(character, out var result);
return result;
}
internal IEnumerable<FigletCharacter> GetCharacters(string text)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
var result = new List<FigletCharacter>();
foreach (var character in text)
{
if (_characters.TryGetValue(character, out var figletCharacter))
{
result.Add(figletCharacter);
}
}
return result;
}
}
}

View File

@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Spectre.Console
{
internal static class FigletFontParser
{
public static FigletFont Parse(string source)
{
var lines = source.SplitLines();
var header = ParseHeader(lines.FirstOrDefault());
var characters = new List<FigletCharacter>();
var index = 32;
var indexOverridden = false;
var hasOverriddenIndex = false;
var buffer = new List<string>();
foreach (var line in lines.Skip(header.CommentLines + 1))
{
if (!line.EndsWith("@", StringComparison.Ordinal))
{
var words = line.SplitWords();
if (words.Length > 0 && TryParseIndex(words[0], out var newIndex))
{
index = newIndex;
indexOverridden = true;
hasOverriddenIndex = true;
continue;
}
continue;
}
if (hasOverriddenIndex && !indexOverridden)
{
throw new InvalidOperationException("Unknown index for FIGlet character");
}
buffer.Add(line.Replace('$', ' ').ReplaceExact("@", string.Empty));
if (line.EndsWith("@@", StringComparison.Ordinal))
{
characters.Add(new FigletCharacter(index, buffer));
buffer.Clear();
if (!hasOverriddenIndex)
{
index++;
}
// Reset the flag so we know if we're trying to parse
// a character that wasn't prefixed with an ASCII index.
indexOverridden = false;
}
}
return new FigletFont(characters, header);
}
private static bool TryParseIndex(string index, out int result)
{
var style = NumberStyles.Integer;
if (index.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
// TODO: ReplaceExact should not be used
index = index.ReplaceExact("0x", string.Empty).ReplaceExact("0x", string.Empty);
style = NumberStyles.HexNumber;
}
return int.TryParse(index, style, CultureInfo.InvariantCulture, out result);
}
private static FigletHeader ParseHeader(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
throw new InvalidOperationException("Invalid Figlet font");
}
var parts = text.SplitWords(StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 6)
{
throw new InvalidOperationException("Invalid Figlet font header");
}
if (!IsValidSignature(parts[0]))
{
throw new InvalidOperationException("Invalid Figlet font header signature");
}
return new FigletHeader
{
Hardblank = parts[0][5],
Height = int.Parse(parts[1], CultureInfo.InvariantCulture),
Baseline = int.Parse(parts[2], CultureInfo.InvariantCulture),
MaxLength = int.Parse(parts[3], CultureInfo.InvariantCulture),
OldLayout = int.Parse(parts[4], CultureInfo.InvariantCulture),
CommentLines = int.Parse(parts[5], CultureInfo.InvariantCulture),
};
}
private static bool IsValidSignature(string signature)
{
return signature.Length == 6
&& signature[0] == 'f' && signature[1] == 'l'
&& signature[2] == 'f' && signature[3] == '2'
&& signature[4] == 'a';
}
}
}

View File

@ -0,0 +1,12 @@
namespace Spectre.Console
{
internal sealed class FigletHeader
{
public char Hardblank { get; set; }
public int Height { get; set; }
public int Baseline { get; set; }
public int MaxLength { get; set; }
public int OldLayout { get; set; }
public int CommentLines { get; set; }
}
}

View File

@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents text rendered with a FIGlet font.
/// </summary>
public sealed class FigletText : Renderable, IAlignable
{
private readonly FigletFont _font;
private readonly string _text;
/// <summary>
/// Gets or sets the color of the text.
/// </summary>
public Color? Color { get; set; }
/// <inheritdoc/>
public Justify? Alignment { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="FigletText"/> class.
/// </summary>
/// <param name="text">The text.</param>
public FigletText(string text)
: this(FigletFont.Default, text)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FigletText"/> class.
/// </summary>
/// <param name="font">The FIGlet font to use.</param>
/// <param name="text">The text.</param>
public FigletText(FigletFont font, string text)
{
_font = font ?? throw new ArgumentNullException(nameof(font));
_text = text ?? throw new ArgumentNullException(nameof(text));
}
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
var style = new Style(Color ?? Console.Color.Default);
var alignment = Alignment ?? Justify.Left;
foreach (var row in GetRows(maxWidth))
{
for (var index = 0; index < _font.Height; index++)
{
var line = new Segment(string.Concat(row.Select(x => x.Lines[index])), style);
var lineWidth = line.CellCount(context);
if (alignment == Justify.Left)
{
yield return line;
if (lineWidth < maxWidth)
{
yield return new Segment(new string(' ', maxWidth - lineWidth));
}
}
else if (alignment == Justify.Center)
{
var left = (maxWidth - lineWidth) / 2;
var right = left + ((maxWidth - lineWidth) % 2);
yield return new Segment(new string(' ', left));
yield return line;
yield return new Segment(new string(' ', right));
}
else if (alignment == Justify.Right)
{
if (lineWidth < maxWidth)
{
yield return new Segment(new string(' ', maxWidth - lineWidth));
}
yield return line;
}
yield return Segment.LineBreak;
}
}
}
private List<List<FigletCharacter>> GetRows(int maxWidth)
{
var result = new List<List<FigletCharacter>>();
var words = _text.SplitWords(StringSplitOptions.None);
var totalWidth = 0;
var line = new List<FigletCharacter>();
foreach (var word in words)
{
// Does the whole word fit?
var width = _font.GetWidth(word);
if (width + totalWidth < maxWidth)
{
// Add it to the line
line.AddRange(_font.GetCharacters(word));
totalWidth += width;
}
else
{
// Does it fit on it's own line?
if (width < maxWidth)
{
// Flush the line
result.Add(line);
line = new List<FigletCharacter>();
totalWidth = 0;
line.AddRange(_font.GetCharacters(word));
totalWidth += width;
}
else
{
// We need to split it up.
var queue = new Queue<FigletCharacter>(_font.GetCharacters(word));
while (queue.Count > 0)
{
var current = queue.Dequeue();
if (totalWidth + current.Width > maxWidth)
{
// Flush the line
result.Add(line);
line = new List<FigletCharacter>();
totalWidth = 0;
}
line.Add(current);
totalWidth += current.Width;
}
}
}
}
if (line.Count > 0)
{
result.Add(line);
}
return result;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,32 @@
using System;
using System.IO;
namespace Spectre.Console.Internal
{
internal static class ResourceReader
{
public static string ReadManifestData(string resourceName)
{
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
var assembly = typeof(ResourceReader).Assembly;
resourceName = resourceName.ReplaceExact("/", ".");
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd().NormalizeLineEndings();
}
}
}
}
}

View File

@ -5,8 +5,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="Figlet\Fonts\Standard.flf" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\stylecop.json" Link="Properties/stylecop.json" />
<EmbeddedResource Include="Figlet\Fonts\Standard.flf" />
<None Include="../../resources/gfx/small-logo.png" Pack="true" PackagePath="\" Link="Properties/small-logo.png" />
</ItemGroup>
@ -20,20 +25,6 @@
<PackageReference Include="Wcwidth" Version="0.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="BoxBorder.Known.cs">
<DependentUpon>BoxBorder.cs</DependentUpon>
</Compile>
<Compile Update="TableBorder.Known.cs">
<DependentUpon>TableBorder.cs</DependentUpon>
</Compile>
<Compile Update="Extensions\AnsiConsoleExtensions.Markup.cs">
<DependentUpon>AnsiConsoleExtensions.cs</DependentUpon>
</Compile>
<Compile Update="Extensions\AnsiConsoleExtensions.Rendering.cs">
<DependentUpon>AnsiConsoleExtensions.cs</DependentUpon>
</Compile>
</ItemGroup>
<PropertyGroup>
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>