FileSystemIconProvider.cs 3.0 KB

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