FileSystemInfoStats.cs 3.6 KB

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