Add implicit conversion op from string to Style

Closes #71
This commit is contained in:
Patrik Svensson 2020-09-06 22:58:35 +02:00 committed by Patrik Svensson
parent 87bde3e5a2
commit 3cc19520b0

View File

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Spectre.Console.Internal;
namespace Spectre.Console
@ -47,16 +48,6 @@ namespace Spectre.Console
Decoration = decoration ?? Decoration.None;
}
/// <summary>
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
/// </summary>
/// <param name="text">A string containing a style to parse.</param>
/// <returns>A <see cref="Style"/> equivalent of the text contained in <paramref name="text"/>.</returns>
public static Style Parse(string text)
{
return StyleParser.Parse(text);
}
/// <summary>
/// Creates a copy of the current <see cref="Style"/>.
/// </summary>
@ -66,21 +57,6 @@ namespace Spectre.Console
return new Style(Foreground, Background, Decoration);
}
/// <summary>
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="text">A string containing a style to parse.</param>
/// <param name="result">
/// When this method returns, contains the <see cref="Style"/> equivalent of the text contained in <paramref name="text"/>,
/// if the conversion succeeded, or <c>null</c> if the conversion failed.
/// </param>
/// <returns><c>true</c> if s was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(string text, out Style? result)
{
return StyleParser.TryParse(text, out result);
}
/// <summary>
/// Combines this style with another one.
/// </summary>
@ -108,6 +84,41 @@ namespace Spectre.Console
return new Style(foreground, background, Decoration | other.Decoration);
}
/// <summary>
/// Implicitly converts <see cref="string"/> into a <see cref="Style"/>.
/// </summary>
/// <param name="style">The style string.</param>
[SuppressMessage("Usage", "CA2225:Operator overloads have named alternates")]
public static explicit operator Style(string style)
{
return Parse(style);
}
/// <summary>
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
/// </summary>
/// <param name="text">A string containing a style to parse.</param>
/// <returns>A <see cref="Style"/> equivalent of the text contained in <paramref name="text"/>.</returns>
public static Style Parse(string text)
{
return StyleParser.Parse(text);
}
/// <summary>
/// Converts the string representation of a style to its <see cref="Style"/> equivalent.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="text">A string containing a style to parse.</param>
/// <param name="result">
/// When this method returns, contains the <see cref="Style"/> equivalent of the text contained in <paramref name="text"/>,
/// if the conversion succeeded, or <c>null</c> if the conversion failed.
/// </param>
/// <returns><c>true</c> if s was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(string text, out Style? result)
{
return StyleParser.TryParse(text, out result);
}
/// <inheritdoc/>
public override int GetHashCode()
{