FileDialogTableSource.cs 2.4 KB

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