Make table and grid extension methods fluent

This commit is contained in:
Patrik Svensson 2020-09-05 08:49:36 +02:00
parent 13c56eca01
commit ceb3572d6a
4 changed files with 91 additions and 42 deletions

View File

@ -57,16 +57,19 @@ namespace Spectre.Console
/// <summary> /// <summary>
/// Adds a column to the grid. /// Adds a column to the grid.
/// </summary> /// </summary>
public void AddColumn() /// <returns>The same instance so that multiple calls can be chained.</returns>
public Grid AddColumn()
{ {
AddColumn(new GridColumn()); AddColumn(new GridColumn());
return this;
} }
/// <summary> /// <summary>
/// Adds a column to the grid. /// Adds a column to the grid.
/// </summary> /// </summary>
/// <param name="column">The column to add.</param> /// <param name="column">The column to add.</param>
public void AddColumn(GridColumn column) /// <returns>The same instance so that multiple calls can be chained.</returns>
public Grid AddColumn(GridColumn column)
{ {
if (column is null) if (column is null)
{ {
@ -88,13 +91,16 @@ namespace Spectre.Console
Padding = column.Padding, Padding = column.Padding,
Alignment = column.Alignment, Alignment = column.Alignment,
}); });
return this;
} }
/// <summary> /// <summary>
/// Adds a new row to the grid. /// Adds a new row to the grid.
/// </summary> /// </summary>
/// <param name="columns">The columns to add.</param> /// <param name="columns">The columns to add.</param>
public void AddRow(params IRenderable[] columns) /// <returns>The same instance so that multiple calls can be chained.</returns>
public Grid AddRow(params IRenderable[] columns)
{ {
if (columns is null) if (columns is null)
{ {
@ -107,6 +113,7 @@ namespace Spectre.Console
} }
_table.AddRow(columns); _table.AddRow(columns);
return this;
} }
} }
} }

View File

@ -15,7 +15,8 @@ namespace Spectre.Console
/// </summary> /// </summary>
/// <param name="grid">The grid to add the column to.</param> /// <param name="grid">The grid to add the column to.</param>
/// <param name="count">The number of columns to add.</param> /// <param name="count">The number of columns to add.</param>
public static void AddColumns(this Grid grid, int count) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Grid AddColumns(this Grid grid, int count)
{ {
if (grid is null) if (grid is null)
{ {
@ -26,6 +27,8 @@ namespace Spectre.Console
{ {
grid.AddColumn(new GridColumn()); grid.AddColumn(new GridColumn());
} }
return grid;
} }
/// <summary> /// <summary>
@ -33,7 +36,8 @@ namespace Spectre.Console
/// </summary> /// </summary>
/// <param name="grid">The grid to add the column to.</param> /// <param name="grid">The grid to add the column to.</param>
/// <param name="columns">The columns to add.</param> /// <param name="columns">The columns to add.</param>
public static void AddColumns(this Grid grid, params GridColumn[] columns) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Grid AddColumns(this Grid grid, params GridColumn[] columns)
{ {
if (grid is null) if (grid is null)
{ {
@ -49,13 +53,16 @@ namespace Spectre.Console
{ {
grid.AddColumn(column); grid.AddColumn(column);
} }
return grid;
} }
/// <summary> /// <summary>
/// Adds an empty row to the grid. /// Adds an empty row to the grid.
/// </summary> /// </summary>
/// <param name="grid">The grid to add the row to.</param> /// <param name="grid">The grid to add the row to.</param>
public static void AddEmptyRow(this Grid grid) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Grid AddEmptyRow(this Grid grid)
{ {
if (grid is null) if (grid is null)
{ {
@ -65,6 +72,8 @@ namespace Spectre.Console
var columns = new IRenderable[grid.ColumnCount]; var columns = new IRenderable[grid.ColumnCount];
Enumerable.Range(0, grid.ColumnCount).ForEach(index => columns[index] = Text.Empty); Enumerable.Range(0, grid.ColumnCount).ForEach(index => columns[index] = Text.Empty);
grid.AddRow(columns); grid.AddRow(columns);
return grid;
} }
/// <summary> /// <summary>
@ -72,7 +81,8 @@ namespace Spectre.Console
/// </summary> /// </summary>
/// <param name="grid">The grid to add the row to.</param> /// <param name="grid">The grid to add the row to.</param>
/// <param name="columns">The columns to add.</param> /// <param name="columns">The columns to add.</param>
public static void AddRow(this Grid grid, params string[] columns) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Grid AddRow(this Grid grid, params string[] columns)
{ {
if (grid is null) if (grid is null)
{ {
@ -85,6 +95,7 @@ namespace Spectre.Console
} }
grid.AddRow(columns.Select(column => new Markup(column)).ToArray()); grid.AddRow(columns.Select(column => new Markup(column)).ToArray());
return grid;
} }
} }
} }

View File

@ -72,7 +72,8 @@ namespace Spectre.Console
/// Adds a column to the table. /// Adds a column to the table.
/// </summary> /// </summary>
/// <param name="column">The column to add.</param> /// <param name="column">The column to add.</param>
public void AddColumn(TableColumn column) /// <returns>The same instance so that multiple calls can be chained.</returns>
public Table AddColumn(TableColumn column)
{ {
if (column is null) if (column is null)
{ {
@ -85,40 +86,15 @@ namespace Spectre.Console
} }
_columns.Add(column); _columns.Add(column);
} return this;
/// <summary>
/// Adds multiple columns to the table.
/// </summary>
/// <param name="columns">The columns to add.</param>
public void AddColumns(params TableColumn[] columns)
{
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
foreach (var column in columns)
{
AddColumn(column);
}
}
/// <summary>
/// Adds an empty row to the table.
/// </summary>
public void AddEmptyRow()
{
var columns = new IRenderable[ColumnCount];
Enumerable.Range(0, ColumnCount).ForEach(index => columns[index] = Text.Empty);
AddRow(columns);
} }
/// <summary> /// <summary>
/// Adds a row to the table. /// Adds a row to the table.
/// </summary> /// </summary>
/// <param name="columns">The row columns to add.</param> /// <param name="columns">The row columns to add.</param>
public void AddRow(params IRenderable[] columns) /// <returns>The same instance so that multiple calls can be chained.</returns>
public Table AddRow(params IRenderable[] columns)
{ {
if (columns is null) if (columns is null)
{ {
@ -138,6 +114,8 @@ namespace Spectre.Console
var diff = _columns.Count - columns.Length; var diff = _columns.Count - columns.Length;
Enumerable.Range(0, diff).ForEach(_ => _rows.Last().Add(Text.Empty)); Enumerable.Range(0, diff).ForEach(_ => _rows.Last().Add(Text.Empty));
} }
return this;
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console namespace Spectre.Console
{ {
@ -8,13 +10,58 @@ namespace Spectre.Console
/// </summary> /// </summary>
public static class TableExtensions public static class TableExtensions
{ {
/// <summary>
/// Adds multiple columns to the table.
/// </summary>
/// <param name="table">The table to add the column to.</param>
/// <param name="columns">The columns to add.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
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;
}
/// <summary>
/// Adds an empty row to the table.
/// </summary>
/// <param name="table">The table to add the row to.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
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;
}
/// <summary> /// <summary>
/// Adds a column to the table. /// Adds a column to the table.
/// </summary> /// </summary>
/// <param name="table">The table to add the column to.</param> /// <param name="table">The table to add the column to.</param>
/// <param name="column">The column to add.</param> /// <param name="column">The column to add.</param>
/// <returns>The added <see cref="TableColumn"/> instance.</returns> /// <param name="configure">Delegate that can be used to configure the added column.</param>
public static TableColumn AddColumn(this Table table, string column) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Table AddColumn(this Table table, string column, Action<TableColumn>? configure = null)
{ {
if (table is null) if (table is null)
{ {
@ -27,9 +74,10 @@ namespace Spectre.Console
} }
var tableColumn = new TableColumn(column); var tableColumn = new TableColumn(column);
table.AddColumn(tableColumn); configure?.Invoke(tableColumn);
return tableColumn; table.AddColumn(tableColumn);
return table;
} }
/// <summary> /// <summary>
@ -37,7 +85,8 @@ namespace Spectre.Console
/// </summary> /// </summary>
/// <param name="table">The table to add the columns to.</param> /// <param name="table">The table to add the columns to.</param>
/// <param name="columns">The columns to add.</param> /// <param name="columns">The columns to add.</param>
public static void AddColumns(this Table table, params string[] columns) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Table AddColumns(this Table table, params string[] columns)
{ {
if (table is null) if (table is null)
{ {
@ -53,6 +102,8 @@ namespace Spectre.Console
{ {
AddColumn(table, column); AddColumn(table, column);
} }
return table;
} }
/// <summary> /// <summary>
@ -60,7 +111,8 @@ namespace Spectre.Console
/// </summary> /// </summary>
/// <param name="table">The table to add the row to.</param> /// <param name="table">The table to add the row to.</param>
/// <param name="columns">The row columns to add.</param> /// <param name="columns">The row columns to add.</param>
public static void AddRow(this Table table, params string[] columns) /// <returns>The same instance so that multiple calls can be chained.</returns>
public static Table AddRow(this Table table, params string[] columns)
{ {
if (table is null) if (table is null)
{ {
@ -73,6 +125,7 @@ namespace Spectre.Console
} }
table.AddRow(columns.Select(column => new Markup(column)).ToArray()); table.AddRow(columns.Select(column => new Markup(column)).ToArray());
return table;
} }
/// <summary> /// <summary>