Fix carriage return to correctly remove lines from the GUI console #126
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
If you work with Instamatic and tqdm, or you tried to run some calibration from the main GUI which communicates using the
instamatic.tools.printer
with '\r\x1b[K' return sequence, you probably ran into the same problem I have. While both these commands produce quickly-changing strings in the command-line terminal, e.g. progress bars, the carriage return does not work correctly in the GUI console. According to my research, it should not:tkinter.Text
was never designed with carriage return or ANSI escape codes in mind. As a result, after counting merely to 25, it starts looking like that:This PR improves the behavior of the carriage return symbol '\r': any time console's
Writer
class now gets a message with '\r', it will immediately strip everything on its left, remove the carriage return, remove any other ANSI escape codes, delete the last message in the terminal, and print the new message in its place. As a result, bothtqdm
andprinter
now work completely correctly in the GUI! You can even update a line yourself by passing a string that starts with '\r'. Below you will see the beam shift calibration running completely correctly in the GUI console:This has an added benefit (i.e. my original reason to look at it): it makes the GUI faster! For very long lines, tkinter can take surprisingly long to calculate line wrapping. As an example, I ran a 15 x 15 = 225-point beam shift calibration on 2 PCs, my work PC1 and the microscope-handling PC2. After ~200 points, writing to the console on PC2 took longer than the rest of the calibration step!
The only caveat with this PR is it makes the GUI actively remove lines from the console. This means that if the user clicks somewhere in the console during
tqdm
orprinter
loop, the console might start removing random lines and appending new info at the end. Previously it would only write in a completely wrong place. Given that the console itself is very wonky and user can just as well remove data by hand (because it is a normal text field), I am not particularly concerned with it.Note: you can test the feature using Advanced tab -> run custom script -> 'showcase_movie.py', left in
src/instamatic/config/scripts/showcase_movie.py
after PR #121. I can see not time improvements there, but the caret return works as intended.Bug fixes
tqdm
andprinter
output much nicer;