ColumnStyle.cs 4.2 KB

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