Skip to content
Open
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
39 changes: 35 additions & 4 deletions src/textual/widgets/_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class DirectoryTree(Tree[DirEntry]):

DEFAULT_CSS = """
DirectoryTree {

& > .directory-tree--folder {
text-style: bold;
}
Expand All @@ -71,11 +71,11 @@ class DirectoryTree(Tree[DirEntry]):
}

&:ansi {

& > .tree--guides {
color: transparent;
color: transparent;
}

& > .directory-tree--folder {
text-style: bold;
}
Expand Down Expand Up @@ -453,6 +453,37 @@ def filter_paths(self, paths: Iterable[Path]) -> Iterable[Path]:
"""
return paths

async def expand_by_path(self, target_path: Path) -> None:
"""
Expands the directory tree to reveal the target path.

Args:
target_path: The filesystem path to reveal in the tree.
"""

async def expand_to_target_path_recursively(
current_node: TreeNode, target_path: Path
) -> None:
for child_node in current_node.children:
child_path = child_node.data.path # Current directory path

if (
target_path.is_relative_to(child_path)
or child_path == target_path
):
child_node.expand()
await asyncio.sleep(
0.1
) # Hack to wait for children to populate
self.move_cursor(node=child_node)
await expand_to_target_path_recursively(
current_node=child_node, target_path=target_path
)
return

# Start expanding from the root of the directory tree
await expand_to_target_path_recursively(self.root, target_path)

@staticmethod
def _safe_is_dir(path: Path) -> bool:
"""Safely check if a path is a directory.
Expand Down