2
0

ColumnStyle.cs 3.8 KB

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