FileDialogTableSource.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // do not use icon for ".."
  24. if(stats?.IsParent ?? false) {
  25. return stats.Name;
  26. }
  27. var icon = dlg.Style.IconProvider.GetIconWithOptionalSpace(stats.FileSystemInfo);
  28. return (icon + (stats?.Name ?? string.Empty)).Trim();
  29. case 1:
  30. return stats?.HumanReadableLength ?? string.Empty;
  31. case 2:
  32. if (stats == null || stats.IsParent || stats.LastWriteTime == null) {
  33. return string.Empty;
  34. }
  35. return stats.LastWriteTime.Value.ToString (style.DateFormat);
  36. case 3:
  37. return stats?.Type ?? string.Empty;
  38. default:
  39. throw new ArgumentOutOfRangeException (nameof (col));
  40. }
  41. }
  42. internal static object GetRawColumnValue (int col, FileSystemInfoStats stats)
  43. {
  44. switch (col) {
  45. case 0: return stats.FileSystemInfo.Name;
  46. case 1: return stats.MachineReadableLength;
  47. case 2: return stats.LastWriteTime;
  48. case 3: return stats.Type;
  49. }
  50. throw new ArgumentOutOfRangeException (nameof (col));
  51. }
  52. public int Rows => state.Children.Count ();
  53. public int Columns => 4;
  54. public string [] ColumnNames => new string []{
  55. MaybeAddSortArrows(style.FilenameColumnName,0),
  56. MaybeAddSortArrows(style.SizeColumnName,1),
  57. MaybeAddSortArrows(style.ModifiedColumnName,2),
  58. MaybeAddSortArrows(style.TypeColumnName,3)
  59. };
  60. private string MaybeAddSortArrows (string name, int idx)
  61. {
  62. if (idx == currentSortColumn) {
  63. return name + (currentSortIsAsc ? " (▲)" : " (▼)");
  64. }
  65. return name;
  66. }
  67. }
  68. }