ListTableSource.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #nullable disable
  2. using System.Collections;
  3. using System.Data;
  4. namespace Terminal.Gui.Views;
  5. /// <summary>
  6. /// <see cref="ITableSource"/> implementation that wraps a <see cref="System.Collections.IList"/>. This class is
  7. /// mutable: changes are permitted to the wrapped <see cref="IList"/>.
  8. /// </summary>
  9. public class ListTableSource : ITableSource
  10. {
  11. /// <summary>The list this source wraps.</summary>
  12. public IList List;
  13. /// <summary>The style this source uses.</summary>
  14. public ListColumnStyle Style;
  15. private readonly TableView _tableView;
  16. private Rectangle _lastBounds;
  17. private IList _lastList;
  18. private int _lastMaxCellWidth;
  19. private int _lastMinCellWidth;
  20. private ListColumnStyle _lastStyle;
  21. /// <summary>
  22. /// Creates a new columned list table instance based on the data in <paramref name="list"/> and dimensions from
  23. /// <paramref name="tableView"/>.
  24. /// </summary>
  25. /// <param name="list"></param>
  26. /// <param name="tableView"></param>
  27. /// <param name="style"></param>
  28. public ListTableSource (IList list, TableView tableView, ListColumnStyle style)
  29. {
  30. List = list;
  31. _tableView = tableView;
  32. Style = style;
  33. DataTable = CreateTable (CalculateColumns ());
  34. // TODO: Determine the best event for this
  35. tableView.DrawingContent += TableView_DrawContent;
  36. }
  37. /// <inheritdoc/>
  38. public ListTableSource (IList list, TableView tableView) : this (list, tableView, new ListColumnStyle ()) { }
  39. /// <summary>The number of items in the IList source</summary>
  40. public int Count => List.Count;
  41. /// <summary>The data table this source wraps.</summary>
  42. public DataTable DataTable { get; private set; }
  43. /// <inheritdoc/>
  44. public object this [int row, int col]
  45. {
  46. get
  47. {
  48. int idx;
  49. if (Style.Orientation == Orientation.Vertical)
  50. {
  51. idx = col * Rows + row;
  52. }
  53. else
  54. {
  55. idx = row * Columns + col;
  56. }
  57. if (idx < 0 || idx >= Count)
  58. {
  59. return null;
  60. }
  61. return List [idx];
  62. }
  63. }
  64. /// <inheritdoc/>
  65. public int Rows => DataTable.Rows.Count;
  66. /// <inheritdoc/>
  67. public int Columns => DataTable.Columns.Count;
  68. /// <inheritdoc/>
  69. public string [] ColumnNames => Enumerable.Range (0, Columns).Select (n => n.ToString ()).ToArray ();
  70. private int CalculateColumns ()
  71. {
  72. int cols;
  73. int colWidth = CalculateMaxLength ();
  74. if (colWidth > _tableView.MaxCellWidth)
  75. {
  76. colWidth = _tableView.MaxCellWidth;
  77. }
  78. if (_tableView.MinCellWidth > 0 && colWidth < _tableView.MinCellWidth)
  79. {
  80. if (_tableView.MinCellWidth > _tableView.MaxCellWidth)
  81. {
  82. colWidth = _tableView.MaxCellWidth;
  83. }
  84. else
  85. {
  86. colWidth = _tableView.MinCellWidth;
  87. }
  88. }
  89. if (Style.Orientation == Orientation.Vertical != Style.ScrollParallel)
  90. {
  91. float f = (float)_tableView.Viewport.Height - _tableView.GetHeaderHeight ();
  92. cols = (int)Math.Ceiling (Count / f);
  93. }
  94. else
  95. {
  96. cols = (int)Math.Ceiling (((float)_tableView.Viewport.Width - 1) / colWidth) - 2;
  97. }
  98. return cols > 1 ? cols : 1;
  99. }
  100. /// <summary>Returns the size in characters of the longest value read from <see cref="ListTableSource.List"/></summary>
  101. /// <returns></returns>
  102. private int CalculateMaxLength ()
  103. {
  104. if (List is null || Count == 0)
  105. {
  106. return 0;
  107. }
  108. var maxLength = 0;
  109. foreach (object t in List)
  110. {
  111. int l;
  112. if (t is string s)
  113. {
  114. l = s.GetColumns ();
  115. }
  116. else
  117. {
  118. l = t.ToString ().Length;
  119. }
  120. if (l > maxLength)
  121. {
  122. maxLength = l;
  123. }
  124. }
  125. return maxLength;
  126. }
  127. /// <summary>Creates a DataTable from an IList to display in a <see cref="TableView"/></summary>
  128. private DataTable CreateTable (int cols = 1)
  129. {
  130. var table = new DataTable ();
  131. for (var col = 0; col < cols; col++)
  132. {
  133. table.Columns.Add (new DataColumn (col.ToString ()));
  134. }
  135. for (var row = 0; row < Count / table.Columns.Count; row++)
  136. {
  137. table.Rows.Add ();
  138. }
  139. // return partial row
  140. if (Count % table.Columns.Count != 0)
  141. {
  142. table.Rows.Add ();
  143. }
  144. return table;
  145. }
  146. private void TableView_DrawContent (object sender, DrawEventArgs e)
  147. {
  148. if (!_tableView.Viewport.Equals (_lastBounds)
  149. || _tableView.MaxCellWidth != _lastMaxCellWidth
  150. || _tableView.MinCellWidth != _lastMinCellWidth
  151. || Style != _lastStyle
  152. || List != _lastList)
  153. {
  154. DataTable = CreateTable (CalculateColumns ());
  155. }
  156. _lastBounds = _tableView.Viewport;
  157. _lastMinCellWidth = _tableView.MaxCellWidth;
  158. _lastMaxCellWidth = _tableView.MaxCellWidth;
  159. _lastStyle = Style;
  160. _lastList = List;
  161. }
  162. }