using System;
using System.Linq;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
///
/// Contains extension methods for .
///
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.
/// 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)
{
throw new ArgumentNullException(nameof(table));
}
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
var tableColumn = new TableColumn(column);
configure?.Invoke(tableColumn);
table.AddColumn(tableColumn);
return table;
}
///
/// Adds multiple columns to the table.
///
/// The table to add the columns to.
/// The columns to add.
/// The same instance so that multiple calls can be chained.
public static Table AddColumns(this Table table, params string[] columns)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
foreach (var column in columns)
{
AddColumn(table, column);
}
return table;
}
///
/// Adds a row to the table.
///
/// The table to add the row to.
/// The row columns to add.
/// The same instance so that multiple calls can be chained.
public static Table AddRow(this Table table, params string[] columns)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
table.AddRow(columns.Select(column => new Markup(column)).ToArray());
return table;
}
///
/// Sets the table width.
///
/// The table.
/// The width.
/// The same instance so that multiple calls can be chained.
public static Table SetWidth(this Table table, int width)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
table.Width = width;
return table;
}
///
/// Shows table headers.
///
/// The table.
/// The same instance so that multiple calls can be chained.
public static Table ShowHeaders(this Table table)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
table.ShowHeaders = true;
return table;
}
///
/// Hides table headers.
///
/// The table.
/// The same instance so that multiple calls can be chained.
public static Table HideHeaders(this Table table)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
table.ShowHeaders = false;
return table;
}
}
}