FileSystemIconProvider.cs 3.0 KB

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