diff --git a/src/Spectre.Console/Rendering/Grid.cs b/src/Spectre.Console/Rendering/Grid.cs index f261a26..cab140c 100644 --- a/src/Spectre.Console/Rendering/Grid.cs +++ b/src/Spectre.Console/Rendering/Grid.cs @@ -57,16 +57,19 @@ namespace Spectre.Console /// /// Adds a column to the grid. /// - public void AddColumn() + /// The same instance so that multiple calls can be chained. + public Grid AddColumn() { AddColumn(new GridColumn()); + return this; } /// /// Adds a column to the grid. /// /// The column to add. - public void AddColumn(GridColumn column) + /// The same instance so that multiple calls can be chained. + public Grid AddColumn(GridColumn column) { if (column is null) { @@ -88,13 +91,16 @@ namespace Spectre.Console Padding = column.Padding, Alignment = column.Alignment, }); + + return this; } /// /// Adds a new row to the grid. /// /// The columns to add. - public void AddRow(params IRenderable[] columns) + /// The same instance so that multiple calls can be chained. + public Grid AddRow(params IRenderable[] columns) { if (columns is null) { @@ -107,6 +113,7 @@ namespace Spectre.Console } _table.AddRow(columns); + return this; } } } diff --git a/src/Spectre.Console/Rendering/GridExtensions.cs b/src/Spectre.Console/Rendering/GridExtensions.cs index 9508eb3..710d585 100644 --- a/src/Spectre.Console/Rendering/GridExtensions.cs +++ b/src/Spectre.Console/Rendering/GridExtensions.cs @@ -15,7 +15,8 @@ namespace Spectre.Console /// /// The grid to add the column to. /// The number of columns to add. - public static void AddColumns(this Grid grid, int count) + /// The same instance so that multiple calls can be chained. + public static Grid AddColumns(this Grid grid, int count) { if (grid is null) { @@ -26,6 +27,8 @@ namespace Spectre.Console { grid.AddColumn(new GridColumn()); } + + return grid; } /// @@ -33,7 +36,8 @@ namespace Spectre.Console /// /// The grid to add the column to. /// The columns to add. - public static void AddColumns(this Grid grid, params GridColumn[] columns) + /// The same instance so that multiple calls can be chained. + public static Grid AddColumns(this Grid grid, params GridColumn[] columns) { if (grid is null) { @@ -49,13 +53,16 @@ namespace Spectre.Console { grid.AddColumn(column); } + + return grid; } /// /// Adds an empty row to the grid. /// /// The grid to add the row to. - public static void AddEmptyRow(this Grid grid) + /// The same instance so that multiple calls can be chained. + public static Grid AddEmptyRow(this Grid grid) { if (grid is null) { @@ -65,6 +72,8 @@ namespace Spectre.Console var columns = new IRenderable[grid.ColumnCount]; Enumerable.Range(0, grid.ColumnCount).ForEach(index => columns[index] = Text.Empty); grid.AddRow(columns); + + return grid; } /// @@ -72,7 +81,8 @@ namespace Spectre.Console /// /// The grid to add the row to. /// The columns to add. - public static void AddRow(this Grid grid, params string[] columns) + /// The same instance so that multiple calls can be chained. + public static Grid AddRow(this Grid grid, params string[] columns) { if (grid is null) { @@ -85,6 +95,7 @@ namespace Spectre.Console } grid.AddRow(columns.Select(column => new Markup(column)).ToArray()); + return grid; } } } diff --git a/src/Spectre.Console/Rendering/Table.cs b/src/Spectre.Console/Rendering/Table.cs index 5555d28..55d2f82 100644 --- a/src/Spectre.Console/Rendering/Table.cs +++ b/src/Spectre.Console/Rendering/Table.cs @@ -72,7 +72,8 @@ namespace Spectre.Console /// Adds a column to the table. /// /// The column to add. - public void AddColumn(TableColumn column) + /// The same instance so that multiple calls can be chained. + public Table AddColumn(TableColumn column) { if (column is null) { @@ -85,40 +86,15 @@ namespace Spectre.Console } _columns.Add(column); - } - - /// - /// Adds multiple columns to the table. - /// - /// The columns to add. - public void AddColumns(params TableColumn[] columns) - { - if (columns is null) - { - throw new ArgumentNullException(nameof(columns)); - } - - foreach (var column in columns) - { - AddColumn(column); - } - } - - /// - /// Adds an empty row to the table. - /// - public void AddEmptyRow() - { - var columns = new IRenderable[ColumnCount]; - Enumerable.Range(0, ColumnCount).ForEach(index => columns[index] = Text.Empty); - AddRow(columns); + return this; } /// /// Adds a row to the table. /// /// The row columns to add. - public void AddRow(params IRenderable[] columns) + /// The same instance so that multiple calls can be chained. + public Table AddRow(params IRenderable[] columns) { if (columns is null) { @@ -138,6 +114,8 @@ namespace Spectre.Console var diff = _columns.Count - columns.Length; Enumerable.Range(0, diff).ForEach(_ => _rows.Last().Add(Text.Empty)); } + + return this; } /// diff --git a/src/Spectre.Console/Rendering/TableExtensions.cs b/src/Spectre.Console/Rendering/TableExtensions.cs index d0a819f..38b4fd1 100644 --- a/src/Spectre.Console/Rendering/TableExtensions.cs +++ b/src/Spectre.Console/Rendering/TableExtensions.cs @@ -1,5 +1,7 @@ using System; using System.Linq; +using Spectre.Console.Internal; +using Spectre.Console.Rendering; namespace Spectre.Console { @@ -8,13 +10,58 @@ namespace Spectre.Console /// public static class TableExtensions { + /// + /// Adds multiple columns to the table. + /// + /// The table to add the column to. + /// The columns to add. + /// The same instance so that multiple calls can be chained. + public static Table AddColumns(this Table table, params TableColumn[] columns) + { + if (table is null) + { + throw new ArgumentNullException(nameof(table)); + } + + if (columns is null) + { + throw new ArgumentNullException(nameof(columns)); + } + + foreach (var column in columns) + { + table.AddColumn(column); + } + + return table; + } + + /// + /// Adds an empty row to the table. + /// + /// The table to add the row to. + /// The same instance so that multiple calls can be chained. + public static Table AddEmptyRow(this Table table) + { + if (table is null) + { + throw new ArgumentNullException(nameof(table)); + } + + var columns = new IRenderable[table.ColumnCount]; + Enumerable.Range(0, table.ColumnCount).ForEach(index => columns[index] = Text.Empty); + table.AddRow(columns); + return table; + } + /// /// Adds a column to the table. /// /// The table to add the column to. /// The column to add. - /// The added instance. - public static TableColumn AddColumn(this Table table, string column) + /// Delegate that can be used to configure the added column. + /// The same instance so that multiple calls can be chained. + public static Table AddColumn(this Table table, string column, Action? configure = null) { if (table is null) { @@ -27,9 +74,10 @@ namespace Spectre.Console } var tableColumn = new TableColumn(column); - table.AddColumn(tableColumn); + configure?.Invoke(tableColumn); - return tableColumn; + table.AddColumn(tableColumn); + return table; } /// @@ -37,7 +85,8 @@ namespace Spectre.Console /// /// The table to add the columns to. /// The columns to add. - public static void AddColumns(this Table table, params string[] columns) + /// The same instance so that multiple calls can be chained. + public static Table AddColumns(this Table table, params string[] columns) { if (table is null) { @@ -53,6 +102,8 @@ namespace Spectre.Console { AddColumn(table, column); } + + return table; } /// @@ -60,7 +111,8 @@ namespace Spectre.Console /// /// The table to add the row to. /// The row columns to add. - public static void AddRow(this Table table, params string[] columns) + /// The same instance so that multiple calls can be chained. + public static Table AddRow(this Table table, params string[] columns) { if (table is null) { @@ -73,6 +125,7 @@ namespace Spectre.Console } table.AddRow(columns.Select(column => new Markup(column)).ToArray()); + return table; } ///