TableView.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Terminal.Gui.Views {
  8. /// <summary>
  9. /// View for tabular data based on a <see cref="DataTable"/>
  10. /// </summary>
  11. public class TableView : View {
  12. private int columnOffset;
  13. private int rowOffset;
  14. public DataTable Table { get; private set; }
  15. /// <summary>
  16. /// Zero indexed offset for the upper left <see cref="DataColumn"/> to display in <see cref="Table"/>.
  17. /// </summary>
  18. /// <remarks>This property allows very wide tables to be rendered with horizontal scrolling</remarks>
  19. public int ColumnOffset {
  20. get => columnOffset;
  21. //try to prevent this being set to an out of bounds column
  22. set => columnOffset = Math.Min (Table.Columns.Count - 1, Math.Max (0, value));
  23. }
  24. /// <summary>
  25. /// Zero indexed offset for the <see cref="DataRow"/> to display in <see cref="Table"/> on line 2 of the control (first line being headers)
  26. /// </summary>
  27. /// <remarks>This property allows very wide tables to be rendered with horizontal scrolling</remarks>
  28. public int RowOffset {
  29. get => rowOffset;
  30. set => rowOffset = Math.Min (Table.Rows.Count - 1, Math.Max (0, value));
  31. }
  32. /// <summary>
  33. /// The maximum number of characters to render in any given column. This prevents one long column from pushing out all the others
  34. /// </summary>
  35. public int MaximumCellWidth {get;set;} = 100;
  36. /// <summary>
  37. /// The text representation that should be rendered for cells with the value <see cref="DBNull.Value"/>
  38. /// </summary>
  39. public string NullSymbol {get;set;} = "-";
  40. /// <summary>
  41. /// Initialzies a <see cref="TableView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  42. /// </summary>
  43. /// <param name="table">The table to display in the control</param>
  44. public TableView (DataTable table) : base ()
  45. {
  46. this.Table = table ?? throw new ArgumentNullException (nameof (table));
  47. }
  48. ///<inheritdoc/>
  49. public override void Redraw (Rect bounds)
  50. {
  51. Attribute currentAttribute;
  52. var current = ColorScheme.Focus;
  53. Driver.SetAttribute (current);
  54. Move (0, 0);
  55. var frame = Frame;
  56. int activeColor = ColorScheme.HotNormal;
  57. int trackingColor = ColorScheme.HotFocus;
  58. // What columns to render at what X offset in viewport
  59. Dictionary<DataColumn, int> columnsToRender = CalculateViewport(bounds);
  60. Driver.SetAttribute (ColorScheme.HotNormal);
  61. // Render the headers
  62. foreach(var kvp in columnsToRender) {
  63. Move (kvp.Value,0);
  64. Driver.AddStr(kvp.Key.ColumnName);
  65. }
  66. //render the cells
  67. for (int line = 1; line < frame.Height; line++) {
  68. //work out what Row to render
  69. var rowToRender = RowOffset + (line-1);
  70. if(rowToRender >= Table.Rows.Count)
  71. break;
  72. foreach(var kvp in columnsToRender) {
  73. Move (kvp.Value,line);
  74. Driver.AddStr(GetRenderedVal(Table.Rows[rowToRender][kvp.Key]));
  75. }
  76. }
  77. /*
  78. for (int line = 1; line < frame.Height; line++) {
  79. var lineRect = new Rect (0, line, frame.Width, 1);
  80. if (!bounds.Contains (lineRect))
  81. continue;
  82. Move (0, line);
  83. Driver.SetAttribute (ColorScheme.HotNormal);
  84. Driver.AddStr ("test");
  85. currentAttribute = ColorScheme.HotNormal;
  86. SetAttribute (ColorScheme.Normal);
  87. }*/
  88. void SetAttribute (Attribute attribute)
  89. {
  90. if (currentAttribute != attribute) {
  91. currentAttribute = attribute;
  92. Driver.SetAttribute (attribute);
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Calculates which columns should be rendered given the <paramref name="bounds"/> in which to display and the <see cref="ColumnOffset"/>
  98. /// </summary>
  99. /// <param name="bounds"></param>
  100. /// <param name="padding"></param>
  101. /// <returns></returns>
  102. private Dictionary<DataColumn,int> CalculateViewport(Rect bounds, int padding = 1)
  103. {
  104. Dictionary<DataColumn,int> toReturn = new Dictionary<DataColumn, int>();
  105. int usedSpace = 0;
  106. int availableHorizontalSpace = bounds.Width;
  107. int rowsToRender = bounds.Height-1; //1 reserved for the headers row
  108. foreach(var col in Table.Columns.Cast<DataColumn>().Skip(ColumnOffset)) {
  109. toReturn.Add(col,usedSpace);
  110. usedSpace += CalculateMaxRowSize(col,rowsToRender) + padding;
  111. if(usedSpace > availableHorizontalSpace)
  112. return toReturn;
  113. }
  114. return toReturn;
  115. }
  116. /// <summary>
  117. /// Returns the maximum of the <paramref name="col"/> name and the maximum length of data that will be rendered starting at <see cref="RowOffset"/> and rendering <paramref name="rowsToRender"/>
  118. /// </summary>
  119. /// <param name="col"></param>
  120. /// <param name="rowsToRender"></param>
  121. /// <returns></returns>
  122. private int CalculateMaxRowSize (DataColumn col, int rowsToRender)
  123. {
  124. int spaceRequired = col.ColumnName.Length;
  125. for(int i = RowOffset; i<rowsToRender && i<Table.Rows.Count;i++) {
  126. //expand required space if cell is bigger than the last biggest cell or header
  127. spaceRequired = Math.Max(spaceRequired,GetRenderedVal(Table.Rows[i][col]).Length);
  128. }
  129. return spaceRequired;
  130. }
  131. /// <summary>
  132. /// Returns the value that should be rendered to best represent a strongly typed <paramref name="value"/> read from <see cref="Table"/>
  133. /// </summary>
  134. /// <param name="value"></param>
  135. /// <returns></returns>
  136. private string GetRenderedVal (object value)
  137. {
  138. if(value == null || value == DBNull.Value)
  139. {
  140. return NullSymbol;
  141. }
  142. var representation = value.ToString();
  143. //if it is too long to fit
  144. if(representation.Length > MaximumCellWidth)
  145. return representation.Substring(0,MaximumCellWidth);
  146. return representation;
  147. }
  148. }
  149. }