FileDialogTableSource.cs 2.4 KB

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