Rename Style and Appearance

* Renames Style -> Decoration
* Renames Appearance -> Style
* Adds Style.Parse and Style.TryParse
This commit is contained in:
Patrik Svensson
2020-08-03 22:33:08 +02:00
committed by Patrik Svensson
parent c3286a4842
commit 98cf63f485
32 changed files with 691 additions and 405 deletions

View File

@ -5,14 +5,14 @@ namespace Spectre.Console.Internal
{
internal static class MarkupParser
{
public static Text Parse(string text, Appearance appearance = null)
public static Text Parse(string text, Style style = null)
{
appearance ??= Appearance.Plain;
style ??= Style.Plain;
var result = new Text(string.Empty);
using var tokenizer = new MarkupTokenizer(text);
var stack = new Stack<Appearance>();
var stack = new Stack<Style>();
while (tokenizer.MoveNext())
{
@ -20,8 +20,8 @@ namespace Spectre.Console.Internal
if (token.Kind == MarkupTokenKind.Open)
{
var (style, foreground, background) = MarkupStyleParser.Parse(token.Value);
stack.Push(new Appearance(foreground, background, style));
var parsedStyle = StyleParser.Parse(token.Value);
stack.Push(parsedStyle);
}
else if (token.Kind == MarkupTokenKind.Close)
{
@ -35,8 +35,8 @@ namespace Spectre.Console.Internal
else if (token.Kind == MarkupTokenKind.Text)
{
// Get the effecive style.
var style = appearance.Combine(stack);
result.Append(token.Value, style);
var effectiveStyle = style.Combine(stack);
result.Append(token.Value, effectiveStyle);
}
else
{

View File

@ -1,65 +0,0 @@
using System;
namespace Spectre.Console.Internal
{
internal static class MarkupStyleParser
{
public static (Styles? Style, Color? Foreground, Color? Background) Parse(string text)
{
var effectiveStyle = (Styles?)null;
var effectiveForeground = (Color?)null;
var effectiveBackground = (Color?)null;
var parts = text.Split(new[] { ' ' });
var foreground = true;
foreach (var part in parts)
{
if (part.Equals("on", StringComparison.OrdinalIgnoreCase))
{
foreground = false;
continue;
}
var style = StyleTable.GetStyle(part);
if (style != null)
{
if (effectiveStyle == null)
{
effectiveStyle = Styles.None;
}
effectiveStyle |= style.Value;
}
else
{
var color = ColorTable.GetColor(part);
if (color == null)
{
throw new InvalidOperationException("Could not find color..");
}
if (foreground)
{
if (effectiveForeground != null)
{
throw new InvalidOperationException("A foreground has already been set.");
}
effectiveForeground = color;
}
else
{
if (effectiveBackground != null)
{
throw new InvalidOperationException("A background has already been set.");
}
effectiveBackground = color;
}
}
}
return (effectiveStyle, effectiveForeground, effectiveBackground);
}
}
}

View File

@ -0,0 +1,104 @@
using System;
namespace Spectre.Console.Internal
{
internal static class StyleParser
{
public static Style Parse(string text)
{
var style = Parse(text, out var error);
if (error != null)
{
throw new InvalidOperationException(error);
}
return style;
}
public static bool TryParse(string text, out Style style)
{
style = Parse(text, out var error);
if (error != null)
{
return false;
}
return true;
}
private static Style Parse(string text, out string error)
{
var effectiveDecoration = (Decoration?)null;
var effectiveForeground = (Color?)null;
var effectiveBackground = (Color?)null;
var parts = text.Split(new[] { ' ' });
var foreground = true;
foreach (var part in parts)
{
if (part.Equals("default", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (part.Equals("on", StringComparison.OrdinalIgnoreCase))
{
foreground = false;
continue;
}
var decoration = DecorationTable.GetDecoration(part);
if (decoration != null)
{
if (effectiveDecoration == null)
{
effectiveDecoration = Decoration.None;
}
effectiveDecoration |= decoration.Value;
}
else
{
var color = ColorTable.GetColor(part);
if (color == null)
{
if (!foreground)
{
error = $"Could not find color '{part}'.";
}
else
{
error = $"Could not find color or style '{part}'.";
}
return null;
}
if (foreground)
{
if (effectiveForeground != null)
{
error = "A foreground color has already been set.";
return null;
}
effectiveForeground = color;
}
else
{
if (effectiveBackground != null)
{
error = "A background color has already been set.";
return null;
}
effectiveBackground = color;
}
}
}
error = null;
return new Style(effectiveForeground, effectiveBackground, effectiveDecoration);
}
}
}