FileDialogTableSource.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Linq;
  3. namespace Terminal.Gui {
  4. internal class FileDialogTableSource : ITableSource {
  5. readonly FileDialogStyle style;
  6. readonly int currentSortColumn;
  7. readonly bool currentSortIsAsc;
  8. readonly FileDialogState state;
  9. public FileDialogTableSource (FileDialogState state, FileDialogStyle style, int currentSortColumn, bool currentSortIsAsc)
  10. {
  11. this.style = style;
  12. this.currentSortColumn = currentSortColumn;
  13. this.currentSortIsAsc = currentSortIsAsc;
  14. this.state = state;
  15. }
  16. public object this [int row, int col] => GetColumnValue (col, state.Children [row]);
  17. private object GetColumnValue (int col, FileSystemInfoStats stats)
  18. {
  19. switch (col) {
  20. case 0:
  21. var icon = stats.IsParent ? null : style.IconGetter?.Invoke (stats.FileSystemInfo);
  22. return icon + (stats?.Name ?? string.Empty);
  23. case 1:
  24. return stats?.HumanReadableLength ?? string.Empty;
  25. case 2:
  26. if (stats == null || stats.IsParent || stats.LastWriteTime == null) {
  27. return string.Empty;
  28. }
  29. return stats.LastWriteTime.Value.ToString (style.DateFormat);
  30. case 3:
  31. return stats?.Type ?? string.Empty;
  32. default:
  33. throw new ArgumentOutOfRangeException (nameof (col));
  34. }
  35. }
  36. internal static object GetRawColumnValue (int col, FileSystemInfoStats stats)
  37. {
  38. switch (col) {
  39. case 0: return stats.FileSystemInfo.Name;
  40. case 1: return stats.MachineReadableLength;
  41. case 2: return stats.LastWriteTime;
  42. case 3: return stats.Type;
  43. }
  44. throw new ArgumentOutOfRangeException (nameof (col));
  45. }
  46. public int Rows => state.Children.Count ();
  47. public int Columns => 4;
  48. public string [] ColumnNames => new string []{
  49. MaybeAddSortArrows(style.FilenameColumnName,0),
  50. MaybeAddSortArrows(style.SizeColumnName,1),
  51. MaybeAddSortArrows(style.ModifiedColumnName,2),
  52. MaybeAddSortArrows(style.TypeColumnName,3)
  53. };
  54. private string MaybeAddSortArrows (string name, int idx)
  55. {
  56. if (idx == currentSortColumn) {
  57. return name + (currentSortIsAsc ? " (▲)" : " (▼)");
  58. }
  59. return name;
  60. }
  61. }
  62. }