Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
49.04k stars 1.71k forks source link

Don't display() spurious newlines in jupyter #3329

Open NickCrews opened 5 months ago

NickCrews commented 5 months ago

Fixes #3274

I couldn't think of a way to test the "real" behavior that we are looking for, which is "are random newlines appearing in jupyter's output?", so I had to settle for the proxy of "is ipython's display() ever called?".

I materialized the Iterable[Segment]s into a list, IDK if there are some performance concerns with this (we're about to materialize them all to strings anyway???). If so then I could come up with a less-clean way of checking for "did we get passed nothing")

Type of changes

Checklist

NickCrews commented 1 month ago

@huzecong does this look like it solves your issue you described in #3274 ?

@willmcgugan friendly ping here, I realize you must have a lot on your plate, but think this should be a fairly easy review. Thank you!

huzecong commented 1 month ago

Hey @NickCrews, thanks for putting up this PR! This looks good to me and should solve the issue in #3274.

I materialized the Iterable[Segment]s into a list, IDK if there are some performance concerns with this (we're about to materialize them all to strings anyway???).

This seems reasonable to me too, but I'm no expert in this project and will leave it to the maintainers. One thing I'd like to mention is that our in-house patch only checked if not text, and that was sufficient for us. So if you're concerned about the list conversion, you might want to consider just dropping the check on segments.