FileDialogTableSource.cs 2.2 KB

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