Skip to content

Commit 0f04921

Browse files
authored
Merge pull request #3378 from jdvanwijk/master
Tree: ASCII_GUIDES and TREE_GUIDES promoted to class attributes
2 parents cf9f331 + db17c23 commit 0f04921

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
89
## Unreleased
910

1011
### Fixed
@@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2324
- Removed the empty line printed in jupyter while using `Progress` https://github.com/Textualize/rich/pull/2616
2425
- Running tests in environment with `FORCE_COLOR` or `NO_COLOR` environment variables
2526
- ansi decoder will now strip problematic private escape sequences (like `\x1b7`) https://github.com/Textualize/rich/pull/3278/
27+
- Tree's ASCII_GUIDES and TREE_GUIDES constants promoted to class attributes
2628

2729
### Added
2830

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The following people have contributed to the development of Rich:
7171
- [Gabriele N. Tornetta](https://github.com/p403n1x87)
7272
- [Nils Vu](https://github.com/nilsvu)
7373
- [Arian Mollik Wasi](https://github.com/wasi-master)
74+
- [Jan van Wijk](https://github.com/jdvanwijk)
7475
- [Handhika Yanuar Pratama](https://github.com/theDreamer911)
7576
- [za](https://github.com/za)
7677
- [Motahhar Mokfi](https://github.com/motahhar)

rich/tree.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
from .style import Style, StyleStack, StyleType
99
from .styled import Styled
1010

11+
GuideType = Tuple[str, str, str, str]
1112

1213
class Tree(JupyterMixin):
1314
"""A renderable for a tree structure.
1415
16+
Attributes:
17+
ASCII_GUIDES (GuideType): Guide lines used when Console.ascii_only is True.
18+
TREE_GUIDES (List[GuideType, GuideType, GuideType]): Default guide lines.
19+
1520
Args:
1621
label (RenderableType): The renderable or str for the tree label.
1722
style (StyleType, optional): Style of this tree. Defaults to "tree".
@@ -21,6 +26,13 @@ class Tree(JupyterMixin):
2126
hide_root (bool, optional): Hide the root node. Defaults to False.
2227
"""
2328

29+
ASCII_GUIDES = (" ", "| ", "+-- ", "`-- ")
30+
TREE_GUIDES = [
31+
(" ", "│ ", "├── ", "└── "),
32+
(" ", "┃ ", "┣━━ ", "┗━━ "),
33+
(" ", "║ ", "╠══ ", "╚══ "),
34+
]
35+
2436
def __init__(
2537
self,
2638
label: RenderableType,
@@ -83,21 +95,15 @@ def __rich_console__(
8395
guide_style = get_style(self.guide_style, default="") or null_style
8496
SPACE, CONTINUE, FORK, END = range(4)
8597

86-
ASCII_GUIDES = (" ", "| ", "+-- ", "`-- ")
87-
TREE_GUIDES = [
88-
(" ", "│ ", "├── ", "└── "),
89-
(" ", "┃ ", "┣━━ ", "┗━━ "),
90-
(" ", "║ ", "╠══ ", "╚══ "),
91-
]
9298
_Segment = Segment
9399

94100
def make_guide(index: int, style: Style) -> Segment:
95101
"""Make a Segment for a level of the guide lines."""
96102
if options.ascii_only:
97-
line = ASCII_GUIDES[index]
103+
line = self.ASCII_GUIDES[index]
98104
else:
99105
guide = 1 if style.bold else (2 if style.underline2 else 0)
100-
line = TREE_GUIDES[0 if options.legacy_windows else guide][index]
106+
line = self.TREE_GUIDES[0 if options.legacy_windows else guide][index]
101107
return _Segment(line, style)
102108

103109
levels: List[Segment] = [make_guide(CONTINUE, guide_style)]

0 commit comments

Comments
 (0)