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

Capture output goes to terminal by default #2741

Closed
wants to merge 3 commits into from

Conversation

tinvaan
Copy link

@tinvaan tinvaan commented Jan 10, 2023

Capture and console.capture() accepts a kwargs, where you may specify echo=False to silence output being sent to terminal.

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

Attempts a fix for #2172 by implementing @willmcgugan's suggestion

Capture and console.capture() accept kwargs, where you may specify
echo=False to silence output being sent to terminal.
rich/console.py Outdated
@@ -327,9 +327,10 @@ class Capture:
console (Console): A console instance to capture output.
"""

def __init__(self, console: "Console") -> None:
def __init__(self, console: "Console", **kwargs) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see any reason to use kwargs here. It breaks typing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm guessing you're suggesting a default arg instead - def __init__(..., echo: bool=True)?

rich/console.py Outdated
@@ -342,6 +343,8 @@ def __exit__(
exc_tb: Optional[TracebackType],
) -> None:
self._result = self._console.end_capture()
if self._echo:
self._console.out(self._result)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to run captured input through print again, which may modify the result.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, guess I'll invoke print directly then.

@@ -379,22 +379,33 @@ def test_capture():
console.print("Hello")
assert capture.get() == "Hello\n"

with console.capture(echo=False) as capture:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't test that echo is working.

@tinvaan tinvaan force-pushed the fix/capture-to-terminal branch from 9c1adad to 7ead408 Compare January 13, 2023 03:11
@tinvaan
Copy link
Author

tinvaan commented Jan 13, 2023

@willmcgugan I've addressed the review comments, but I see locally that my change breaks the pager tests(tests/test_console.py::test_pager).

However, printing the test variable to console shows the text styled as expected("styled content: Hello World") but we're asserting for a plain string in the tests. Shouldn't it be the other way round? What am I missing?

Screenshot 2023-01-13 at 8 51 28 AM

Wanted to check and confirm before modifying any test logic.

@tinvaan tinvaan requested a review from willmcgugan January 13, 2023 03:23
@tinvaan
Copy link
Author

tinvaan commented Jan 17, 2023

@willmcgugan poke?

@willmcgugan
Copy link
Collaborator

There are other considerations you may not be aware of. You might want to leave this one to us.

@tinvaan
Copy link
Author

tinvaan commented Jan 17, 2023

There are other considerations you may not be aware of. You might want to leave this one to us.

Ok, I'll keep the PR open for anyone who might want to attempt a fix. Thanks!

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.

2 participants