Add support for tables

This commit is contained in:
Patrik Svensson
2020-08-04 16:05:57 +02:00
committed by Patrik Svensson
parent aa34c145b9
commit a068fc68c3
19 changed files with 837 additions and 33 deletions

View File

@ -32,12 +32,12 @@ namespace Spectre.Console.Internal
new Regex("bvterm"), // Bitvise SSH Client
};
public static bool Detect(bool upgrade)
public static (bool SupportsAnsi, bool LegacyConsole) Detect(bool upgrade)
{
// Github action doesn't setup a correct PTY but supports ANSI.
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("GITHUB_ACTION")))
{
return true;
return (true, false);
}
// Running on Windows?
@ -47,10 +47,11 @@ namespace Spectre.Console.Internal
var conEmu = Environment.GetEnvironmentVariable("ConEmuANSI");
if (!string.IsNullOrEmpty(conEmu) && conEmu.Equals("On", StringComparison.OrdinalIgnoreCase))
{
return true;
return (true, false);
}
return Windows.SupportsAnsi(upgrade);
var supportsAnsi = Windows.SupportsAnsi(upgrade, out var legacyConsole);
return (supportsAnsi, legacyConsole);
}
// Check if the terminal is of type ANSI/VT100/xterm compatible.
@ -59,11 +60,11 @@ namespace Spectre.Console.Internal
{
if (_regexes.Any(regex => regex.IsMatch(term)))
{
return true;
return (true, false);
}
}
return false;
return (false, true);
}
[SuppressMessage("Design", "CA1060:Move pinvokes to native methods class")]
@ -91,8 +92,10 @@ namespace Spectre.Console.Internal
public static extern uint GetLastError();
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
public static bool SupportsAnsi(bool upgrade)
public static bool SupportsAnsi(bool upgrade, out bool isLegacy)
{
isLegacy = false;
try
{
var @out = GetStdHandle(STD_OUTPUT_HANDLE);
@ -104,6 +107,8 @@ namespace Spectre.Console.Internal
if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)
{
isLegacy = true;
if (!upgrade)
{
return false;

View File

@ -41,12 +41,12 @@ namespace Spectre.Console.Internal
}
}
public AnsiConsoleRenderer(TextWriter @out, ColorSystem system)
public AnsiConsoleRenderer(TextWriter @out, ColorSystem system, bool legacyConsole)
{
_out = @out ?? throw new ArgumentNullException(nameof(@out));
_system = system;
Capabilities = new Capabilities(true, system);
Capabilities = new Capabilities(true, system, legacyConsole);
Encoding = @out.IsStandardOut() ? System.Console.OutputEncoding : Encoding.UTF8;
Foreground = Color.Default;
Background = Color.Default;

View File

@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
namespace Spectre.Console.Internal
{
@ -13,9 +14,41 @@ namespace Spectre.Console.Internal
var buffer = settings.Out ?? System.Console.Out;
var supportsAnsi = settings.Ansi == AnsiSupport.Detect
? AnsiDetector.Detect(true)
: settings.Ansi == AnsiSupport.Yes;
var supportsAnsi = settings.Ansi == AnsiSupport.Yes;
var legacyConsole = false;
if (settings.Ansi == AnsiSupport.Detect)
{
(supportsAnsi, legacyConsole) = AnsiDetector.Detect(true);
// Check whether or not this is a legacy console from the existing instance (if any).
// We need to do this because once we upgrade the console to support ENABLE_VIRTUAL_TERMINAL_PROCESSING
// on Windows, there is no way of detecting whether or not we're running on a legacy console or not.
if (AnsiConsole.Created && !legacyConsole && buffer.IsStandardOut() && AnsiConsole.Capabilities.LegacyConsole)
{
legacyConsole = AnsiConsole.Capabilities.LegacyConsole;
}
}
else
{
if (buffer.IsStandardOut())
{
// Are we running on Windows?
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Not the first console we're creating?
if (AnsiConsole.Created)
{
legacyConsole = AnsiConsole.Capabilities.LegacyConsole;
}
else
{
// Try detecting whether or not this
(_, legacyConsole) = AnsiDetector.Detect(false);
}
}
}
}
var colorSystem = settings.ColorSystem == ColorSystemSupport.Detect
? ColorSystemDetector.Detect(supportsAnsi)
@ -23,13 +56,13 @@ namespace Spectre.Console.Internal
if (supportsAnsi)
{
return new AnsiConsoleRenderer(buffer, colorSystem)
return new AnsiConsoleRenderer(buffer, colorSystem, legacyConsole)
{
Decoration = Decoration.None,
};
}
return new FallbackConsoleRenderer(buffer, colorSystem);
return new FallbackConsoleRenderer(buffer, colorSystem, legacyConsole);
}
}
}

View File

@ -85,7 +85,7 @@ namespace Spectre.Console.Internal
}
}
public FallbackConsoleRenderer(TextWriter @out, ColorSystem system)
public FallbackConsoleRenderer(TextWriter @out, ColorSystem system, bool legacyConsole)
{
_out = @out;
_system = system;
@ -105,7 +105,7 @@ namespace Spectre.Console.Internal
Encoding = Encoding.UTF8;
}
Capabilities = new Capabilities(false, _system);
Capabilities = new Capabilities(false, _system, legacyConsole);
}
public void Write(string text)

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Spectre.Console.Internal
{
@ -35,7 +36,7 @@ namespace Spectre.Console.Internal
else if (token.Kind == MarkupTokenKind.Text)
{
// Get the effecive style.
var effectiveStyle = style.Combine(stack);
var effectiveStyle = style.Combine(stack.Reverse());
result.Append(token.Value, effectiveStyle);
}
else

View File

@ -18,12 +18,7 @@ namespace Spectre.Console.Internal
public static bool TryParse(string text, out Style style)
{
style = Parse(text, out var error);
if (error != null)
{
return false;
}
return true;
return error == null;
}
private static Style Parse(string text, out string error)