Files
Terminal.Gui/Terminal.Gui/Views/FileSystemIconProvider.cs
Thomas Nind a8d1a79615 Fixes #2726 - Refactor filedialog classes to be more easily reused (#2727)
* Refactor FileDialogTreeBuilder to be more generally useful outside of dialog context

* Fix comparer

* Change TreeViewFileSystem scenario to use the core builder

* Refactor icon provision for reusability

* Add IsOpenGetter implementations

* Xmldoc and tests

* xmldoc and trim icon when blank (files and no nerd)

* unit test fixes

* FixFix unit tests when running on linux

* Add option to pick which icon set to use for TreeViewFileSystem

* Add spaces when using nerd to avoid icon overaps

* Refactor the addition of space for nerd icons to reduce code duplication
2023-07-05 16:51:18 -06:00

86 lines
2.6 KiB
C#

using System;
using System.IO;
using System.IO.Abstractions;
using System.Text;
namespace Terminal.Gui {
/// <summary>
/// Determines which symbol to use to represent files and directories.
/// </summary>
public class FileSystemIconProvider {
/// <summary>
/// <para>
/// Gets or sets a flag indicating whether to use Nerd Font icons.
/// Defaults to <see cref="NerdFonts.Enable"/> which can be configured
/// by end users from their <c>./.tui/config.json</c>
/// via <see cref="ConfigurationManager"/>.
/// </para>
/// <remarks>Enabling <see cref="UseNerdIcons"/> implicitly
/// disables <see cref="UseUnicodeCharacters"/>.</remarks>
/// </summary>
public bool UseNerdIcons {
get => _useNerdIcons; set {
_useNerdIcons = value;
if (value) {
UseUnicodeCharacters = false;
}
}
}
/// <summary>
/// Gets or sets a flag indicating whether to use common unicode
/// characters for file/directory icons.
/// </summary>
public bool UseUnicodeCharacters {
get => _useUnicodeCharacters;
set {
_useUnicodeCharacters = value;
if (value) {
UseNerdIcons = false;
}
}
}
private NerdFonts _nerd = new NerdFonts ();
private bool _useNerdIcons = NerdFonts.Enable;
private bool _useUnicodeCharacters;
/// <summary>
/// Gets or sets the delegate to be used to determine opened state of directories
/// when resolving <see cref="GetIcon(IFileSystemInfo)"/>. Defaults to always false.
/// </summary>
public Func<IDirectoryInfo, bool> IsOpenGetter { get; set; } = (d) => false;
/// <summary>
/// Returns the character to use to represent <paramref name="fileSystemInfo"/> or an empty
/// space if no icon should be used.
/// </summary>
/// <param name="fileSystemInfo">The file or directory requiring an icon.</param>
/// <returns></returns>
public Rune GetIcon (IFileSystemInfo fileSystemInfo)
{
if (UseNerdIcons) {
return new Rune (
_nerd.GetNerdIcon (
fileSystemInfo,
fileSystemInfo is IDirectoryInfo dir ? IsOpenGetter (dir) : false
));
}
if (fileSystemInfo is IDirectoryInfo) {
return UseUnicodeCharacters ? ConfigurationManager.Glyphs.Folder : new Rune (Path.DirectorySeparatorChar);
}
return UseUnicodeCharacters ? ConfigurationManager.Glyphs.File : new Rune (' ');
}
/// <summary>
/// Returns <see cref="GetIcon(IFileSystemInfo)"/> with an extra
/// space on the end if icon is likely to overlap adjacent cells.
/// </summary>
public string GetIconWithOptionalSpace(IFileSystemInfo fileSystemInfo)
{
var space = UseNerdIcons ? " " : "";
return GetIcon(fileSystemInfo) + space;
}
}
}