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 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 IRenderable[] columns) { if (table is null) { throw new ArgumentNullException(nameof(table)); } return table.AddRow(columns); } /// /// 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.Columns.Count]; Enumerable.Range(0, table.Columns.Count).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 Width(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; } /// /// Shows table footers. /// /// The table. /// The same instance so that multiple calls can be chained. public static Table ShowFooters(this Table table) { if (table is null) { throw new ArgumentNullException(nameof(table)); } table.ShowFooters = true; return table; } /// /// Hides table footers. /// /// The table. /// The same instance so that multiple calls can be chained. public static Table HideFooters(this Table table) { if (table is null) { throw new ArgumentNullException(nameof(table)); } table.ShowFooters = false; return table; } /// /// Sets the table title. /// /// The table. /// The table title markup text. /// The table title style. /// The same instance so that multiple calls can be chained. public static Table Title(this Table table, string text, Style? style = null) { if (table is null) { throw new ArgumentNullException(nameof(table)); } if (text is null) { throw new ArgumentNullException(nameof(text)); } return Title(table, new TableTitle(text, style)); } /// /// Sets the table title. /// /// The table. /// The table title. /// The same instance so that multiple calls can be chained. public static Table Title(this Table table, TableTitle title) { if (table is null) { throw new ArgumentNullException(nameof(table)); } table.Title = title; return table; } /// /// Sets the table caption. /// /// The table. /// The caption markup text. /// The style. /// The same instance so that multiple calls can be chained. public static Table Caption(this Table table, string text, Style? style = null) { if (table is null) { throw new ArgumentNullException(nameof(table)); } if (text is null) { throw new ArgumentNullException(nameof(text)); } return Caption(table, new TableTitle(text, style)); } /// /// Sets the table caption. /// /// The table. /// The caption. /// The same instance so that multiple calls can be chained. public static Table Caption(this Table table, TableTitle caption) { if (table is null) { throw new ArgumentNullException(nameof(table)); } table.Caption = caption; return table; } } }