Skip to content

Commit b6a4341

Browse files
authored
Fix printing tree with rich and attribute square brackets (#437)
* test: rename variables * test: rename variables + ensure tree helper methods return Tree type * docs: enhance docstring * feat: add get_subtree as plugin * docs: enhance docstring * docs: update CHANGELOG * test: rename variables * feat: get_attr to support nested attr * bump: v1.3.0 * test: handle warning from polar tests * test: handle warning from polar tests * fix: handle pandas v3 * fix: handle pandas v3 * fix: handle pandas v3 * fix: handle pandas v3 * docs: more examples for listing directories * docs: more examples for listing directories * docs: more examples for listing directories * docs: Fix CHANGELOG * bump: update CHANGELOG * fix: Fix attr square brackets being confused as rich formatting * bump: v1.3.1
1 parent 8388228 commit b6a4341

File tree

6 files changed

+77
-12
lines changed

6 files changed

+77
-12
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Docs: Some grammar improvements in docstring.
1212
- Setup: Test with different Python environments.
1313

14+
## [1.3.1] - 2026-02-13
15+
### Added:
16+
- Docs: More examples for listing directories.
17+
### Fixed:
18+
- Tree Export: Fix attr square brackets being confused as rich formatting.
19+
- Docs: Some grammar improvements in docstring.
20+
- Setup: Test with different Python environments.
21+
1422
## [1.3.0] - 2026-01-25
1523
### Added:
1624
- Tree: Added modify methods as plugins.

bigtree/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.3.0"
1+
__version__ = "1.3.1"
22

33
from bigtree._globals import Globals
44
from bigtree.binarytree.binarytree import BinaryTree

bigtree/tree/export/stdout.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Callable, Collection, Iterable, TypeVar
3+
from typing import Any, Callable, Iterable, TypeVar
44

55
from bigtree.node import node
66
from bigtree.utils import common, constants, exceptions
@@ -62,7 +62,6 @@ def print_rich(
6262
(e.g., `\U0001f600`), or anything rich supports. If string type, it refers to ``Node`` attribute for icon.
6363
If callable type, it takes in the node itself and returns the icon
6464
"""
65-
# Add rich formatting
6665
if icon_prefix_attr:
6766
node_str_prefix = common.get_attr(_node, icon_prefix_attr, "")
6867
node_str = f"{node_str_prefix} {node_str}" if node_str_prefix else node_str
@@ -73,8 +72,8 @@ def print_rich(
7372
_node_format = common.get_attr(_node, node_format_attr, node_format)
7473
node_str = f"[{_node_format}]{node_str}[/]" if _node_format else node_str
7574
if edge_format:
76-
pre_str = f"[{edge_format}]{pre_str}[/{edge_format}]"
77-
fill_str = f"[{edge_format}]{fill_str}[/{edge_format}]"
75+
pre_str = f"[{edge_format}]{pre_str}[/]"
76+
fill_str = f"[{edge_format}]{fill_str}[/]"
7877
console.print(f"{pre_str}{fill_str}{node_str}", **kwargs)
7978

8079

@@ -88,7 +87,7 @@ def print_tree(
8887
attr_format: str = "{k}={v}",
8988
attr_sep: str = ", ",
9089
attr_omit_null: bool = False,
91-
attr_bracket: Collection[str] = ("[", "]"),
90+
attr_bracket: tuple[str, str] = ("[", "]"),
9291
style: str | Iterable[str] | constants.BasePrintStyle = "const",
9392
**kwargs: Any,
9493
) -> None:
@@ -274,6 +273,9 @@ def print_tree(
274273
from rich.console import Console
275274

276275
kwargs["console"] = kwargs.get("console") or Console(force_terminal=True)
276+
if attr_bracket[0] == "[":
277+
# Handle specific case where [ will be mistaken as rich formatting
278+
attr_bracket = ("\\" + attr_bracket[0], attr_bracket[1])
277279

278280
for pre_str, fill_str, _node in yield_tree(
279281
tree=tree,

tests/tree/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,15 @@ def tree_node_diff(tree_node):
319319
)
320320
node.Node("i", parent=other_tree_node, children=[node.Node("j")])
321321
return other_tree_node
322+
323+
324+
@pytest.fixture
325+
def rich_console():
326+
from rich.console import Console
327+
328+
return Console(
329+
# no_color=True,
330+
record=False,
331+
color_system=None,
332+
force_terminal=False,
333+
)

tests/tree/export/conftest.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
import pytest
2-
from rich.console import Console
2+
3+
from bigtree.node import node
4+
5+
6+
@pytest.fixture
7+
def tree_node_rich():
8+
"""
9+
Root
10+
├── Child 1
11+
│ └── Grandchild 1
12+
└── Child 2
13+
"""
14+
tree_node = node.Node("[magenta]Root[/]")
15+
child_node = node.Node("Child [red]1[/red]", parent=tree_node)
16+
_ = node.Node("Grand[blue]child[/] 1", parent=child_node)
17+
_ = node.Node("[blue]Child[/] [red]2[/]", parent=tree_node)
18+
return tree_node
319

420

521
@pytest.fixture
6-
def rich_console():
7-
return Console(
8-
record=False,
9-
color_system=None,
10-
force_terminal=False,
22+
def tree_node_rich_str():
23+
return (
24+
"""Root\n"""
25+
"""├── Child 1\n"""
26+
"""│ └── Grandchild 1\n"""
27+
"""└── Child 2\n"""
1128
)

tests/tree/export/test_stdout.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,18 @@ def get_node_format(_node):
603603
console=rich_console,
604604
)
605605

606+
@staticmethod
607+
def test_print_tree_rich_node_format_all_attrs(tree_node, rich_console):
608+
assert_print_statement(
609+
export.print_tree,
610+
tree_node_str,
611+
tree=tree_node,
612+
all_attrs=True,
613+
rich=True,
614+
node_format="magenta",
615+
console=rich_console,
616+
)
617+
606618
@staticmethod
607619
def test_print_tree_rich_edge_format(tree_node, rich_console):
608620
assert_print_statement(
@@ -638,6 +650,20 @@ def test_print_tree_rich_icon_attr(tree_node, rich_console):
638650
console=rich_console,
639651
)
640652

653+
@staticmethod
654+
def test_print_tree_rich_substring(
655+
tree_node_rich, tree_node_rich_str, rich_console
656+
):
657+
assert_print_statement(
658+
export.print_tree,
659+
tree_node_rich_str,
660+
tree=tree_node_rich,
661+
rich=True,
662+
icon_prefix_attr="icon",
663+
icon_suffix_attr="icon_suffix",
664+
console=rich_console,
665+
)
666+
641667

642668
class TestHPrintTree:
643669
@staticmethod

0 commit comments

Comments
 (0)