FileSystemIconProvider.cs 2.6 KB

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