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