-
Notifications
You must be signed in to change notification settings - Fork 201
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
Fix Carriage Return Handling in QtConsole #607
Conversation
Preserve position of cursor so ANSI commands such as \r that modify position will preserve the position they specifed over multiple calls, even if these calls are not all combined into one.
c614840
to
ed4ddc8
Compare
Add regression to test for jupyter#272
Added regression test. Needed to run it manually, as the |
Hey @TheMatt2, thanks for your contribution! Unfortunately, it doesn't seem the right approach if it breaks scrolling. My suggestion is for you to rethink your changes so that |
Let me rephrase. Working on it, to see if I can figure it out, and go through the CI results. |
Ok, sorry for the misunderstanding. Since I haven't seen that test failing before, I assumed it was do to your changes. |
So it seems the following error the CI reports is introduced by the PR, I'm looking into what the implications are.
The error I was referring to previously, is in my testing And on Windows, And it appears these failures were masking that there is an issue in this PR that needs to be addressed. |
8ee003b
to
96ab4e8
Compare
Instead of using the main cursor (which the user can control by clicking), create a dedicated cursor `_insert_text_cursor` to track the position of where text should be inserted.
Need to set _executing = True for test to work.
Updated approach to not break |
Change to draft, as |
After much revision, found a way to deal with this that solves the issue. The issue was text is added using Solution is to only use This has been added to the test cases. The way you could trigger this corner case where you still need insert mode is something like this |
At this point, the issue is solved, and all test cases pass, but I have one last edge case I want to chase down before removing the draft tag. |
And added test case.
Added fix for an edge case where switching between printing before the prompt, and printing after the prompt caused the All tests past... but there's a remaining issue if the prompt is a blank character... i.e. |
Make sure prompt is correctly setup even if the prompt is literally a blank string, as could be set using input()
Changes actually broke all input() handling. Reverting. This reverts commit 50bb0fd.
So I've confirmed this is a separate issue that doesn't require any changes to this PR. Before digging too much into that, I would actually like some feedback on the change in this PR, as this seems to now all being working (took a while though). |
@ccordoba12 Thoughts? |
Hi @TheMatt2 thank you for all you work here and sorry for the late response! Could it be possible for you to merge with latest main so this can get green CI (some tests fixes have been done over there)? Also, I gave a quick check to this PR manually and seems like things are working as expected 👍 Will try to do a more in depth code review now but if there are any specific changes that you think need more attention let us know! |
Sorry for dropping the ball here. @dalthviz will take it from here, he's also a maintainer of this repo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you again @TheMatt2 for your work here! Left a comment to update a method docstring and several suggestions for format nitpicks. Other than that this LGTM 👍
Merged in main and made format changes. None of the changes in main look like they impact this code directly. Appreciate you guys taking a look at this. As for a note on the readiness of this code, I think this code is tested and improves on the current behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once again thank you @TheMatt2 ! This LGTM 👍
Just in case, do you have any further feedback @ccordoba12 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some small suggestions for you @TheMatt2, the rest looks good to me.
@ccordoba12 to move this forward, is okay if we merge this as it is and then over a follow up PR I work on the style suggestions you left here? |
Sure, go ahead and merge this one. |
Fix the handling of carriage return so it is not ignored at the end of a print line.
This allows for progress bars that rely on adding a carriage return at the end of the line such as:
tl;dr
Preserve position of cursor so ANSI commands such as \r that modify position will preserve the position they specified over multiple calls. This is particularly relevant if time passes between print calls (~100 ms).
Explanation
First of all, when you
print()
or callsys.stdout.write()
in QtConsole, this ultimately causes the text to be passed to the frontend widget.qtconsole/qtconsole/frontend_widget.py
Line 729 in 56e5a5e
This will lead to the text by passed to the console widget, which will call the ANSI Processor in:
qtconsole/qtconsole/console_widget.py
Line 2085 in 56e5a5e
The ANSI preprocessor already handles the carriage return, so code that that uses the carriage return in the middle of a print already worked.
This will even work for calls from multiple prints, but this only works because the text is internally buffered, and recombined so
print("Hello", end = "\r"); print("World")
is all combined into the text"Hello\rWorld" by the time
_insert_plain_text()` is called.However,
_insert_plain_text()
and_flush_pending_stream()
both force the cursor position to the end of the line at the end of the call. This means that a'\r'
at the end of a line is effectively ignored, if something stops the text from being combined back together, such as more than ~100 ms passing between prints.This PR fixes this issue by updating the cursor position to the place it was put to by
_insert_plain_text()
.Before this gets merged, I do want to make sure there are no odd situations where the cursor now not being the end of text leads to issues.
Behavior Change
Before
After
Notes
Related Github Issues
\r
doesn't overwrite print() spyder-ide/spyder#20388This does not exactly address #350, not adding support for any new ANSI codes, just fixing the existing support for
'\r'