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 ByteConversion = 1024;
  21. private static readonly List<string> ExecutableExtensions = new () { ".EXE", ".BAT" };
  22. private static readonly List<string> ImageExtensions = new ()
  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. /// <summary>Gets or Sets a value indicating whether this instance represents the parent of the current state (i.e. "..").</summary>
  57. public bool IsParent { get; internal set; }
  58. public DateTime? LastWriteTime { get; }
  59. public long MachineReadableLength { get; }
  60. public string Name => IsParent ? ".." : FileSystemInfo.Name;
  61. public string Type { get; }
  62. public bool IsExecutable ()
  63. {
  64. // TODO: handle linux executable status
  65. return FileSystemInfo is IFileSystemInfo f
  66. && ExecutableExtensions.Contains (
  67. f.Extension,
  68. StringComparer.InvariantCultureIgnoreCase
  69. );
  70. }
  71. public bool IsImage ()
  72. {
  73. return FileSystemInfo is IFileSystemInfo f
  74. && ImageExtensions.Contains (
  75. f.Extension,
  76. StringComparer.InvariantCultureIgnoreCase
  77. );
  78. }
  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, ByteConversion);
  90. double adjustedSize = value / Math.Pow (1000, mag);
  91. return string.Format (culture.NumberFormat, "{0:n2} {1}", adjustedSize, SizeSuffixes [mag]);
  92. }
  93. }