Add output abstraction and reorganize profile

* Moves ColorSystem from Profile to Capabilities
* Renames Tty to IsTerminal
* Adds IAnsiConsoleOutput to make output more flexible

Closes #343
Closes #359
Closes #369
This commit is contained in:
Patrik Svensson
2021-04-12 18:15:21 +02:00
committed by Phil Scott
parent bc9f610258
commit 3e2eea730b
27 changed files with 194 additions and 139 deletions

View File

@ -29,7 +29,7 @@ namespace Spectre.Console
{
codes = codes.Concat(
AnsiColorBuilder.GetAnsiCodes(
_profile.ColorSystem,
_profile.Capabilities.ColorSystem,
style.Foreground,
true));
}
@ -39,7 +39,7 @@ namespace Spectre.Console
{
codes = codes.Concat(
AnsiColorBuilder.GetAnsiCodes(
_profile.ColorSystem,
_profile.Capabilities.ColorSystem,
style.Background,
false));
}

View File

@ -59,8 +59,8 @@ namespace Spectre.Console
if (builder.Length > 0)
{
_console.Profile.Out.Write(builder.ToString());
_console.Profile.Out.Flush();
_console.Profile.Out.Writer.Write(builder.ToString());
_console.Profile.Out.Writer.Flush();
}
}
}

View File

@ -44,7 +44,7 @@ namespace Spectre.Console
SetStyle(segment.Style);
}
_console.Profile.Out.Write(segment.Text.NormalizeNewLines(native: true));
_console.Profile.Out.Writer.Write(segment.Text.NormalizeNewLines(native: true));
}
}
@ -55,13 +55,13 @@ namespace Spectre.Console
System.Console.ResetColor();
var background = Color.ToConsoleColor(style.Background);
if (_console.Profile.ColorSystem != ColorSystem.NoColors && (int)background != -1)
if (_console.Profile.Capabilities.ColorSystem != ColorSystem.NoColors && (int)background != -1)
{
System.Console.BackgroundColor = background;
}
var foreground = Color.ToConsoleColor(style.Foreground);
if (_console.Profile.ColorSystem != ColorSystem.NoColors && (int)foreground != -1)
if (_console.Profile.Capabilities.ColorSystem != ColorSystem.NoColors && (int)foreground != -1)
{
System.Console.ForegroundColor = foreground;
}

View File

@ -13,11 +13,6 @@ namespace Spectre.Console
public ConsoleKeyInfo ReadKey(bool intercept)
{
if (_profile.Capabilities.Tty)
{
throw new InvalidOperationException("Cannot read input from a TTY console.");
}
if (!_profile.Capabilities.Interactive)
{
throw new InvalidOperationException("Failed to read input in non-interactive mode.");

View File

@ -0,0 +1,19 @@
namespace Spectre.Console.Internal
{
internal sealed class EncoderCapabilities : IReadOnlyCapabilities
{
public ColorSystem ColorSystem { get; }
public bool Ansi => false;
public bool Links => false;
public bool Legacy => false;
public bool IsTerminal => false;
public bool Interactive => false;
public bool Unicode => true;
public EncoderCapabilities(ColorSystem colors)
{
ColorSystem = colors;
}
}
}

View File

@ -9,7 +9,7 @@ namespace Spectre.Console.Internal
{
public string Encode(IAnsiConsole console, IEnumerable<IRenderable> renderables)
{
var context = new RenderContext(ColorSystem.TrueColor, EncoderCapabilities.Default);
var context = new RenderContext(new EncoderCapabilities(ColorSystem.TrueColor));
var builder = new StringBuilder();
builder.Append("<pre style=\"font-size:90%;font-family:consolas,'Courier New',monospace\">\n");

View File

@ -4,23 +4,11 @@ using Spectre.Console.Rendering;
namespace Spectre.Console.Internal
{
internal sealed class EncoderCapabilities : IReadOnlyCapabilities
{
public bool Ansi => false;
public bool Links => false;
public bool Legacy => false;
public bool Tty => false;
public bool Interactive => false;
public bool Unicode => true;
public static EncoderCapabilities Default { get; } = new EncoderCapabilities();
}
internal sealed class TextEncoder : IAnsiConsoleEncoder
{
public string Encode(IAnsiConsole console, IEnumerable<IRenderable> renderables)
{
var context = new RenderContext(ColorSystem.TrueColor, EncoderCapabilities.Default);
var context = new RenderContext(new EncoderCapabilities(ColorSystem.TrueColor));
var builder = new StringBuilder();
foreach (var renderable in renderables)