FileSystemInfoStats.cs 3.2 KB

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