namespace Terminal.Gui;
/// implementation that wraps arbitrary data.
///
public class EnumerableTableSource : IEnumerableTableSource
{
private readonly T [] data;
private readonly Dictionary> lamdas;
/// Creates a new instance of the class that presents collection as a table.
///
/// The elements of the collection are recorded during construction (immutable) but the
/// properties of those objects are permitted to change.
///
///
/// The data that you want to present. The members of this collection will be frozen after
/// construction.
///
///
/// Getter methods for each property you want to present in the table. For example:
///
/// new () {
/// { "Colname1", (t)=>t.SomeField},
/// { "Colname2", (t)=>t.SomeOtherField}
/// }
///
///
public EnumerableTableSource (IEnumerable data, Dictionary> columnDefinitions)
{
this.data = data.ToArray ();
ColumnNames = columnDefinitions.Keys.ToArray ();
lamdas = columnDefinitions;
}
/// Gets the object collection hosted by this wrapper.
public IReadOnlyCollection Data => data.AsReadOnly ();
///
public object this [int row, int col] => lamdas [ColumnNames [col]] (data [row]);
///
public int Rows => data.Length;
///
public int Columns => ColumnNames.Length;
///
public string [] ColumnNames { get; }
///
public IEnumerable GetAllObjects () { return Data; }
///
public T GetObjectOnRow (int row) { return Data.ElementAt (row); }
}