FileDialogTableSource.cs 2.7 KB

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