FileSystemIconProvider.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.IO.Abstractions;
  2. namespace Terminal.Gui.FileServices;
  3. /// <summary>Determines which symbol to use to represent files and directories.</summary>
  4. public class FileSystemIconProvider
  5. {
  6. private readonly NerdFonts _nerd = new ();
  7. private bool _useNerdIcons = NerdFonts.Enable;
  8. private bool _useUnicodeCharacters;
  9. /// <summary>
  10. /// Returns the character to use to represent <paramref name="fileSystemInfo"/> or an empty space if no icon
  11. /// should be used.
  12. /// </summary>
  13. /// <param name="fileSystemInfo">The file or directory requiring an icon.</param>
  14. /// <returns></returns>
  15. public Rune GetIcon (IFileSystemInfo? fileSystemInfo)
  16. {
  17. if (UseNerdIcons)
  18. {
  19. return new (
  20. _nerd.GetNerdIcon (
  21. fileSystemInfo,
  22. fileSystemInfo is IDirectoryInfo dir && IsOpenGetter (dir)
  23. )
  24. );
  25. }
  26. if (fileSystemInfo is IDirectoryInfo)
  27. {
  28. return UseUnicodeCharacters ? Glyphs.Folder : new (Path.DirectorySeparatorChar);
  29. }
  30. return UseUnicodeCharacters ? Glyphs.File : new (' ');
  31. }
  32. /// <summary>
  33. /// Returns <see cref="GetIcon(IFileSystemInfo)"/> with an extra space on the end if icon is likely to overlap
  34. /// adjacent cells.
  35. /// </summary>
  36. public string GetIconWithOptionalSpace (IFileSystemInfo? fileSystemInfo)
  37. {
  38. string space = UseNerdIcons ? " " : "";
  39. return GetIcon (fileSystemInfo!) + space;
  40. }
  41. /// <summary>
  42. /// Gets or sets the delegate to be used to determine opened state of directories when resolving
  43. /// <see cref="GetIcon(IFileSystemInfo)"/>. Defaults to always false.
  44. /// </summary>
  45. public Func<IDirectoryInfo, bool> IsOpenGetter { get; set; } = d => false;
  46. /// <summary>
  47. /// <para>
  48. /// Gets or sets a flag indicating whether to use Nerd Font icons. Defaults to <see cref="NerdFonts.Enable"/>
  49. /// which can be configured by end users from their <c>./.tui/config.json</c> via
  50. /// <see cref="ConfigurationManager"/>.
  51. /// </para>
  52. /// <remarks>Enabling <see cref="UseNerdIcons"/> implicitly disables <see cref="UseUnicodeCharacters"/>.</remarks>
  53. /// </summary>
  54. public bool UseNerdIcons
  55. {
  56. get => _useNerdIcons;
  57. set
  58. {
  59. _useNerdIcons = value;
  60. if (value)
  61. {
  62. UseUnicodeCharacters = false;
  63. }
  64. }
  65. }
  66. /// <summary>Gets or sets a flag indicating whether to use common unicode characters for file/directory icons.</summary>
  67. public bool UseUnicodeCharacters
  68. {
  69. get => _useUnicodeCharacters;
  70. set
  71. {
  72. _useUnicodeCharacters = value;
  73. if (value)
  74. {
  75. UseNerdIcons = false;
  76. }
  77. }
  78. }
  79. }