textual_fspicker.icons
Helper code for picking an 'icon' to display along with directory entries.
By default the library will display just two icons: either a folder style icon next to directory entries, or a page style icon next to file entries.
In your application you may wish to use a richer set of icons, perhaps you
even want to make use of Nerd Fonts or
something similar. The Icons
class lets
you override the icon-picking behaviour.
DEFAULT_FILE_ICON
module-attribute
DEFAULT_FILE_ICON = from_markup(':page_facing_up:')
The default icon to use for a file.
DEFAULT_FOLDER_ICON
module-attribute
DEFAULT_FOLDER_ICON = from_markup(':file_folder:')
The default icon to use for a folder.
Icons
Helper class for picking the best icon to use for a directory entry.
best_for
classmethod
best_for(location)
Get the best icon for a given location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | Path
|
The location to get an icon for. |
required |
Returns:
Type | Description |
---|---|
Text
|
The chosen icon for the location. |
Example
>>> from textual_fspicker import Icons
>>> Icons.best_for(".")
📁
>>> Icons.best_for("pyproject.toml")
📄
set_picker
classmethod
set_picker(icon_picker)
Set the function that will pick the best icon for a directory entry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Callable[[str | Path], Text]
|
A function that will return a
Rich |
required |
The given function should take a Path
and return a
Rich Text
object that is an icon that can be
used to represent that location. For example, given this function:
from pathlib import Path
from rich.text import Text
from textual_fspicker.icons import DEFAULT_FILE_ICON, DEFAULT_FOLDER_ICON
from textual_fspicker.safe_tests import is_dir
def my_icon_picker(location: Path) -> Text:
if location.suffix.lower() == ".py":
return Text.from_markup(":snake:")
return DEFAULT_FOLDER_ICON if is_dir(location) else DEFAULT_FILE_ICON
If the picker is set to it:
Icons.set_picker(my_icon_picker)
Any subsequent use of
Icon.best_for
will use a
snake for any files with a py
extension:
>>> Icons.best_for(".")
📁
>>> Icons.best_for("pyproject.toml")
📄
>>> Icons.best_for("src/textual_fspicker/__init__.py")
🐍
Important
Remember to keep the code as fast as possible, as it will be
called for every entry that is shown in one of the dialogs. Also
remember to perform tests in the safest way possible (see
safe_tests
for example).