FileDialogTableSource.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. namespace Terminal.Gui;
  2. internal class FileDialogTableSource : ITableSource
  3. {
  4. private readonly int currentSortColumn;
  5. private readonly bool currentSortIsAsc;
  6. private readonly FileDialog dlg;
  7. private readonly FileDialogState state;
  8. private readonly FileDialogStyle style;
  9. public FileDialogTableSource (
  10. FileDialog dlg,
  11. FileDialogState state,
  12. FileDialogStyle style,
  13. int currentSortColumn,
  14. bool currentSortIsAsc
  15. )
  16. {
  17. this.style = style;
  18. this.currentSortColumn = currentSortColumn;
  19. this.currentSortIsAsc = currentSortIsAsc;
  20. this.dlg = dlg;
  21. this.state = state;
  22. }
  23. public object this [int row, int col] => GetColumnValue (col, state.Children [row]);
  24. public int Rows => state.Children.Count ();
  25. public int Columns => 4;
  26. public string [] ColumnNames => new []
  27. {
  28. MaybeAddSortArrows (style.FilenameColumnName, 0),
  29. MaybeAddSortArrows (style.SizeColumnName, 1),
  30. MaybeAddSortArrows (style.ModifiedColumnName, 2),
  31. MaybeAddSortArrows (style.TypeColumnName, 3)
  32. };
  33. internal static object GetRawColumnValue (int col, FileSystemInfoStats stats)
  34. {
  35. switch (col)
  36. {
  37. case 0: return stats.FileSystemInfo.Name;
  38. case 1: return stats.MachineReadableLength;
  39. case 2: return stats.LastWriteTime;
  40. case 3: return stats.Type;
  41. }
  42. throw new ArgumentOutOfRangeException (nameof (col));
  43. }
  44. private object GetColumnValue (int col, FileSystemInfoStats stats)
  45. {
  46. switch (col)
  47. {
  48. case 0:
  49. // do not use icon for ".."
  50. if (stats?.IsParent ?? false)
  51. {
  52. return stats.Name;
  53. }
  54. string icon = dlg.Style.IconProvider.GetIconWithOptionalSpace (stats.FileSystemInfo);
  55. return (icon + (stats?.Name ?? string.Empty)).Trim ();
  56. case 1:
  57. return stats?.HumanReadableLength ?? string.Empty;
  58. case 2:
  59. if (stats is null || stats.IsParent || stats.LastWriteTime is null)
  60. {
  61. return string.Empty;
  62. }
  63. return stats.LastWriteTime.Value.ToString (style.DateFormat);
  64. case 3:
  65. return stats?.Type ?? string.Empty;
  66. default:
  67. throw new ArgumentOutOfRangeException (nameof (col));
  68. }
  69. }
  70. private string MaybeAddSortArrows (string name, int idx)
  71. {
  72. if (idx == currentSortColumn)
  73. {
  74. return name + (currentSortIsAsc ? " (▲)" : " (▼)");
  75. }
  76. return name;
  77. }
  78. }