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

feat: add the option not to include the extra newline of the Tree's __rich_console__ #3583

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

0zitro
Copy link

@0zitro 0zitro commented Dec 9, 2024

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

Provides a simple way to remove the ugly newline after the Tree string representation (for example, when using the rich.Console.capture() for a __repr__ of some tree-like class).

This is done by creating a new parameter end_new_line (name is subject to change) in the Tree constructor. This option will then determine whether the newline will be yold in the __rich_console__. This removes the need to manually delete the newline in the console output afterwards.

Usage examples:

New behaviour:

from rich.tree import Tree
from rich.console import Console

console = Console()

tree = Tree("some tree", end_new_line=False)
with console.capture() as capture:
    console.print(tree)
cap_text = capture.get()

assert cap_text == "some tree" 

Old behaviour (default):

from rich.tree import Tree
from rich.console import Console

console = Console()

tree = Tree("some tree", end_new_line=True) # default value for the `end_new_line`
with console.capture() as capture:
    console.print(tree)
cap_text = capture.get()

assert cap_text == "some tree\n" # maybe "some tree\r\n" on Windows 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant