FileSystemInfoStats.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Globalization;
  2. using System.IO.Abstractions;
  3. using Terminal.Gui.Resources;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Wrapper for <see cref="FileSystemInfo"/> that contains additional information (e.g. <see cref="IsParent"/>)
  7. /// and helper methods.
  8. /// </summary>
  9. internal class FileSystemInfoStats
  10. {
  11. /* ---- Colors used by the ls command line tool ----
  12. *
  13. * Blue: Directory
  14. * Green: Executable or recognized data file
  15. * Cyan (Sky Blue): Symbolic link file
  16. * Yellow with black background: Device
  17. * Magenta (Pink): Graphic image file
  18. * Red: Archive file
  19. * Red with black background: Broken link
  20. */
  21. private const long ByteConversion = 1024;
  22. private static readonly List<string> ExecutableExtensions = new () { ".EXE", ".BAT" };
  23. private static readonly List<string> ImageExtensions = new ()
  24. {
  25. ".JPG",
  26. ".JPEG",
  27. ".JPE",
  28. ".BMP",
  29. ".GIF",
  30. ".PNG"
  31. };
  32. private static readonly string [] SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  33. /// <summary>Initializes a new instance of the <see cref="FileSystemInfoStats"/> class.</summary>
  34. /// <param name="fsi">The directory of path to wrap.</param>
  35. /// <param name="culture"></param>
  36. public FileSystemInfoStats (IFileSystemInfo fsi, CultureInfo culture)
  37. {
  38. FileSystemInfo = fsi;
  39. LastWriteTime = fsi.LastWriteTime;
  40. if (fsi is IFileInfo fi)
  41. {
  42. MachineReadableLength = fi.Length;
  43. HumanReadableLength = GetHumanReadableFileSize (MachineReadableLength, culture);
  44. Type = fi.Extension;
  45. }
  46. else
  47. {
  48. HumanReadableLength = string.Empty;
  49. Type = $"<{Strings.fdDirectory}>";
  50. IsDir = true;
  51. }
  52. }
  53. /// <summary>Gets the wrapped <see cref="FileSystemInfo"/> (directory or file).</summary>
  54. public IFileSystemInfo FileSystemInfo { get; }
  55. public string HumanReadableLength { get; }
  56. public bool IsDir { get; }
  57. /// <summary>Gets or Sets a value indicating whether this instance represents the parent of the current state (i.e. "..").</summary>
  58. public bool IsParent { get; internal set; }
  59. public DateTime? LastWriteTime { get; }
  60. public long MachineReadableLength { get; }
  61. public string Name => IsParent ? ".." : FileSystemInfo.Name;
  62. public string Type { get; }
  63. public bool IsExecutable ()
  64. {
  65. // TODO: handle linux executable status
  66. return FileSystemInfo is IFileSystemInfo f
  67. && ExecutableExtensions.Contains (
  68. f.Extension,
  69. StringComparer.InvariantCultureIgnoreCase
  70. );
  71. }
  72. public bool IsImage ()
  73. {
  74. return FileSystemInfo is IFileSystemInfo f
  75. && ImageExtensions.Contains (
  76. f.Extension,
  77. StringComparer.InvariantCultureIgnoreCase
  78. );
  79. }
  80. private static string GetHumanReadableFileSize (long value, CultureInfo culture)
  81. {
  82. if (value < 0)
  83. {
  84. return "-" + GetHumanReadableFileSize (-value, culture);
  85. }
  86. if (value == 0)
  87. {
  88. return "0.0 B";
  89. }
  90. var mag = (int)Math.Log (value, ByteConversion);
  91. double adjustedSize = value / Math.Pow (1000, mag);
  92. return string.Format (culture.NumberFormat, "{0:n2} {1}", adjustedSize, SizeSuffixes [mag]);
  93. }
  94. }