using System;
using System.Linq;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
///
/// Contains extension methods for .
///
public static class GridExtensions
{
///
/// Adds a column to the grid.
///
/// The grid to add the column to.
/// The number of columns to add.
/// The same instance so that multiple calls can be chained.
public static Grid AddColumns(this Grid grid, int count)
{
if (grid is null)
{
throw new ArgumentNullException(nameof(grid));
}
for (var index = 0; index < count; index++)
{
grid.AddColumn(new GridColumn());
}
return grid;
}
///
/// Adds a column to the grid.
///
/// The grid to add the column to.
/// The columns to add.
/// The same instance so that multiple calls can be chained.
public static Grid AddColumns(this Grid grid, params GridColumn[] columns)
{
if (grid is null)
{
throw new ArgumentNullException(nameof(grid));
}
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
foreach (var column in columns)
{
grid.AddColumn(column);
}
return grid;
}
///
/// Adds an empty row to the grid.
///
/// The grid to add the row to.
/// The same instance so that multiple calls can be chained.
public static Grid AddEmptyRow(this Grid grid)
{
if (grid is null)
{
throw new ArgumentNullException(nameof(grid));
}
var columns = new IRenderable[grid.Columns.Count];
Enumerable.Range(0, grid.Columns.Count).ForEach(index => columns[index] = Text.Empty);
grid.AddRow(columns);
return grid;
}
///
/// Adds a new row to the grid.
///
/// The grid to add the row to.
/// The columns to add.
/// The same instance so that multiple calls can be chained.
public static Grid AddRow(this Grid grid, params string[] columns)
{
if (grid is null)
{
throw new ArgumentNullException(nameof(grid));
}
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
grid.AddRow(columns.Select(column => new Markup(column)).ToArray());
return grid;
}
///
/// Sets the grid width.
///
/// The grid.
/// The width.
/// The same instance so that multiple calls can be chained.
public static Grid Width(this Grid grid, int? width)
{
if (grid is null)
{
throw new ArgumentNullException(nameof(grid));
}
grid.Width = width;
return grid;
}
}
}