mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Remove BorderKind in favour of Border
This commit is contained in:

committed by
Patrik Svensson

parent
e6e9a4d950
commit
87bde3e5a2
30
src/Spectre.Console/Rendering/Border.Known.cs
Normal file
30
src/Spectre.Console/Rendering/Border.Known.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a border.
|
||||
/// </summary>
|
||||
public abstract partial class Border
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an invisible border.
|
||||
/// </summary>
|
||||
public static Border None { get; } = new NoBorder();
|
||||
|
||||
/// <summary>
|
||||
/// Gets an ASCII border.
|
||||
/// </summary>
|
||||
public static Border Ascii { get; } = new AsciiBorder();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a square border.
|
||||
/// </summary>
|
||||
public static Border Square { get; } = new SquareBorder();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a rounded border.
|
||||
/// </summary>
|
||||
public static Border Rounded { get; } = new RoundedBorder();
|
||||
}
|
||||
}
|
@ -2,28 +2,26 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Rendering
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a border used by tables.
|
||||
/// Represents a border.
|
||||
/// </summary>
|
||||
public abstract class Border
|
||||
public abstract partial class Border
|
||||
{
|
||||
private readonly Dictionary<BorderPart, string> _lookup;
|
||||
|
||||
private static readonly Dictionary<BorderKind, Border> _borders = new Dictionary<BorderKind, Border>
|
||||
{
|
||||
{ BorderKind.None, new NoBorder() },
|
||||
{ BorderKind.Ascii, new AsciiBorder() },
|
||||
{ BorderKind.Square, new SquareBorder() },
|
||||
{ BorderKind.Rounded, new RoundedBorder() },
|
||||
};
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the border is visible.
|
||||
/// </summary>
|
||||
public virtual bool Visible { get; } = true;
|
||||
|
||||
private static readonly Dictionary<BorderKind, BorderKind> _safeLookup = new Dictionary<BorderKind, BorderKind>
|
||||
{
|
||||
{ BorderKind.Rounded, BorderKind.Square },
|
||||
};
|
||||
/// <summary>
|
||||
/// Gets the safe border for this border or <c>null</c> if none exist.
|
||||
/// </summary>
|
||||
public virtual Border? SafeBorder { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Border"/> class.
|
||||
@ -33,27 +31,6 @@ namespace Spectre.Console.Rendering
|
||||
_lookup = Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Border"/> represented by the specified <see cref="BorderKind"/>.
|
||||
/// </summary>
|
||||
/// <param name="kind">The kind of border to get.</param>
|
||||
/// <param name="safe">Whether or not to get a "safe" border that can be rendered in a legacy console.</param>
|
||||
/// <returns>A <see cref="Border"/> instance representing the specified <see cref="BorderKind"/>.</returns>
|
||||
public static Border GetBorder(BorderKind kind, bool safe)
|
||||
{
|
||||
if (safe && _safeLookup.TryGetValue(kind, out var safeKind))
|
||||
{
|
||||
kind = safeKind;
|
||||
}
|
||||
|
||||
if (!_borders.TryGetValue(kind, out var border))
|
||||
{
|
||||
throw new InvalidOperationException("Unknown border kind");
|
||||
}
|
||||
|
||||
return border;
|
||||
}
|
||||
|
||||
private Dictionary<BorderPart, string> Initialize()
|
||||
{
|
||||
var lookup = new Dictionary<BorderPart, string>();
|
||||
|
31
src/Spectre.Console/Rendering/BorderExtensions.cs
Normal file
31
src/Spectre.Console/Rendering/BorderExtensions.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="Border"/>.
|
||||
/// </summary>
|
||||
public static class BorderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the safe border for a border.
|
||||
/// </summary>
|
||||
/// <param name="border">The border to get the safe border for.</param>
|
||||
/// <param name="safe">Whether or not to return the safe border.</param>
|
||||
/// <returns>The safe border if one exist, otherwise the original border.</returns>
|
||||
public static Border GetSafeBorder(this Border border, bool safe)
|
||||
{
|
||||
if (border is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(border));
|
||||
}
|
||||
|
||||
if (safe && border.SafeBorder != null)
|
||||
{
|
||||
border = border.SafeBorder;
|
||||
}
|
||||
|
||||
return border;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents different kinds of borders.
|
||||
/// </summary>
|
||||
public enum BorderKind
|
||||
{
|
||||
/// <summary>
|
||||
/// No border.
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// A square border.
|
||||
/// </summary>
|
||||
Square = 1,
|
||||
|
||||
/// <summary>
|
||||
/// An old school ASCII border.
|
||||
/// </summary>
|
||||
Ascii = 2,
|
||||
|
||||
/// <summary>
|
||||
/// A rounded border.
|
||||
/// </summary>
|
||||
Rounded = 3,
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@ namespace Spectre.Console.Rendering
|
||||
/// </summary>
|
||||
public sealed class NoBorder : Border
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool Visible => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override string GetBoxPart(BorderPart part)
|
||||
{
|
||||
|
@ -7,6 +7,9 @@ namespace Spectre.Console.Rendering
|
||||
/// </summary>
|
||||
public sealed class RoundedBorder : Border
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override Border? SafeBorder { get; } = Border.Square;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override string GetBoxPart(BorderPart part)
|
||||
{
|
||||
|
@ -35,7 +35,7 @@ namespace Spectre.Console
|
||||
{
|
||||
_table = new Table
|
||||
{
|
||||
BorderKind = BorderKind.None,
|
||||
Border = Border.None,
|
||||
ShowHeaders = false,
|
||||
IsGrid = true,
|
||||
PadRightCell = false,
|
||||
|
@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
using SpectreBorder = Spectre.Console.Rendering.Border;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
@ -16,10 +15,10 @@ namespace Spectre.Console
|
||||
private readonly IRenderable _child;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public BorderKind BorderKind { get; set; } = BorderKind.Square;
|
||||
public Border Border { get; set; } = Border.Square;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool SafeBorder { get; set; } = true;
|
||||
public bool UseSafeBorder { get; set; } = true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Style? BorderStyle { get; set; }
|
||||
@ -71,7 +70,7 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
var border = SpectreBorder.GetBorder(BorderKind, (context.LegacyConsole || !context.Unicode) && SafeBorder);
|
||||
var border = Border.GetSafeBorder((context.LegacyConsole || !context.Unicode) && UseSafeBorder);
|
||||
var borderStyle = BorderStyle ?? Style.Plain;
|
||||
|
||||
var paddingWidth = Padding.GetHorizontalPadding();
|
||||
@ -132,7 +131,7 @@ namespace Spectre.Console
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void AddBottomBorder(List<Segment> result, SpectreBorder border, Style borderStyle, int panelWidth)
|
||||
private static void AddBottomBorder(List<Segment> result, Border border, Style borderStyle, int panelWidth)
|
||||
{
|
||||
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomLeft), borderStyle));
|
||||
result.Add(new Segment(border.GetPart(BorderPart.FooterBottom, panelWidth - EdgeWidth), borderStyle));
|
||||
@ -140,7 +139,7 @@ namespace Spectre.Console
|
||||
result.Add(Segment.LineBreak);
|
||||
}
|
||||
|
||||
private void AddTopBorder(List<Segment> segments, RenderContext context, SpectreBorder border, Style borderStyle, int panelWidth)
|
||||
private void AddTopBorder(List<Segment> segments, RenderContext context, Border border, Style borderStyle, int panelWidth)
|
||||
{
|
||||
segments.Add(new Segment(border.GetPart(BorderPart.HeaderTopLeft), borderStyle));
|
||||
|
||||
|
@ -117,7 +117,7 @@ namespace Spectre.Console
|
||||
|
||||
private int GetExtraWidth(bool includePadding)
|
||||
{
|
||||
var hideBorder = BorderKind == BorderKind.None;
|
||||
var hideBorder = !Border.Visible;
|
||||
var separators = hideBorder ? 0 : _columns.Count - 1;
|
||||
var edges = hideBorder ? 0 : EdgeCount;
|
||||
var padding = includePadding ? _columns.Select(x => x.Padding.GetHorizontalPadding()).Sum() : 0;
|
||||
|
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal;
|
||||
using Spectre.Console.Rendering;
|
||||
using SpectreBorder = Spectre.Console.Rendering.Border;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
@ -26,13 +25,13 @@ namespace Spectre.Console
|
||||
public int RowCount => _rows.Count;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public BorderKind BorderKind { get; set; } = BorderKind.Square;
|
||||
public Border Border { get; set; } = Border.Square;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Style? BorderStyle { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool SafeBorder { get; set; } = true;
|
||||
public bool UseSafeBorder { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not table headers should be shown.
|
||||
@ -148,13 +147,13 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var border = SpectreBorder.GetBorder(BorderKind, (context.LegacyConsole || !context.Unicode) && SafeBorder);
|
||||
var border = Border.GetSafeBorder((context.LegacyConsole || !context.Unicode) && UseSafeBorder);
|
||||
var borderStyle = BorderStyle ?? Style.Plain;
|
||||
|
||||
var tableWidth = maxWidth;
|
||||
|
||||
var showBorder = BorderKind != BorderKind.None;
|
||||
var hideBorder = BorderKind == BorderKind.None;
|
||||
var showBorder = Border.Visible;
|
||||
var hideBorder = !Border.Visible;
|
||||
var hasRows = _rows.Count > 0;
|
||||
|
||||
if (Width != null)
|
||||
|
@ -16,7 +16,7 @@ namespace Spectre.Console
|
||||
public static T NoBorder<T>(this T obj)
|
||||
where T : class, IHasBorder
|
||||
{
|
||||
return SetBorderKind(obj, BorderKind.None);
|
||||
return SetBorder(obj, Border.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -28,7 +28,7 @@ namespace Spectre.Console
|
||||
public static T SquareBorder<T>(this T obj)
|
||||
where T : class, IHasBorder
|
||||
{
|
||||
return SetBorderKind(obj, BorderKind.Square);
|
||||
return SetBorder(obj, Border.Square);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -40,7 +40,7 @@ namespace Spectre.Console
|
||||
public static T AsciiBorder<T>(this T obj)
|
||||
where T : class, IHasBorder
|
||||
{
|
||||
return SetBorderKind(obj, BorderKind.Ascii);
|
||||
return SetBorder(obj, Border.Ascii);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -52,17 +52,17 @@ namespace Spectre.Console
|
||||
public static T RoundedBorder<T>(this T obj)
|
||||
where T : class, IHasBorder
|
||||
{
|
||||
return SetBorderKind(obj, BorderKind.Rounded);
|
||||
return SetBorder(obj, Border.Rounded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the border kind.
|
||||
/// Sets the border.
|
||||
/// </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>
|
||||
/// <param name="border">The border 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)
|
||||
public static T SetBorder<T>(this T obj, Border border)
|
||||
where T : class, IHasBorder
|
||||
{
|
||||
if (obj is null)
|
||||
@ -70,7 +70,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.BorderKind = border;
|
||||
obj.Border = border;
|
||||
return obj;
|
||||
}
|
||||
|
||||
@ -88,7 +88,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.SafeBorder = false;
|
||||
obj.UseSafeBorder = false;
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
@ -10,12 +10,12 @@ namespace Spectre.Console
|
||||
/// a "safe" border on legacy consoles that might not be able
|
||||
/// to render non-ASCII characters.
|
||||
/// </summary>
|
||||
bool SafeBorder { get; set; }
|
||||
bool UseSafeBorder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the kind of border to use.
|
||||
/// Gets or sets the border.
|
||||
/// </summary>
|
||||
public BorderKind BorderKind { get; set; }
|
||||
public Border Border { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the border style.
|
||||
|
Reference in New Issue
Block a user