Fix #5: clarify parent directory icon

This commit is contained in:
Philip Henning 2026-09-08 18:05:26 +02:00
parent 2c7840426d
commit 48c9760e3c
2 changed files with 19 additions and 0 deletions

View file

@ -3,6 +3,7 @@
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from rich.text import Text
from textual.app import ComposeResult from textual.app import ComposeResult
from textual.binding import Binding from textual.binding import Binding
from textual.containers import Grid, Horizontal, Vertical from textual.containers import Grid, Horizontal, Vertical
@ -258,6 +259,8 @@ class ImportModal(_FormatModal):
class ParentDirectoryTree(DirectoryTree): class ParentDirectoryTree(DirectoryTree):
"""Directory tree with a conventional parent entry above its root.""" """Directory tree with a conventional parent entry above its root."""
PARENT_ICON = ""
def _populate_node(self, node, content) -> None: def _populate_node(self, node, content) -> None:
super()._populate_node(node, content) super()._populate_node(node, content)
if node is not self.root or node.data is None: if node is not self.root or node.data is None:
@ -272,6 +275,18 @@ class ParentDirectoryTree(DirectoryTree):
before=0, before=0,
) )
def render_label(self, node, base_style, style) -> Text:
"""Render the parent entry as navigation rather than as a file."""
if str(node.label) != "..":
return super().render_label(node, base_style, style)
label = node.label.copy()
label.stylize(style)
label.stylize_before(
self.get_component_rich_style("directory-tree--folder", partial=True)
)
return Text.assemble((self.PARENT_ICON, base_style), label)
class FileBrowserModal(ModalScreen[Path | None]): class FileBrowserModal(ModalScreen[Path | None]):
"""A keyboard-accessible directory tree for file and folder selection.""" """A keyboard-accessible directory tree for file and folder selection."""

View file

@ -3,6 +3,7 @@
from unittest.mock import Mock from unittest.mock import Mock
import pytest import pytest
from rich.style import Style
from textual.containers import Vertical from textual.containers import Vertical
from textual.widgets import Button, DirectoryTree, Input, RadioButton, RadioSet, Static from textual.widgets import Button, DirectoryTree, Input, RadioButton, RadioSet, Static
@ -141,6 +142,9 @@ async def test_file_browser_presents_parent_directory_as_dot_dot(tmp_path):
await pilot.pause() await pilot.pause()
assert str(tree.root.children[0].label) == ".." assert str(tree.root.children[0].label) == ".."
assert tree.render_label(
tree.root.children[0], Style(), Style()
).plain.startswith("")
assert len(browser.query("#up-button")) == 0 assert len(browser.query("#up-button")) == 0
tree.focus() tree.focus()