11from __future__ import annotations
22
3- from asyncio import Queue
3+ from asyncio import Queue , sleep
44from dataclasses import dataclass
55from pathlib import Path
66from typing import TYPE_CHECKING , Callable , ClassVar , Iterable , Iterator
@@ -56,7 +56,7 @@ class DirectoryTree(Tree[DirEntry]):
5656
5757 DEFAULT_CSS = """
5858 DirectoryTree {
59-
59+
6060 & > .directory-tree--folder {
6161 text-style: bold;
6262 }
@@ -70,11 +70,11 @@ class DirectoryTree(Tree[DirEntry]):
7070 }
7171
7272 &:ansi {
73-
73+
7474 & > .tree--guides {
75- color: transparent;
75+ color: transparent;
7676 }
77-
77+
7878 & > .directory-tree--folder {
7979 text-style: bold;
8080 }
@@ -452,6 +452,37 @@ def filter_paths(self, paths: Iterable[Path]) -> Iterable[Path]:
452452 """
453453 return paths
454454
455+ async def expand_by_path (self , target_path : Path ) -> None :
456+ """
457+ Expands the directory tree to reveal the target path.
458+
459+ Args:
460+ target_path: The filesystem path to reveal in the tree.
461+ """
462+
463+ async def expand_to_target_path_recursively (
464+ current_node : TreeNode , target_path : Path
465+ ) -> None :
466+ for child_node in current_node .children :
467+ child_path = child_node .data .path # Current directory path
468+
469+ if (
470+ target_path .is_relative_to (child_path )
471+ or child_path == target_path
472+ ):
473+ child_node .expand ()
474+ await sleep (
475+ 0.1
476+ ) # Hack to wait for children to populate
477+ self .move_cursor (node = child_node )
478+ await expand_to_target_path_recursively (
479+ current_node = child_node , target_path = target_path
480+ )
481+ return
482+
483+ # Start expanding from the root of the directory tree
484+ await expand_to_target_path_recursively (self .root , target_path )
485+
455486 @staticmethod
456487 def _safe_is_dir (path : Path ) -> bool :
457488 """Safely check if a path is a directory.
0 commit comments