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

Allow paths when creating 'DirectoryTree'. #2095

Merged
merged 2 commits into from
Mar 21, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Tabs widget now sends Tabs.Cleared when there is no active tab.
- Breaking change: changed default behaviour of `Vertical` (see `VerticalScroll`) https://github.com/Textualize/textual/issues/1957
- The default `overflow` style for `Horizontal` was changed to `hidden hidden` https://github.com/Textualize/textual/issues/1957
- `DirectoryTree` also accepts `pathlib.Path` objects as the path to list https://github.com/Textualize/textual/issues/1438

### Removed

Expand Down
19 changes: 10 additions & 9 deletions src/textual/widgets/_directory_tree.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from __future__ import annotations

import os
from dataclasses import dataclass
from pathlib import Path
from typing import ClassVar

from rich.style import Style
from rich.text import Text, TextType

from .._types import MessageTarget
from ..message import Message
from ._tree import TOGGLE_STYLE, Tree, TreeNode


@dataclass
class DirEntry:
"""Attaches directory information ot a node."""
"""Attaches directory information to a node."""

path: str
is_dir: bool
Expand All @@ -26,9 +26,9 @@ class DirectoryTree(Tree[DirEntry]):

Args:
path: Path to directory.
name: The name of the widget, or None for no name. Defaults to None.
id: The ID of the widget in the DOM, or None for no ID. Defaults to None.
classes: A space-separated list of classes, or None for no classes. Defaults to None.
name: The name of the widget, or None for no name.
id: The ID of the widget in the DOM, or None for no ID.
classes: A space-separated list of classes, or None for no classes.
disabled: Whether the directory tree is disabled or not.
"""

Expand Down Expand Up @@ -83,17 +83,18 @@ def __init__(self, path: str) -> None:

def __init__(
self,
path: str,
path: str | Path,
*,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
) -> None:
self.path = path
str_path = os.fspath(path)
self.path = str_path
super().__init__(
path,
data=DirEntry(path, True),
str_path,
data=DirEntry(str_path, True),
name=name,
id=id,
classes=classes,
Expand Down