ColumnStyle.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 
  2. namespace Terminal.Gui.Views;
  3. /// <summary>
  4. /// Describes how to render a given column in a <see cref="TableView"/> including <see cref="Alignment"/> and
  5. /// textual representation of cells (e.g. date formats)
  6. /// <a href="../docs/tableview.md">See TableView Deep Dive for more information</a>.
  7. /// </summary>
  8. public class ColumnStyle
  9. {
  10. /// <summary>
  11. /// Defines a delegate for returning custom alignment per cell based on cell values. When specified this will
  12. /// override <see cref="Alignment"/>
  13. /// </summary>
  14. public Func<object, Alignment> AlignmentGetter;
  15. /// <summary>
  16. /// Defines a delegate for returning a custom scheme per cell based on cell values. Return null for the
  17. /// default
  18. /// </summary>
  19. public CellColorGetterDelegate ColorGetter;
  20. /// <summary>
  21. /// Defines a delegate for returning custom representations of cell values. If not set then
  22. /// <see cref="object.ToString()"/> is used. Return values from your delegate may be truncated e.g. based on
  23. /// <see cref="MaxWidth"/>
  24. /// </summary>
  25. public Func<object, string> RepresentationGetter;
  26. private bool _visible = true;
  27. /// <summary>
  28. /// Defines the default alignment for all values rendered in this column. For custom alignment based on cell
  29. /// contents use <see cref="AlignmentGetter"/>.
  30. /// </summary>
  31. public Alignment Alignment { get; set; }
  32. /// <summary>Defines the format for values e.g. "yyyy-MM-dd" for dates</summary>
  33. public string Format { get; set; }
  34. /// <summary>
  35. /// Set the maximum width of the column in characters. This value will be ignored if more than the tables
  36. /// <see cref="TableView.MaxCellWidth"/>. Defaults to <see cref="TableView.DefaultMaxCellWidth"/>
  37. /// </summary>
  38. public int MaxWidth { get; set; } = TableView.DefaultMaxCellWidth;
  39. /// <summary>Enables flexible sizing of this column based on available screen space to render into.</summary>
  40. public int MinAcceptableWidth { get; set; } = TableView.DefaultMinAcceptableWidth;
  41. /// <summary>
  42. /// Set the minimum width of the column in characters. Setting this will ensure that even when a column has short
  43. /// content/header it still fills a given width of the control.
  44. /// <para>
  45. /// This value will be ignored if more than the tables <see cref="TableView.MaxCellWidth"/> or the
  46. /// <see cref="MaxWidth"/>
  47. /// </para>
  48. /// <remarks>For setting a flexible column width (down to a lower limit) use <see cref="MinAcceptableWidth"/> instead</remarks>
  49. /// </summary>
  50. public int MinWidth { get; set; }
  51. /// <summary>
  52. /// Gets or Sets a value indicating whether the column should be visible to the user. This affects both whether it
  53. /// is rendered and whether it can be selected. Defaults to true.
  54. /// </summary>
  55. /// <remarks>If <see cref="MaxWidth"/> is 0 then <see cref="Visible"/> will always return false.</remarks>
  56. public bool Visible
  57. {
  58. get => MaxWidth >= 0 && _visible;
  59. set => _visible = value;
  60. }
  61. /// <summary>
  62. /// Returns the alignment for the cell based on <paramref name="cellValue"/> and <see cref="AlignmentGetter"/>/
  63. /// <see cref="Alignment"/>
  64. /// </summary>
  65. /// <param name="cellValue"></param>
  66. /// <returns></returns>
  67. public Alignment GetAlignment (object cellValue)
  68. {
  69. if (AlignmentGetter is { })
  70. {
  71. return AlignmentGetter (cellValue);
  72. }
  73. return Alignment;
  74. }
  75. /// <summary>
  76. /// Returns the full string to render (which may be truncated if too long) that the current style says best
  77. /// represents the given <paramref name="value"/>
  78. /// </summary>
  79. /// <param name="value"></param>
  80. /// <returns></returns>
  81. public string GetRepresentation (object value)
  82. {
  83. if (!string.IsNullOrWhiteSpace (Format))
  84. {
  85. if (value is IFormattable f)
  86. {
  87. return f.ToString (Format, null);
  88. }
  89. }
  90. if (RepresentationGetter is { })
  91. {
  92. return RepresentationGetter (value);
  93. }
  94. return value?.ToString ();
  95. }
  96. }