Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filtering support to DirectoryTree #2215

Merged
merged 7 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Added support for filtering a `DirectoryTree` https://github.com/Textualize/textual/pull/2215

### Changed

- Allowed border_title and border_subtitle to accept Text objects
Expand Down
20 changes: 20 additions & 0 deletions docs/examples/widgets/directory_tree_filtered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pathlib import Path
from typing import Iterable

from textual.app import App, ComposeResult
from textual.widgets import DirectoryTree


class FilteredDirectoryTree(DirectoryTree):
def filter_paths(self, paths: Iterable[Path]) -> Iterable[Path]:
return [path for path in paths if not path.name.startswith(".")]


class DirectoryTreeApp(App):
def compose(self) -> ComposeResult:
yield FilteredDirectoryTree("./")


if __name__ == "__main__":
app = DirectoryTreeApp()
app.run()
20 changes: 20 additions & 0 deletions docs/widgets/directory_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ The example below creates a simple tree to navigate the current working director
--8<-- "docs/examples/widgets/directory_tree.py"
```

## Filtering

There may be times where you want to filter what appears in the
`DirectoryTree`. To do this inherit from `DirectoryTree` and implement your
own version of the `filter_paths` method. It should take an iterable of
Python `Path` objects, and return those that pass the filter. For example,
if you wanted to take the above code an filter out all of the "hidden" files
and directories:

=== "Output"

```{.textual path="docs/examples/widgets/directory_tree_filtered.py"}
```

=== "directory_tree_filtered.py"

~~~python
--8<-- "docs/examples/widgets/directory_tree_filtered.py"
~~~

## Messages

### ::: textual.widgets.DirectoryTree.FileSelected
Expand Down
19 changes: 17 additions & 2 deletions src/textual/widgets/_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from dataclasses import dataclass
from pathlib import Path
from typing import ClassVar
from typing import ClassVar, Iterable

from rich.style import Style
from rich.text import Text, TextType
Expand Down Expand Up @@ -149,12 +149,27 @@ def render_label(self, node: TreeNode[DirEntry], base_style: Style, style: Style
text = Text.assemble(prefix, node_label)
return text

def filter_paths(self, paths: Iterable[Path]) -> Iterable[Path]:
"""Filter the paths before adding them to the tree.

Args:
paths: The paths to be filtered.

Returns:
The filtered paths.

By default this method returns all of the paths provided. To create
a filtered `DirectoryTree` inherit from it and implement your own
version of this method.
"""
return paths

def load_directory(self, node: TreeNode[DirEntry]) -> None:
assert node.data is not None
dir_path = Path(node.data.path)
node.data.loaded = True
directory = sorted(
list(dir_path.iterdir()),
self.filter_paths(dir_path.iterdir()),
key=lambda path: (not path.is_dir(), path.name.lower()),
)
for path in directory:
Expand Down