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

Add echo param to Console.capture #2347

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

Add echo param to Console.capture #2347

wants to merge 11 commits into from

Conversation

darrenburns
Copy link
Member

@darrenburns darrenburns commented Jun 16, 2022

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

Adds echo parameter to the capture method of Console. When True, captured output will also be written to the output file.

@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2022

Codecov Report

Merging #2347 (3b02ec9) into master (d110847) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #2347      +/-   ##
==========================================
- Coverage   98.71%   98.66%   -0.05%     
==========================================
  Files          73       72       -1     
  Lines        7704     7721      +17     
==========================================
+ Hits         7605     7618      +13     
- Misses         99      103       +4     
Flag Coverage Δ
unittests 98.66% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
rich/console.py 98.32% <100.00%> (+0.03%) ⬆️
rich/cells.py 96.05% <0.00%> (-3.95%) ⬇️
rich/style.py 99.75% <0.00%> (-0.25%) ⬇️
rich/rule.py 100.00% <0.00%> (ø)
rich/_lru_cache.py

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d110847...3b02ec9. Read the comment docs.

@darrenburns darrenburns linked an issue Jun 16, 2022 that may be closed by this pull request
Base automatically changed from capture-bugfix to master June 17, 2022 10:40
Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

Can update the docs to describe the new echo param please.

rich/console.py Outdated Show resolved Hide resolved
rich/console.py Outdated
@@ -723,6 +727,7 @@ def __init__(
self._thread_locals = ConsoleThreadLocals(
theme_stack=ThemeStack(themes.DEFAULT if theme is None else theme)
)
self._buffer_indices_to_render: Set[int] = {self._thread_locals.buffer_index}
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 quite follow what the intent of this is. Handling nested captures? Can you explain to me on Tuesday please.

Copy link
Member Author

Choose a reason for hiding this comment

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

Previously we only rendered content if the buffer index was 0. This was a hard-coded check. Now we also want to do this if we're inside a capture context (the buffer index is not 0) and where the echo arg is True, so we record the indices where that's the case.

Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

A few requests.

rich/console.py Outdated Show resolved Hide resolved
rich/console.py Show resolved Hide resolved
@@ -368,6 +368,19 @@ def test_capture_and_record(capsys):
assert out == "ABC\n"


def test_capture_echo_outputs_captured_content_to_terminal(capsys):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a test with more nesting. e..g. a capture inside a capture. Or a simple with console: block within a capture.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tried this test and it passes on master (it shouldn't), so I'm not sure nested captures work at all:

def test_capture_echo_nested_capture():
    console = Console()
    console.print(1)
    with console.capture() as capture1:
        console.print(2)
        with console.capture() as capture2:
            console.print(3)
    console.print(4)

    assert capture1.get() == ""
    assert capture2.get() == ""

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering what the expected behaviour should be.

The outer capture prevents content from getting to the terminal. So the inner capture has nothing to capture? So maybe this is the expected behaviour.

Could you also test this:

with console.capture():
    with console:
        console.print("foo")

The with console should make the block atomic, i.e. it saves up all content until it exists the block.

Copy link
Member Author

Choose a reason for hiding this comment

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

The outer capture prevents content from getting to the terminal. So the inner capture has nothing to capture? So maybe this is the expected behaviour.

I'd still expect the 2 to be captured inside capture1.

This test passes:

def test_capture_echo_nested_capture_console():
    console = Console()
    with console.capture() as capture1:
        console.print(1)
        with console:
            console.print(2)

    assert capture1.get() == "1\n2\n"

Copy link
Member Author

Choose a reason for hiding this comment

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

Scrap this, I think things are working as expected.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, now that I've refreshed my memory with this stuff 😄 ...

This is the behaviour on master, which looks wrong:

def test_capture_echo_nested_capture():
    console = Console()
    console.print(1)
    with console.capture() as capture1:
        console.print(2)
        with console.capture() as capture2:
            console.print(3)
    console.print(4)

    assert capture1.get() == ""
    assert capture2.get() == "2\n3\n"

My expectation would be that capture1 captures 2\n3\n and capture2 captures 3\n.

rich/console.py Outdated Show resolved Hide resolved
@nathanrpage97
Copy link
Contributor

Just wanted to see if echo is the right term or would tee be better?

@darrenburns
Copy link
Member Author

darrenburns commented Jun 29, 2022

@nathanrpage97 I agree, I don't think echo is the right term either 🤷‍♂️ tee has better semantics to me but I had to click the link you provided as I couldn't remember what it was. Could also do something like write_to_terminal defaulted to False.

@nathanrpage97
Copy link
Contributor

Yea, I guess it may be a bit esoteric for the general users of the library

@willmcgugan willmcgugan self-assigned this Sep 19, 2022
@ssbarnea
Copy link
Contributor

ssbarnea commented Oct 4, 2022

@darrenburns Any chance to revive this? The changes introduced by 12.5.1 broke capture support in enrich, pycontribs/enrich#40 and temporary I had to pin-down rich as I would need the echo/tee functionality. Thanks.

@ssbarnea
Copy link
Contributor

@willmcgugan That PR happens to be on xmas wishlist for quite some time. I wonder if Santa is feeling generous these days ;)

@ssbarnea
Copy link
Contributor

@darrenburns any chance to update it? It would be quite cool to see it in.

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.

Added echo bool to capture.
5 participants