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
@ -11,7 +11,7 @@ namespace Spectre.Console
|
||||
/// <summary>
|
||||
/// A renderable calendar.
|
||||
/// </summary>
|
||||
public sealed class Calendar : Renderable, IHasCulture, IHasTableBorder, IAlignable
|
||||
public sealed class Calendar : JustInTimeRenderable, IHasCulture, IHasTableBorder, IAlignable
|
||||
{
|
||||
private const int NumberOfWeekDays = 7;
|
||||
private const int ExpectedRowCount = 6;
|
||||
@ -21,11 +21,9 @@ namespace Spectre.Console
|
||||
private int _year;
|
||||
private int _month;
|
||||
private int _day;
|
||||
private IRenderable? _table;
|
||||
private TableBorder _border;
|
||||
private bool _useSafeBorder;
|
||||
private Style? _borderStyle;
|
||||
private bool _dirty;
|
||||
private CultureInfo _culture;
|
||||
private Style _highlightStyle;
|
||||
private bool _showHeader;
|
||||
@ -158,43 +156,17 @@ namespace Spectre.Console
|
||||
_year = year;
|
||||
_month = month;
|
||||
_day = day;
|
||||
_table = null;
|
||||
_border = TableBorder.Square;
|
||||
_useSafeBorder = true;
|
||||
_borderStyle = null;
|
||||
_dirty = true;
|
||||
_culture = CultureInfo.InvariantCulture;
|
||||
_highlightStyle = new Style(foreground: Color.Blue);
|
||||
_showHeader = true;
|
||||
_calendarEvents = new ListWithCallback<CalendarEvent>(() => _dirty = true);
|
||||
_calendarEvents = new ListWithCallback<CalendarEvent>(() => MarkAsDirty());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override Measurement Measure(RenderContext context, int maxWidth)
|
||||
{
|
||||
var table = GetTable();
|
||||
return table.Measure(context, maxWidth);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
return GetTable().Render(context, maxWidth);
|
||||
}
|
||||
|
||||
private IRenderable GetTable()
|
||||
{
|
||||
// Table needs to be built?
|
||||
if (_dirty || _table == null)
|
||||
{
|
||||
_table = BuildTable();
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
return _table;
|
||||
}
|
||||
|
||||
private IRenderable BuildTable()
|
||||
protected override IRenderable Build()
|
||||
{
|
||||
var culture = Culture ?? CultureInfo.InvariantCulture;
|
||||
|
||||
@ -264,9 +236,9 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
// We want all calendars to have the same height.
|
||||
if (table.RowCount < ExpectedRowCount)
|
||||
if (table.Rows.Count < ExpectedRowCount)
|
||||
{
|
||||
var diff = Math.Max(0, ExpectedRowCount - table.RowCount);
|
||||
var diff = Math.Max(0, ExpectedRowCount - table.Rows.Count);
|
||||
for (var i = 0; i < diff; i++)
|
||||
{
|
||||
table.AddEmptyRow();
|
||||
@ -276,12 +248,6 @@ namespace Spectre.Console
|
||||
return table;
|
||||
}
|
||||
|
||||
private void MarkAsDirty(Action action)
|
||||
{
|
||||
action();
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
private DayOfWeek[] GetWeekdays()
|
||||
{
|
||||
var culture = Culture ?? CultureInfo.InvariantCulture;
|
||||
|
@ -13,7 +13,7 @@ namespace Spectre.Console
|
||||
private readonly List<IRenderable> _items;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Padding Padding { get; set; } = new Padding(0, 0, 1, 0);
|
||||
public Padding? Padding { get; set; } = new Padding(0, 0, 1, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not the columns should
|
||||
@ -62,7 +62,7 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
protected override Measurement Measure(RenderContext context, int maxWidth)
|
||||
{
|
||||
var maxPadding = Math.Max(Padding.Left, Padding.Right);
|
||||
var maxPadding = Math.Max(Padding.GetLeftSafe(), Padding.GetRightSafe());
|
||||
|
||||
var itemWidths = _items.Select(item => item.Measure(context, maxWidth).Max).ToArray();
|
||||
var columnCount = CalculateColumnCount(maxWidth, itemWidths, _items.Count, maxPadding);
|
||||
@ -90,7 +90,7 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
var maxPadding = Math.Max(Padding.Left, Padding.Right);
|
||||
var maxPadding = Math.Max(Padding.GetLeftSafe(), Padding.GetRightSafe());
|
||||
|
||||
var itemWidths = _items.Select(item => item.Measure(context, maxWidth).Max).ToArray();
|
||||
var columnCount = CalculateColumnCount(maxWidth, itemWidths, _items.Count, maxPadding);
|
||||
|
@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal.Collections;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
@ -7,32 +9,37 @@ namespace Spectre.Console
|
||||
/// <summary>
|
||||
/// A renderable grid.
|
||||
/// </summary>
|
||||
public sealed class Grid : Renderable, IExpandable, IAlignable
|
||||
public sealed class Grid : JustInTimeRenderable, IExpandable, IAlignable
|
||||
{
|
||||
private readonly Table _table;
|
||||
private readonly ListWithCallback<GridColumn> _columns;
|
||||
private readonly ListWithCallback<GridRow> _rows;
|
||||
|
||||
private bool _expand;
|
||||
private Justify? _alignment;
|
||||
private bool _padRightCell;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of columns in the table.
|
||||
/// Gets the grid columns.
|
||||
/// </summary>
|
||||
public int ColumnCount => _table.ColumnCount;
|
||||
public IReadOnlyList<GridColumn> Columns => _columns;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of rows in the table.
|
||||
/// Gets the grid rows.
|
||||
/// </summary>
|
||||
public int RowCount => _table.RowCount;
|
||||
public IReadOnlyList<GridRow> Rows => _rows;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Expand
|
||||
{
|
||||
get => _table.Expand;
|
||||
set => _table.Expand = value;
|
||||
get => _expand;
|
||||
set => MarkAsDirty(() => _expand = value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Justify? Alignment
|
||||
{
|
||||
get => _table.Alignment;
|
||||
set => _table.Alignment = value;
|
||||
get => _alignment;
|
||||
set => MarkAsDirty(() => _alignment = value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -40,25 +47,10 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
public Grid()
|
||||
{
|
||||
_table = new Table
|
||||
{
|
||||
Border = TableBorder.None,
|
||||
ShowHeaders = false,
|
||||
IsGrid = true,
|
||||
PadRightCell = false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override Measurement Measure(RenderContext context, int maxWidth)
|
||||
{
|
||||
return ((IRenderable)_table).Measure(context, maxWidth);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int width)
|
||||
{
|
||||
return ((IRenderable)_table).Render(context, width);
|
||||
_expand = false;
|
||||
_alignment = null;
|
||||
_columns = new ListWithCallback<GridColumn>(() => MarkAsDirty());
|
||||
_rows = new ListWithCallback<GridRow>(() => MarkAsDirty());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -83,21 +75,15 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (_table.RowCount > 0)
|
||||
if (_rows.Count > 0)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot add new columns to grid with existing rows.");
|
||||
}
|
||||
|
||||
// Only pad the most right cell if we've explicitly set a padding.
|
||||
_table.PadRightCell = column.HasExplicitPadding;
|
||||
_padRightCell = column.HasExplicitPadding;
|
||||
|
||||
_table.AddColumn(new TableColumn(string.Empty)
|
||||
{
|
||||
Width = column.Width,
|
||||
NoWrap = column.NoWrap,
|
||||
Padding = column.Padding,
|
||||
Alignment = column.Alignment,
|
||||
});
|
||||
_columns.Add(column);
|
||||
|
||||
return this;
|
||||
}
|
||||
@ -114,13 +100,49 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(columns));
|
||||
}
|
||||
|
||||
if (columns.Length > _table.ColumnCount)
|
||||
if (columns.Length > _columns.Count)
|
||||
{
|
||||
throw new InvalidOperationException("The number of row columns are greater than the number of grid columns.");
|
||||
}
|
||||
|
||||
_table.AddRow(columns);
|
||||
_rows.Add(new GridRow(columns));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool HasDirtyChildren()
|
||||
{
|
||||
return _columns.Any(c => ((IHasDirtyState)c).IsDirty);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IRenderable Build()
|
||||
{
|
||||
var table = new Table
|
||||
{
|
||||
Border = TableBorder.None,
|
||||
ShowHeaders = false,
|
||||
IsGrid = true,
|
||||
PadRightCell = _padRightCell,
|
||||
};
|
||||
|
||||
foreach (var column in _columns)
|
||||
{
|
||||
table.AddColumn(new TableColumn(string.Empty)
|
||||
{
|
||||
Width = column.Width,
|
||||
NoWrap = column.NoWrap,
|
||||
Padding = column.Padding ?? new Padding(0, 0, 2, 0),
|
||||
Alignment = column.Alignment,
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var row in _rows)
|
||||
{
|
||||
table.AddRow(row);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,71 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a grid column.
|
||||
/// </summary>
|
||||
public sealed class GridColumn : IColumn
|
||||
public sealed class GridColumn : IColumn, IHasDirtyState
|
||||
{
|
||||
private Padding _padding;
|
||||
private bool _isDirty;
|
||||
private int? _width;
|
||||
private bool _noWrap;
|
||||
private Padding? _padding;
|
||||
private Justify? _alignment;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IHasDirtyState.IsDirty => _isDirty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the width of the column.
|
||||
/// If <c>null</c>, the column will adapt to it's contents.
|
||||
/// </summary>
|
||||
public int? Width { get; set; }
|
||||
public int? Width
|
||||
{
|
||||
get => _width;
|
||||
set => MarkAsDirty(() => _width = value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether wrapping of
|
||||
/// text within the column should be prevented.
|
||||
/// </summary>
|
||||
public bool NoWrap { get; set; }
|
||||
public bool NoWrap
|
||||
{
|
||||
get => _noWrap;
|
||||
set => MarkAsDirty(() => _noWrap = value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the padding of the column.
|
||||
/// Vertical padding (top and bottom) is ignored.
|
||||
/// </summary>
|
||||
public Padding Padding
|
||||
public Padding? Padding
|
||||
{
|
||||
get => _padding;
|
||||
set
|
||||
{
|
||||
HasExplicitPadding = true;
|
||||
_padding = value;
|
||||
}
|
||||
set => MarkAsDirty(() => _padding = value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the alignment of the column.
|
||||
/// </summary>
|
||||
public Justify? Alignment { get; set; }
|
||||
public Justify? Alignment
|
||||
{
|
||||
get => _alignment;
|
||||
set => MarkAsDirty(() => _alignment = value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the user
|
||||
/// has set an explicit padding for this column.
|
||||
/// </summary>
|
||||
internal bool HasExplicitPadding { get; private set; }
|
||||
internal bool HasExplicitPadding => Padding != null;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GridColumn"/> class.
|
||||
/// </summary>
|
||||
public GridColumn()
|
||||
private void MarkAsDirty(Action action)
|
||||
{
|
||||
_padding = new Padding(0, 0, 2, 0);
|
||||
action();
|
||||
_isDirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
56
src/Spectre.Console/Widgets/GridRow.cs
Normal file
56
src/Spectre.Console/Widgets/GridRow.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a grid row.
|
||||
/// </summary>
|
||||
public sealed class GridRow : IEnumerable<IRenderable>
|
||||
{
|
||||
private readonly List<IRenderable> _items;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a row item at the specified grid column index.
|
||||
/// </summary>
|
||||
/// <param name="index">The grid column index.</param>
|
||||
/// <returns>The row item at the specified grid column index.</returns>
|
||||
public IRenderable this[int index]
|
||||
{
|
||||
get => _items[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GridRow"/> class.
|
||||
/// </summary>
|
||||
/// <param name="items">The row items.</param>
|
||||
public GridRow(IEnumerable<IRenderable> items)
|
||||
{
|
||||
_items = new List<IRenderable>(items ?? Array.Empty<IRenderable>());
|
||||
}
|
||||
|
||||
internal void Add(IRenderable item)
|
||||
{
|
||||
if (item is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
}
|
||||
|
||||
_items.Add(item);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerator<IRenderable> GetEnumerator()
|
||||
{
|
||||
return _items.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ namespace Spectre.Console
|
||||
private readonly IRenderable _child;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Padding Padding { get; set; } = new Padding(1, 1, 1, 1);
|
||||
public Padding? Padding { get; set; } = new Padding(1, 1, 1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not the padding should
|
||||
@ -35,7 +35,7 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
protected override Measurement Measure(RenderContext context, int maxWidth)
|
||||
{
|
||||
var paddingWidth = Padding.GetWidth();
|
||||
var paddingWidth = Padding?.GetWidth() ?? 0;
|
||||
var measurement = _child.Measure(context, maxWidth - paddingWidth);
|
||||
|
||||
return new Measurement(
|
||||
@ -46,7 +46,7 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
var paddingWidth = Padding.GetWidth();
|
||||
var paddingWidth = Padding?.GetWidth() ?? 0;
|
||||
var childWidth = maxWidth - paddingWidth;
|
||||
|
||||
if (!Expand)
|
||||
@ -59,7 +59,7 @@ namespace Spectre.Console
|
||||
var result = new List<Segment>();
|
||||
|
||||
// Top padding
|
||||
for (var i = 0; i < Padding.Top; i++)
|
||||
for (var i = 0; i < Padding.GetTopSafe(); i++)
|
||||
{
|
||||
result.Add(new Segment(new string(' ', width)));
|
||||
result.Add(Segment.LineBreak);
|
||||
@ -69,22 +69,22 @@ namespace Spectre.Console
|
||||
foreach (var (_, _, _, line) in Segment.SplitLines(context, child).Enumerate())
|
||||
{
|
||||
// Left padding
|
||||
if (Padding.Left != 0)
|
||||
if (Padding.GetLeftSafe() != 0)
|
||||
{
|
||||
result.Add(new Segment(new string(' ', Padding.Left)));
|
||||
result.Add(new Segment(new string(' ', Padding.GetLeftSafe())));
|
||||
}
|
||||
|
||||
result.AddRange(line);
|
||||
|
||||
// Right padding
|
||||
if (Padding.Right != 0)
|
||||
if (Padding.GetRightSafe() != 0)
|
||||
{
|
||||
result.Add(new Segment(new string(' ', Padding.Right)));
|
||||
result.Add(new Segment(new string(' ', Padding.GetRightSafe())));
|
||||
}
|
||||
|
||||
// Missing space on right side?
|
||||
var lineWidth = line.CellCount(context);
|
||||
var diff = width - lineWidth - Padding.Left - Padding.Right;
|
||||
var diff = width - lineWidth - Padding.GetLeftSafe() - Padding.GetRightSafe();
|
||||
if (diff > 0)
|
||||
{
|
||||
result.Add(new Segment(new string(' ', diff)));
|
||||
@ -94,7 +94,7 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
// Bottom padding
|
||||
for (var i = 0; i < Padding.Bottom; i++)
|
||||
for (var i = 0; i < Padding.GetBottomSafe(); i++)
|
||||
{
|
||||
result.Add(new Segment(new string(' ', width)));
|
||||
result.Add(Segment.LineBreak);
|
||||
|
@ -34,7 +34,7 @@ namespace Spectre.Console
|
||||
/// <summary>
|
||||
/// Gets or sets the padding.
|
||||
/// </summary>
|
||||
public Padding Padding { get; set; } = new Padding(1, 0, 1, 0);
|
||||
public Padding? Padding { get; set; } = new Padding(1, 0, 1, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the header.
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal;
|
||||
using Spectre.Console.Rendering;
|
||||
using Spectre.Console.Widgets;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
@ -14,20 +15,20 @@ namespace Spectre.Console
|
||||
private const int EdgeCount = 2;
|
||||
|
||||
private readonly List<TableColumn> _columns;
|
||||
private readonly List<List<IRenderable>> _rows;
|
||||
private readonly List<TableRow> _rows;
|
||||
|
||||
private static Style _defaultHeadingStyle = new Style(Color.Silver);
|
||||
private static Style _defaultCaptionStyle = new Style(Color.Grey);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of columns in the table.
|
||||
/// Gets the table columns.
|
||||
/// </summary>
|
||||
public int ColumnCount => _columns.Count;
|
||||
public IReadOnlyList<TableColumn> Columns => _columns;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of rows in the table.
|
||||
/// Gets the table rows.
|
||||
/// </summary>
|
||||
public int RowCount => _rows.Count;
|
||||
public IReadOnlyList<TableRow> Rows => _rows;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TableBorder Border { get; set; } = TableBorder.Square;
|
||||
@ -87,7 +88,7 @@ namespace Spectre.Console
|
||||
public Table()
|
||||
{
|
||||
_columns = new List<TableColumn>();
|
||||
_rows = new List<List<IRenderable>>();
|
||||
_rows = new List<TableRow>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -116,24 +117,25 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
/// <param name="columns">The row columns to add.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public Table AddRow(params IRenderable[] columns)
|
||||
public Table AddRow(IEnumerable<IRenderable> columns)
|
||||
{
|
||||
if (columns is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(columns));
|
||||
}
|
||||
|
||||
if (columns.Length > _columns.Count)
|
||||
var rowColumnCount = columns.GetCount();
|
||||
if (rowColumnCount > _columns.Count)
|
||||
{
|
||||
throw new InvalidOperationException("The number of row columns are greater than the number of table columns.");
|
||||
}
|
||||
|
||||
_rows.Add(columns.ToList());
|
||||
_rows.Add(new TableRow(columns));
|
||||
|
||||
// Need to add missing columns?
|
||||
if (columns.Length < _columns.Count)
|
||||
if (rowColumnCount < _columns.Count)
|
||||
{
|
||||
var diff = _columns.Count - columns.Length;
|
||||
var diff = _columns.Count - rowColumnCount;
|
||||
Enumerable.Range(0, diff).ForEach(_ => _rows.Last().Add(Text.Empty));
|
||||
}
|
||||
|
||||
@ -198,11 +200,11 @@ namespace Spectre.Console
|
||||
return new List<Segment>(new[] { new Segment("…", BorderStyle ?? Style.Plain) });
|
||||
}
|
||||
|
||||
var rows = new List<List<IRenderable>>();
|
||||
var rows = new List<TableRow>();
|
||||
if (ShowHeaders)
|
||||
{
|
||||
// Add columns to top of rows
|
||||
rows.Add(new List<IRenderable>(_columns.Select(c => c.Header)));
|
||||
rows.Add(new TableRow(new List<IRenderable>(_columns.Select(c => c.Header))));
|
||||
}
|
||||
|
||||
// Add rows.
|
||||
@ -210,7 +212,7 @@ namespace Spectre.Console
|
||||
|
||||
if (hasFooters)
|
||||
{
|
||||
rows.Add(new List<IRenderable>(_columns.Select(c => c.Footer ?? Text.Empty)));
|
||||
rows.Add(new TableRow(new List<IRenderable>(_columns.Select(c => c.Footer ?? Text.Empty))));
|
||||
}
|
||||
|
||||
var result = new List<Segment>();
|
||||
@ -273,7 +275,7 @@ namespace Spectre.Console
|
||||
// Pad column on left side.
|
||||
if (showBorder || IsGrid)
|
||||
{
|
||||
var leftPadding = _columns[cellIndex].Padding.Left;
|
||||
var leftPadding = _columns[cellIndex].Padding.GetLeftSafe();
|
||||
if (leftPadding > 0)
|
||||
{
|
||||
rowResult.Add(new Segment(new string(' ', leftPadding)));
|
||||
@ -293,7 +295,7 @@ namespace Spectre.Console
|
||||
// Pad column on the right side
|
||||
if (showBorder || (hideBorder && !lastCell) || (hideBorder && lastCell && IsGrid && PadRightCell))
|
||||
{
|
||||
var rightPadding = _columns[cellIndex].Padding.Right;
|
||||
var rightPadding = _columns[cellIndex].Padding.GetRightSafe();
|
||||
if (rightPadding > 0)
|
||||
{
|
||||
rowResult.Add(new Segment(new string(' ', rightPadding)));
|
||||
@ -446,7 +448,7 @@ namespace Spectre.Console
|
||||
|
||||
private (int Min, int Max) MeasureColumn(TableColumn column, RenderContext options, int maxWidth)
|
||||
{
|
||||
var padding = column.Padding.GetWidth();
|
||||
var padding = column.Padding?.GetWidth() ?? 0;
|
||||
|
||||
// Predetermined width?
|
||||
if (column.Width != null)
|
||||
@ -482,11 +484,11 @@ namespace Spectre.Console
|
||||
var hideBorder = !Border.Visible;
|
||||
var separators = hideBorder ? 0 : _columns.Count - 1;
|
||||
var edges = hideBorder ? 0 : EdgeCount;
|
||||
var padding = includePadding ? _columns.Select(x => x.Padding.GetWidth()).Sum() : 0;
|
||||
var padding = includePadding ? _columns.Select(x => x.Padding?.GetWidth() ?? 0).Sum() : 0;
|
||||
|
||||
if (!PadRightCell)
|
||||
{
|
||||
padding -= _columns.Last().Padding.Right;
|
||||
padding -= _columns.Last().Padding.GetRightSafe();
|
||||
}
|
||||
|
||||
return separators + edges + padding;
|
||||
|
@ -28,7 +28,7 @@ namespace Spectre.Console
|
||||
/// Gets or sets the padding of the column.
|
||||
/// Vertical padding (top and bottom) is ignored.
|
||||
/// </summary>
|
||||
public Padding Padding { get; set; }
|
||||
public Padding? Padding { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether wrapping of
|
||||
|
56
src/Spectre.Console/Widgets/TableRow.cs
Normal file
56
src/Spectre.Console/Widgets/TableRow.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Widgets
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a table row.
|
||||
/// </summary>
|
||||
public sealed class TableRow : IEnumerable<IRenderable>
|
||||
{
|
||||
private readonly List<IRenderable> _items;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a row item at the specified table column index.
|
||||
/// </summary>
|
||||
/// <param name="index">The table column index.</param>
|
||||
/// <returns>The row item at the specified table column index.</returns>
|
||||
public IRenderable this[int index]
|
||||
{
|
||||
get => _items[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TableRow"/> class.
|
||||
/// </summary>
|
||||
/// <param name="items">The row items.</param>
|
||||
public TableRow(IEnumerable<IRenderable> items)
|
||||
{
|
||||
_items = new List<IRenderable>(items ?? Array.Empty<IRenderable>());
|
||||
}
|
||||
|
||||
internal void Add(IRenderable item)
|
||||
{
|
||||
if (item is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
}
|
||||
|
||||
_items.Add(item);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerator<IRenderable> GetEnumerator()
|
||||
{
|
||||
return _items.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user