mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add row and column accessors for tables and grids
This commit is contained in:

committed by
Patrik Svensson

parent
3e5e22d6c2
commit
e7f497050c
@ -96,5 +96,37 @@ namespace Spectre.Console
|
||||
calendar.HeaderStyle = style ?? Style.Plain;
|
||||
return calendar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the calendar header.
|
||||
/// </summary>
|
||||
/// <param name="calendar">The calendar.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Calendar ShowHeader(this Calendar calendar)
|
||||
{
|
||||
if (calendar is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(calendar));
|
||||
}
|
||||
|
||||
calendar.ShowHeader = true;
|
||||
return calendar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides the calendar header.
|
||||
/// </summary>
|
||||
/// <param name="calendar">The calendar.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Calendar HideHeader(this Calendar calendar)
|
||||
{
|
||||
if (calendar is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(calendar));
|
||||
}
|
||||
|
||||
calendar.ShowHeader = false;
|
||||
return calendar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,5 +24,24 @@ namespace Spectre.Console
|
||||
obj.NoWrap = true;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the width of the column.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">An object implementing <see cref="IColumn"/>.</typeparam>
|
||||
/// <param name="obj">The column.</param>
|
||||
/// <param name="width">The column width.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static T Width<T>(this T obj, int? width)
|
||||
where T : class, IColumn
|
||||
{
|
||||
if (obj is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
obj.Width = width;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -69,8 +69,8 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(grid));
|
||||
}
|
||||
|
||||
var columns = new IRenderable[grid.ColumnCount];
|
||||
Enumerable.Range(0, grid.ColumnCount).ForEach(index => columns[index] = Text.Empty);
|
||||
var columns = new IRenderable[grid.Columns.Count];
|
||||
Enumerable.Range(0, grid.Columns.Count).ForEach(index => columns[index] = Text.Empty);
|
||||
grid.AddRow(columns);
|
||||
|
||||
return grid;
|
||||
|
@ -22,7 +22,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
return Padding(obj, new Padding(left, obj.Padding.Top, obj.Padding.Right, obj.Padding.Bottom));
|
||||
return Padding(obj, new Padding(left, obj.Padding.GetTopSafe(), obj.Padding.GetRightSafe(), obj.Padding.GetBottomSafe()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -40,7 +40,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
return Padding(obj, new Padding(obj.Padding.Left, top, obj.Padding.Right, obj.Padding.Bottom));
|
||||
return Padding(obj, new Padding(obj.Padding.GetLeftSafe(), top, obj.Padding.GetRightSafe(), obj.Padding.GetBottomSafe()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -58,7 +58,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
return Padding(obj, new Padding(obj.Padding.Left, obj.Padding.Top, right, obj.Padding.Bottom));
|
||||
return Padding(obj, new Padding(obj.Padding.GetLeftSafe(), obj.Padding.GetTopSafe(), right, obj.Padding.GetBottomSafe()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -76,7 +76,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
return Padding(obj, new Padding(obj.Padding.Left, obj.Padding.Top, obj.Padding.Right, bottom));
|
||||
return Padding(obj, new Padding(obj.Padding.GetLeftSafe(), obj.Padding.GetTopSafe(), obj.Padding.GetRightSafe(), bottom));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
48
src/Spectre.Console/Extensions/PaddingExtensions.cs
Normal file
48
src/Spectre.Console/Extensions/PaddingExtensions.cs
Normal file
@ -0,0 +1,48 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="Padding"/>.
|
||||
/// </summary>
|
||||
public static class PaddingExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the left padding.
|
||||
/// </summary>
|
||||
/// <param name="padding">The padding.</param>
|
||||
/// <returns>The left padding or zero if <c>padding</c> is null.</returns>
|
||||
public static int GetLeftSafe(this Padding? padding)
|
||||
{
|
||||
return padding?.Left ?? 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the right padding.
|
||||
/// </summary>
|
||||
/// <param name="padding">The padding.</param>
|
||||
/// <returns>The right padding or zero if <c>padding</c> is null.</returns>
|
||||
public static int GetRightSafe(this Padding? padding)
|
||||
{
|
||||
return padding?.Right ?? 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the top padding.
|
||||
/// </summary>
|
||||
/// <param name="padding">The padding.</param>
|
||||
/// <returns>The top padding or zero if <c>padding</c> is null.</returns>
|
||||
public static int GetTopSafe(this Padding? padding)
|
||||
{
|
||||
return padding?.Top ?? 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bottom padding.
|
||||
/// </summary>
|
||||
/// <param name="padding">The padding.</param>
|
||||
/// <returns>The bottom padding or zero if <c>padding</c> is null.</returns>
|
||||
public static int GetBottomSafe(this Padding? padding)
|
||||
{
|
||||
return padding?.Bottom ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -36,6 +36,22 @@ namespace Spectre.Console
|
||||
return table;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a row to the table.
|
||||
/// </summary>
|
||||
/// <param name="table">The table to add the row to.</param>
|
||||
/// <param name="columns">The row columns to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Table AddRow(this Table table, params IRenderable[] columns)
|
||||
{
|
||||
if (table is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(table));
|
||||
}
|
||||
|
||||
return table.AddRow(columns);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an empty row to the table.
|
||||
/// </summary>
|
||||
@ -48,8 +64,8 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(table));
|
||||
}
|
||||
|
||||
var columns = new IRenderable[table.ColumnCount];
|
||||
Enumerable.Range(0, table.ColumnCount).ForEach(index => columns[index] = Text.Empty);
|
||||
var columns = new IRenderable[table.Columns.Count];
|
||||
Enumerable.Range(0, table.Columns.Count).ForEach(index => columns[index] = Text.Empty);
|
||||
table.AddRow(columns);
|
||||
return table;
|
||||
}
|
||||
|
Reference in New Issue
Block a user