Make styles composable

Also adds some new extension methods and make some APIs a bit more consistent.

Closes #64
This commit is contained in:
Patrik Svensson
2020-09-03 21:18:52 +02:00
committed by Patrik Svensson
parent bcaaa6b2d3
commit e946289bd9
12 changed files with 296 additions and 97 deletions

View File

@ -8,13 +8,61 @@ namespace Spectre.Console.Rendering
public static class BorderExtensions
{
/// <summary>
/// Sets the border.
/// Do not display a border.
/// </summary>
/// <typeparam name="T">The object that has a border.</typeparam>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border for.</param>
/// <param name="border">The border to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T SetBorder<T>(this T obj, BorderKind border)
public static T NoBorder<T>(this T obj)
where T : class, IHasBorder
{
return SetBorderKind(obj, BorderKind.None);
}
/// <summary>
/// Display a square border.
/// </summary>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border for.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T SquareBorder<T>(this T obj)
where T : class, IHasBorder
{
return SetBorderKind(obj, BorderKind.Square);
}
/// <summary>
/// Display an ASCII border.
/// </summary>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border for.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T AsciiBorder<T>(this T obj)
where T : class, IHasBorder
{
return SetBorderKind(obj, BorderKind.Ascii);
}
/// <summary>
/// Display a rounded border.
/// </summary>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border for.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T RoundedBorder<T>(this T obj)
where T : class, IHasBorder
{
return SetBorderKind(obj, BorderKind.Rounded);
}
/// <summary>
/// Sets the border kind.
/// </summary>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border for.</param>
/// <param name="border">The border kind to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T SetBorderKind<T>(this T obj, BorderKind border)
where T : class, IHasBorder
{
if (obj is null)
@ -22,14 +70,14 @@ namespace Spectre.Console.Rendering
throw new ArgumentNullException(nameof(obj));
}
obj.Border = border;
obj.BorderKind = border;
return obj;
}
/// <summary>
/// Disables the safe border.
/// </summary>
/// <typeparam name="T">The object that has a border.</typeparam>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border for.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T NoSafeBorder<T>(this T obj)
@ -44,12 +92,31 @@ namespace Spectre.Console.Rendering
return obj;
}
/// <summary>
/// Sets the border style.
/// </summary>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border color for.</param>
/// <param name="style">The border style to set.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T SetBorderStyle<T>(this T obj, Style style)
where T : class, IHasBorder
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.BorderStyle = style;
return obj;
}
/// <summary>
/// Sets the border color.
/// </summary>
/// <typeparam name="T">The object that has a border.</typeparam>
/// <typeparam name="T">An object type with a border.</typeparam>
/// <param name="obj">The object to set the border color for.</param>
/// <param name="color">The color to set.</param>
/// <param name="color">The border color to set.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T SetBorderColor<T>(this T obj, Color color)
where T : class, IHasBorder
@ -59,7 +126,7 @@ namespace Spectre.Console.Rendering
throw new ArgumentNullException(nameof(obj));
}
obj.BorderColor = color;
obj.BorderStyle = (obj.BorderStyle ?? Style.Plain).WithForeground(color);
return obj;
}
}

View File

@ -15,11 +15,11 @@ namespace Spectre.Console
/// <summary>
/// Gets or sets the kind of border to use.
/// </summary>
public BorderKind Border { get; set; }
public BorderKind BorderKind { get; set; }
/// <summary>
/// Gets or sets the border color.
/// Gets or sets the border style.
/// </summary>
public Color? BorderColor { get; set; }
public Style? BorderStyle { get; set; }
}
}