From 65c7e37a82e857631c1068315cc7494a7a305387 Mon Sep 17 00:00:00 2001 From: 0zitro <94910351+0zitro@users.noreply.github.com> Date: Mon, 9 Dec 2024 02:24:19 +0100 Subject: [PATCH 1/2] feat: add the option not to include the extra newline of the `Tree`'s `__rich_console__` --- rich/tree.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rich/tree.py b/rich/tree.py index 9a87d60de..fe06ff1bd 100644 --- a/rich/tree.py +++ b/rich/tree.py @@ -43,6 +43,7 @@ def __init__( expanded: bool = True, highlight: bool = False, hide_root: bool = False, + end_new_line: bool = True, ) -> None: self.label = label self.style = style @@ -51,6 +52,7 @@ def __init__( self.expanded = expanded self.highlight = highlight self.hide_root = hide_root + self.end_new_line = end_new_line def add( self, @@ -79,6 +81,7 @@ def add( guide_style=self.guide_style if guide_style is None else guide_style, expanded=expanded, highlight=self.highlight if highlight is None else highlight, + end_new_line=True, ) self.children.append(node) return node @@ -155,7 +158,8 @@ def make_guide(index: int, style: Style) -> Segment: post_style=remove_guide_styles, ) yield from line - yield new_line + if self.end_new_line: + yield new_line if first and prefix: prefix[-1] = make_guide( SPACE if last else CONTINUE, prefix[-1].style or null_style From 909f844be50fb5590434e319bd723f15b75ec899 Mon Sep 17 00:00:00 2001 From: 0zitro <94910351+0zitro@users.noreply.github.com> Date: Mon, 9 Dec 2024 03:03:45 +0100 Subject: [PATCH 2/2] fix: optional newline support for tree children --- rich/tree.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rich/tree.py b/rich/tree.py index fe06ff1bd..a3d46d5b2 100644 --- a/rich/tree.py +++ b/rich/tree.py @@ -158,8 +158,11 @@ def make_guide(index: int, style: Style) -> Segment: post_style=remove_guide_styles, ) yield from line - if self.end_new_line: + + # Equivalent of `if not last or (depth == 0) or (depth != 0 and self.end_new_line):` + if not last or depth == 0 or self.end_new_line: yield new_line + if first and prefix: prefix[-1] = make_guide( SPACE if last else CONTINUE, prefix[-1].style or null_style