mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00
Make table and grid extension methods fluent
This commit is contained in:
parent
13c56eca01
commit
ceb3572d6a
@ -57,16 +57,19 @@ namespace Spectre.Console
|
||||
/// <summary>
|
||||
/// Adds a column to the grid.
|
||||
/// </summary>
|
||||
public void AddColumn()
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public Grid AddColumn()
|
||||
{
|
||||
AddColumn(new GridColumn());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a column to the grid.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
@ -88,13 +91,16 @@ namespace Spectre.Console
|
||||
Padding = column.Padding,
|
||||
Alignment = column.Alignment,
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new row to the grid.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
@ -107,6 +113,7 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
_table.AddRow(columns);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,8 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
/// <param name="grid">The grid to add the column to.</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)
|
||||
{
|
||||
@ -26,6 +27,8 @@ namespace Spectre.Console
|
||||
{
|
||||
grid.AddColumn(new GridColumn());
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -33,7 +36,8 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
/// <param name="grid">The grid to add the column to.</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)
|
||||
{
|
||||
@ -49,13 +53,16 @@ namespace Spectre.Console
|
||||
{
|
||||
grid.AddColumn(column);
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an empty row to the grid.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -72,7 +81,8 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
/// <param name="grid">The grid to add the row to.</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)
|
||||
{
|
||||
@ -85,6 +95,7 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
grid.AddRow(columns.Select(column => new Markup(column)).ToArray());
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,8 @@ namespace Spectre.Console
|
||||
/// Adds a column to the table.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
@ -85,40 +86,15 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
_columns.Add(column);
|
||||
}
|
||||
|
||||
/// <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);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a row to the table.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
@ -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
|
||||
/// </summary>
|
||||
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>
|
||||
/// Adds a column to the table.
|
||||
/// </summary>
|
||||
/// <param name="table">The table to add the column to.</param>
|
||||
/// <param name="column">The column to add.</param>
|
||||
/// <returns>The added <see cref="TableColumn"/> instance.</returns>
|
||||
public static TableColumn AddColumn(this Table table, string column)
|
||||
/// <param name="configure">Delegate that can be used to configure the added column.</param>
|
||||
/// <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)
|
||||
{
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -37,7 +85,8 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
/// <param name="table">The table to add the columns to.</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)
|
||||
{
|
||||
@ -53,6 +102,8 @@ namespace Spectre.Console
|
||||
{
|
||||
AddColumn(table, column);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -60,7 +111,8 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
/// <param name="table">The table to add the row to.</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)
|
||||
{
|
||||
@ -73,6 +125,7 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
table.AddRow(columns.Select(column => new Markup(column)).ToArray());
|
||||
return table;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user