using System;
using System.Collections.Generic;
using Spectre.Console.Composition;
namespace Spectre.Console
{
///
/// Represents a grid.
///
public sealed class Grid : IRenderable
{
private readonly Table _table;
///
/// Initializes a new instance of the class.
///
public Grid()
{
_table = new Table
{
Border = BorderKind.None,
ShowHeaders = false,
IsGrid = true,
};
}
///
public Measurement Measure(RenderContext context, int maxWidth)
{
return ((IRenderable)_table).Measure(context, maxWidth);
}
///
public IEnumerable Render(RenderContext context, int width)
{
return ((IRenderable)_table).Render(context, width);
}
///
/// Adds a column to the grid.
///
public void AddColumn()
{
AddColumn(new GridColumn());
}
///
/// Adds a column to the grid.
///
/// The column to add.
public void AddColumn(GridColumn column)
{
if (column is null)
{
throw new ArgumentNullException(nameof(column));
}
_table.AddColumn(new TableColumn(string.Empty)
{
Width = column.Width,
NoWrap = column.NoWrap,
Padding = column.Padding,
Alignment = column.Alignment,
});
}
///
/// Adds a column to the grid.
///
/// The number of columns to add.
public void AddColumns(int count)
{
for (var index = 0; index < count; index++)
{
AddColumn(new GridColumn());
}
}
///
/// Adds a column to the grid.
///
/// The columns to add.
public void AddColumns(params GridColumn[] columns)
{
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
foreach (var column in columns)
{
AddColumn(column);
}
}
///
/// Adds a new row to the grid.
///
/// The columns to add.
public void AddRow(params string[] columns)
{
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
if (columns.Length < _table.ColumnCount)
{
throw new InvalidOperationException("The number of row columns are less than the number of grid columns.");
}
if (columns.Length > _table.ColumnCount)
{
throw new InvalidOperationException("The number of row columns are greater than the number of grid columns.");
}
_table.AddRow(columns);
}
}
}