ColumnStyle.cs 4.2 KB

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