FileSystemInfoStats.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #nullable enable
  2. using System.Globalization;
  3. using System.IO.Abstractions;
  4. namespace Terminal.Gui.FileServices;
  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 BYTE_CONVERSION = 1024;
  22. private static readonly List<string> _executableExtensions = [".EXE", ".BAT"];
  23. private static readonly List<string> _imageExtensions =
  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. public bool IsExecutable ()
  58. {
  59. // TODO: handle linux executable status
  60. return FileSystemInfo is { }
  61. && _executableExtensions.Contains (
  62. FileSystemInfo.Extension,
  63. StringComparer.InvariantCultureIgnoreCase
  64. );
  65. }
  66. public bool IsImage ()
  67. {
  68. return FileSystemInfo is { }
  69. && _imageExtensions.Contains (
  70. FileSystemInfo.Extension,
  71. StringComparer.InvariantCultureIgnoreCase
  72. );
  73. }
  74. /// <summary>Gets or Sets a value indicating whether this instance represents the parent of the current state (i.e. "..").</summary>
  75. public bool IsParent { get; internal set; }
  76. public DateTime? LastWriteTime { get; }
  77. public long MachineReadableLength { get; }
  78. public string Name => IsParent ? ".." : FileSystemInfo?.Name ?? string.Empty;
  79. public string Type { get; }
  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, BYTE_CONVERSION);
  91. double adjustedSize = value / Math.Pow (1000, mag);
  92. return string.Format (culture.NumberFormat, "{0:n2} {1}", adjustedSize, _sizeSuffixes [mag]);
  93. }
  94. }