Add row and column accessors for tables and grids

This commit is contained in:
Patrik Svensson
2020-10-26 11:51:35 +01:00
committed by Patrik Svensson
parent 3e5e22d6c2
commit e7f497050c
30 changed files with 511 additions and 162 deletions

View File

@ -63,10 +63,10 @@ namespace Spectre.Console.Rendering
{
var padding = columns[columnIndex].Padding;
if (padding.Left > 0)
if (padding != null && padding.Value.Left > 0)
{
// Left padding
builder.Append(" ".Repeat(padding.Left));
builder.Append(" ".Repeat(padding.Value.Left));
}
var justification = columns[columnIndex].Alignment;
@ -96,9 +96,9 @@ namespace Spectre.Console.Rendering
}
// Right padding
if (padding.Right > 0)
if (padding != null && padding.Value.Right > 0)
{
builder.Append(" ".Repeat(padding.Right));
builder.Append(" ".Repeat(padding.Value.Right));
}
if (!lastColumn)

View File

@ -0,0 +1,13 @@
namespace Spectre.Console.Rendering
{
/// <summary>
/// Represents something that can be dirty.
/// </summary>
public interface IHasDirtyState
{
/// <summary>
/// Gets a value indicating whether the object is dirty.
/// </summary>
bool IsDirty { get; }
}
}

View File

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Rendering
{
/// <summary>
/// Represents something renderable that's reconstructed
/// when it's state change in any way.
/// </summary>
public abstract class JustInTimeRenderable : Renderable
{
private bool _dirty;
private IRenderable? _rendered;
/// <inheritdoc/>
protected sealed override Measurement Measure(RenderContext context, int maxWidth)
{
return GetInner().Measure(context, maxWidth);
}
/// <inheritdoc/>
protected sealed override IEnumerable<Segment> Render(RenderContext context, int width)
{
return GetInner().Render(context, width);
}
/// <summary>
/// Builds the inner renderable.
/// </summary>
/// <returns>A new inner renderable.</returns>
protected abstract IRenderable Build();
/// <summary>
/// Checks if there are any children that has changed.
/// If so, the underlying renderable needs rebuilding.
/// </summary>
/// <returns><c>true</c> if the object needs rebuilding, otherwise <c>false</c>.</returns>
protected virtual bool HasDirtyChildren()
{
return false;
}
/// <summary>
/// Marks this instance as dirty.
/// </summary>
protected void MarkAsDirty()
{
_dirty = true;
}
/// <summary>
/// Marks this instance as dirty.
/// </summary>
/// <param name="action">
/// The action to execute before marking the instance as dirty.
/// </param>
protected void MarkAsDirty(Action action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
action();
_dirty = true;
}
private IRenderable GetInner()
{
if (_dirty || HasDirtyChildren() || _rendered == null)
{
_rendered = Build();
_dirty = false;
}
return _rendered;
}
}
}