diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1d42f6c..9ac07be 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -64,7 +64,7 @@ jobs: shell: bash run: | dotnet tool restore - dotnet example diagnostic + dotnet example info dotnet example table dotnet example grid dotnet example panel diff --git a/examples/Columns/Program.cs b/examples/Columns/Program.cs index 57f0ad6..ebc8098 100644 --- a/examples/Columns/Program.cs +++ b/examples/Columns/Program.cs @@ -1,6 +1,4 @@ using System.Collections.Generic; -using System.IO; -using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json.Linq; diff --git a/examples/Panel/Program.cs b/examples/Panel/Program.cs index 4f76421..3c4e112 100644 --- a/examples/Panel/Program.cs +++ b/examples/Panel/Program.cs @@ -1,5 +1,4 @@ using Spectre.Console; -using Spectre.Console.Rendering; namespace PanelExample { @@ -14,7 +13,7 @@ namespace PanelExample AnsiConsole.Render( new Panel( new Panel(content) - .SetBorderKind(BorderKind.Rounded))); + .SetBorder(Border.Rounded))); // Left adjusted panel with text AnsiConsole.Render( diff --git a/examples/Table/Program.cs b/examples/Table/Program.cs index 23dd678..27549ff 100644 --- a/examples/Table/Program.cs +++ b/examples/Table/Program.cs @@ -35,7 +35,7 @@ namespace TableExample private static void RenderBigTable() { // Create the table. - var table = new Table().SetBorderKind(BorderKind.Rounded); + var table = new Table().SetBorder(Border.Rounded); table.AddColumn("[red underline]Foo[/]"); table.AddColumn(new TableColumn("[blue]Bar[/]") { Alignment = Justify.Right, NoWrap = true }); @@ -57,7 +57,7 @@ namespace TableExample private static void RenderNestedTable() { // Create simple table. - var simple = new Table().SetBorderKind(BorderKind.Rounded).SetBorderColor(Color.Red); + var simple = new Table().SetBorder(Border.Rounded).SetBorderColor(Color.Red); simple.AddColumn(new TableColumn("[u]Foo[/]").Centered()); simple.AddColumn(new TableColumn("[u]Bar[/]")); simple.AddColumn(new TableColumn("[u]Baz[/]")); @@ -66,7 +66,7 @@ namespace TableExample simple.AddRow("[blue]Hej[/]", "[yellow]Världen![/]", ""); // Create other table. - var second = new Table().SetBorderKind(BorderKind.Square).SetBorderColor(Color.Green); + var second = new Table().SetBorder(Border.Square).SetBorderColor(Color.Green); second.AddColumn(new TableColumn("[u]Foo[/]")); second.AddColumn(new TableColumn("[u]Bar[/]")); second.AddColumn(new TableColumn("[u]Baz[/]")); @@ -74,7 +74,7 @@ namespace TableExample second.AddRow(simple, new Text("Whaaat"), new Text("Lolz")); second.AddRow("[blue]Hej[/]", "[yellow]Världen![/]", ""); - var table = new Table().SetBorderKind(BorderKind.Rounded); + var table = new Table().SetBorder(Border.Rounded); table.AddColumn(new TableColumn(new Panel("[u]Foo[/]").SetBorderColor(Color.Red))); table.AddColumn(new TableColumn(new Panel("[u]Bar[/]").SetBorderColor(Color.Green))); table.AddColumn(new TableColumn(new Panel("[u]Baz[/]").SetBorderColor(Color.Blue))); diff --git a/src/Spectre.Console.Tests/Unit/BorderTests.cs b/src/Spectre.Console.Tests/Unit/BorderTests.cs index e70b664..e179f10 100644 --- a/src/Spectre.Console.Tests/Unit/BorderTests.cs +++ b/src/Spectre.Console.Tests/Unit/BorderTests.cs @@ -1,41 +1,112 @@ -using System; using Shouldly; -using Spectre.Console.Rendering; using Xunit; +using Spectre.Console.Rendering; namespace Spectre.Console.Tests.Unit { public sealed class BorderTests { - public sealed class TheGetBorderMethod + public sealed class NoBorder { - [Theory] - [InlineData(BorderKind.None, false, typeof(NoBorder))] - [InlineData(BorderKind.Ascii, false, typeof(AsciiBorder))] - [InlineData(BorderKind.Square, false, typeof(SquareBorder))] - [InlineData(BorderKind.Rounded, false, typeof(RoundedBorder))] - [InlineData(BorderKind.None, true, typeof(NoBorder))] - [InlineData(BorderKind.Ascii, true, typeof(AsciiBorder))] - [InlineData(BorderKind.Square, true, typeof(SquareBorder))] - [InlineData(BorderKind.Rounded, true, typeof(SquareBorder))] - public void Should_Return_Correct_Border_For_Specified_Kind(BorderKind kind, bool safe, Type expected) + [Fact] + public void Should_Return_Correct_Visibility() { // Given, When - var result = Border.GetBorder(kind, safe); + var visibility = Border.None.Visible; // Then - result.ShouldBeOfType(expected); + visibility.ShouldBeFalse(); } + public sealed class TheSafeGetBorderMethod + { + [Fact] + public void Should_Return_Safe_Border() + { + // Given, When + var border = Border.None.GetSafeBorder(safe: true); + + // Then + border.ShouldBeSameAs(Border.None); + } + } + } + + public sealed class AsciiBorder + { [Fact] - public void Should_Throw_If_Unknown_Border_Kind_Is_Specified() + public void Should_Return_Correct_Visibility() { // Given, When - var result = Record.Exception(() => Border.GetBorder((BorderKind)int.MaxValue, false)); + var visibility = Border.Ascii.Visible; // Then - result.ShouldBeOfType(); - result.Message.ShouldBe("Unknown border kind"); + visibility.ShouldBeTrue(); + } + + public sealed class TheSafeGetBorderMethod + { + [Fact] + public void Should_Return_Safe_Border() + { + // Given, When + var border = Border.Ascii.GetSafeBorder(safe: true); + + // Then + border.ShouldBeSameAs(Border.Ascii); + } + } + } + + public sealed class SquareBorder + { + [Fact] + public void Should_Return_Correct_Visibility() + { + // Given, When + var visibility = Border.Square.Visible; + + // Then + visibility.ShouldBeTrue(); + } + + public sealed class TheSafeGetBorderMethod + { + [Fact] + public void Should_Return_Safe_Border() + { + // Given, When + var border = Border.Square.GetSafeBorder(safe: true); + + // Then + border.ShouldBeSameAs(Border.Square); + } + } + } + + public sealed class RoundedBorder + { + [Fact] + public void Should_Return_Correct_Visibility() + { + // Given, When + var visibility = Border.Rounded.Visible; + + // Then + visibility.ShouldBeTrue(); + } + + public sealed class TheSafeGetBorderMethod + { + [Fact] + public void Should_Return_Safe_Border() + { + // Given, When + var border = Border.Rounded.GetSafeBorder(safe: true); + + // Then + border.ShouldBeSameAs(Border.Square); + } } } } diff --git a/src/Spectre.Console.Tests/Unit/TableTests.cs b/src/Spectre.Console.Tests/Unit/TableTests.cs index ca0fde5..c9426fe 100644 --- a/src/Spectre.Console.Tests/Unit/TableTests.cs +++ b/src/Spectre.Console.Tests/Unit/TableTests.cs @@ -173,7 +173,7 @@ namespace Spectre.Console.Tests.Unit { // A simple table var console = new PlainConsole(width: 80); - var table = new Table() { BorderKind = BorderKind.Rounded }; + var table = new Table() { Border = Border.Rounded }; table.AddColumn("Foo"); table.AddColumn("Bar"); table.AddColumn(new TableColumn("Baz") { Alignment = Justify.Right }); @@ -183,7 +183,7 @@ namespace Spectre.Console.Tests.Unit // Render a table in some panels. console.Render(new Panel(new Panel(table) { - BorderKind = BorderKind.Ascii, + Border = Border.Ascii, })); // Then @@ -255,7 +255,7 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new PlainConsole(width: 80); - var table = new Table { BorderKind = BorderKind.Ascii }; + var table = new Table { Border = Border.Ascii }; table.AddColumns("Foo", "Bar", "Baz"); table.AddRow("Qux", "Corgi", "Waldo"); table.AddRow("Grault", "Garply", "Fred"); @@ -278,7 +278,7 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new PlainConsole(width: 80); - var table = new Table { BorderKind = BorderKind.Rounded }; + var table = new Table { Border = Border.Rounded }; table.AddColumns("Foo", "Bar", "Baz"); table.AddRow("Qux", "Corgi", "Waldo"); table.AddRow("Grault", "Garply", "Fred"); @@ -301,7 +301,7 @@ namespace Spectre.Console.Tests.Unit { // Given var console = new PlainConsole(width: 80); - var table = new Table { BorderKind = BorderKind.None }; + var table = new Table { Border = Border.None }; table.AddColumns("Foo", "Bar", "Baz"); table.AddRow("Qux", "Corgi", "Waldo"); table.AddRow("Grault", "Garply", "Fred"); diff --git a/src/Spectre.Console/Rendering/Border.Known.cs b/src/Spectre.Console/Rendering/Border.Known.cs new file mode 100644 index 0000000..ff598f3 --- /dev/null +++ b/src/Spectre.Console/Rendering/Border.Known.cs @@ -0,0 +1,30 @@ +using Spectre.Console.Rendering; + +namespace Spectre.Console +{ + /// + /// Represents a border. + /// + public abstract partial class Border + { + /// + /// Gets an invisible border. + /// + public static Border None { get; } = new NoBorder(); + + /// + /// Gets an ASCII border. + /// + public static Border Ascii { get; } = new AsciiBorder(); + + /// + /// Gets a square border. + /// + public static Border Square { get; } = new SquareBorder(); + + /// + /// Gets a rounded border. + /// + public static Border Rounded { get; } = new RoundedBorder(); + } +} diff --git a/src/Spectre.Console/Rendering/Border.cs b/src/Spectre.Console/Rendering/Border.cs index 22ead5d..45355ea 100644 --- a/src/Spectre.Console/Rendering/Border.cs +++ b/src/Spectre.Console/Rendering/Border.cs @@ -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 { /// - /// Represents a border used by tables. + /// Represents a border. /// - public abstract class Border + public abstract partial class Border { private readonly Dictionary _lookup; - private static readonly Dictionary _borders = new Dictionary - { - { BorderKind.None, new NoBorder() }, - { BorderKind.Ascii, new AsciiBorder() }, - { BorderKind.Square, new SquareBorder() }, - { BorderKind.Rounded, new RoundedBorder() }, - }; + /// + /// Gets a value indicating whether or not the border is visible. + /// + public virtual bool Visible { get; } = true; - private static readonly Dictionary _safeLookup = new Dictionary - { - { BorderKind.Rounded, BorderKind.Square }, - }; + /// + /// Gets the safe border for this border or null if none exist. + /// + public virtual Border? SafeBorder { get; } /// /// Initializes a new instance of the class. @@ -33,27 +31,6 @@ namespace Spectre.Console.Rendering _lookup = Initialize(); } - /// - /// Gets a represented by the specified . - /// - /// The kind of border to get. - /// Whether or not to get a "safe" border that can be rendered in a legacy console. - /// A instance representing the specified . - 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 Initialize() { var lookup = new Dictionary(); diff --git a/src/Spectre.Console/Rendering/BorderExtensions.cs b/src/Spectre.Console/Rendering/BorderExtensions.cs new file mode 100644 index 0000000..6357044 --- /dev/null +++ b/src/Spectre.Console/Rendering/BorderExtensions.cs @@ -0,0 +1,31 @@ +using System; + +namespace Spectre.Console.Rendering +{ + /// + /// Contains extension methods for . + /// + public static class BorderExtensions + { + /// + /// Gets the safe border for a border. + /// + /// The border to get the safe border for. + /// Whether or not to return the safe border. + /// The safe border if one exist, otherwise the original border. + 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; + } + } +} diff --git a/src/Spectre.Console/Rendering/BorderKind.cs b/src/Spectre.Console/Rendering/BorderKind.cs deleted file mode 100644 index 37bf537..0000000 --- a/src/Spectre.Console/Rendering/BorderKind.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace Spectre.Console -{ - /// - /// Represents different kinds of borders. - /// - public enum BorderKind - { - /// - /// No border. - /// - None = 0, - - /// - /// A square border. - /// - Square = 1, - - /// - /// An old school ASCII border. - /// - Ascii = 2, - - /// - /// A rounded border. - /// - Rounded = 3, - } -} diff --git a/src/Spectre.Console/Rendering/Borders/NoBorder.cs b/src/Spectre.Console/Rendering/Borders/NoBorder.cs index c1eba8a..2350e7d 100644 --- a/src/Spectre.Console/Rendering/Borders/NoBorder.cs +++ b/src/Spectre.Console/Rendering/Borders/NoBorder.cs @@ -5,6 +5,9 @@ namespace Spectre.Console.Rendering /// public sealed class NoBorder : Border { + /// + public override bool Visible => false; + /// protected override string GetBoxPart(BorderPart part) { diff --git a/src/Spectre.Console/Rendering/Borders/RoundedBorder.cs b/src/Spectre.Console/Rendering/Borders/RoundedBorder.cs index 9121ee2..33aa634 100644 --- a/src/Spectre.Console/Rendering/Borders/RoundedBorder.cs +++ b/src/Spectre.Console/Rendering/Borders/RoundedBorder.cs @@ -7,6 +7,9 @@ namespace Spectre.Console.Rendering /// public sealed class RoundedBorder : Border { + /// + public override Border? SafeBorder { get; } = Border.Square; + /// protected override string GetBoxPart(BorderPart part) { diff --git a/src/Spectre.Console/Rendering/Grid.cs b/src/Spectre.Console/Rendering/Grid.cs index cab140c..3a7d1a1 100644 --- a/src/Spectre.Console/Rendering/Grid.cs +++ b/src/Spectre.Console/Rendering/Grid.cs @@ -35,7 +35,7 @@ namespace Spectre.Console { _table = new Table { - BorderKind = BorderKind.None, + Border = Border.None, ShowHeaders = false, IsGrid = true, PadRightCell = false, diff --git a/src/Spectre.Console/Rendering/Panel.cs b/src/Spectre.Console/Rendering/Panel.cs index 2682e7d..87dd524 100644 --- a/src/Spectre.Console/Rendering/Panel.cs +++ b/src/Spectre.Console/Rendering/Panel.cs @@ -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; /// - public BorderKind BorderKind { get; set; } = BorderKind.Square; + public Border Border { get; set; } = Border.Square; /// - public bool SafeBorder { get; set; } = true; + public bool UseSafeBorder { get; set; } = true; /// public Style? BorderStyle { get; set; } @@ -71,7 +70,7 @@ namespace Spectre.Console /// protected override IEnumerable 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 result, SpectreBorder border, Style borderStyle, int panelWidth) + private static void AddBottomBorder(List 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 segments, RenderContext context, SpectreBorder border, Style borderStyle, int panelWidth) + private void AddTopBorder(List segments, RenderContext context, Border border, Style borderStyle, int panelWidth) { segments.Add(new Segment(border.GetPart(BorderPart.HeaderTopLeft), borderStyle)); diff --git a/src/Spectre.Console/Rendering/Table.Calculations.cs b/src/Spectre.Console/Rendering/Table.Calculations.cs index d75ac7d..94de599 100644 --- a/src/Spectre.Console/Rendering/Table.Calculations.cs +++ b/src/Spectre.Console/Rendering/Table.Calculations.cs @@ -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; diff --git a/src/Spectre.Console/Rendering/Table.cs b/src/Spectre.Console/Rendering/Table.cs index 55d2f82..6d65173 100644 --- a/src/Spectre.Console/Rendering/Table.cs +++ b/src/Spectre.Console/Rendering/Table.cs @@ -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; /// - public BorderKind BorderKind { get; set; } = BorderKind.Square; + public Border Border { get; set; } = Border.Square; /// public Style? BorderStyle { get; set; } /// - public bool SafeBorder { get; set; } = true; + public bool UseSafeBorder { get; set; } = true; /// /// 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) diff --git a/src/Spectre.Console/Rendering/Traits/Extensions/BorderExtensions.cs b/src/Spectre.Console/Rendering/Traits/Extensions/BorderExtensions.cs index d66e7fa..314541b 100644 --- a/src/Spectre.Console/Rendering/Traits/Extensions/BorderExtensions.cs +++ b/src/Spectre.Console/Rendering/Traits/Extensions/BorderExtensions.cs @@ -16,7 +16,7 @@ namespace Spectre.Console public static T NoBorder(this T obj) where T : class, IHasBorder { - return SetBorderKind(obj, BorderKind.None); + return SetBorder(obj, Border.None); } /// @@ -28,7 +28,7 @@ namespace Spectre.Console public static T SquareBorder(this T obj) where T : class, IHasBorder { - return SetBorderKind(obj, BorderKind.Square); + return SetBorder(obj, Border.Square); } /// @@ -40,7 +40,7 @@ namespace Spectre.Console public static T AsciiBorder(this T obj) where T : class, IHasBorder { - return SetBorderKind(obj, BorderKind.Ascii); + return SetBorder(obj, Border.Ascii); } /// @@ -52,17 +52,17 @@ namespace Spectre.Console public static T RoundedBorder(this T obj) where T : class, IHasBorder { - return SetBorderKind(obj, BorderKind.Rounded); + return SetBorder(obj, Border.Rounded); } /// - /// Sets the border kind. + /// Sets the border. /// /// An object type with a border. /// The object to set the border for. - /// The border kind to use. + /// The border to use. /// The same instance so that multiple calls can be chained. - public static T SetBorderKind(this T obj, BorderKind border) + public static T SetBorder(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; } diff --git a/src/Spectre.Console/Rendering/Traits/IHasBorder.cs b/src/Spectre.Console/Rendering/Traits/IHasBorder.cs index 48831f0..cde0c95 100644 --- a/src/Spectre.Console/Rendering/Traits/IHasBorder.cs +++ b/src/Spectre.Console/Rendering/Traits/IHasBorder.cs @@ -10,12 +10,12 @@ namespace Spectre.Console /// a "safe" border on legacy consoles that might not be able /// to render non-ASCII characters. /// - bool SafeBorder { get; set; } + bool UseSafeBorder { get; set; } /// - /// Gets or sets the kind of border to use. + /// Gets or sets the border. /// - public BorderKind BorderKind { get; set; } + public Border Border { get; set; } /// /// Gets or sets the border style.