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

Fix double-width characters disappearing when wrapping #3180

Merged
merged 22 commits into from
Nov 14, 2023

Conversation

darrenburns
Copy link
Member

@darrenburns darrenburns commented Nov 1, 2023

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

Update wrapping logic to fix issues with CJK charcters disappearing when the "fold" location sat within a double-width character. Ensure we retain browser logic of:

if there is no space on the current line, move to a new line, and if theres not enough space on the entire new line, fold the text over multiple lines at appropriate locations.

Adds some additional tests and docstrings, documentation etc.

Fixes #3176

The wrapping process is overall still quite simple and doesn't match the browser in many cases. For example, wrapping does not consider punctuation (lines can begin with punctuation), and whitespace is handled differently (but practically speaking it seems sensible).

…'s referring to the number of *single-width* characters.

Also a small addition to the gitignore file.
…hen the "fold" location sat *within* a double-width character. Ensure we retain browser logic of: if there is no space on the current line, move to a new line, and if theres not enough space on the entire new line, fold the text over multiple lines at appropriate locations.
@codecov-commenter
Copy link

codecov-commenter commented Nov 1, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (aabfd16) 98.30% compared to head (05c5dfc) 98.28%.
Report is 34 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3180      +/-   ##
==========================================
- Coverage   98.30%   98.28%   -0.03%     
==========================================
  Files          74       74              
  Lines        8038     8049      +11     
==========================================
+ Hits         7902     7911       +9     
- Misses        136      138       +2     
Flag Coverage Δ
unittests 98.28% <97.91%> (-0.03%) ⬇️

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

Files Coverage Δ
rich/_export_format.py 100.00% <ø> (ø)
rich/_wrap.py 100.00% <100.00%> (ø)
rich/align.py 100.00% <ø> (ø)
rich/cells.py 100.00% <100.00%> (ø)
rich/console.py 98.04% <100.00%> (+<0.01%) ⬆️
rich/containers.py 100.00% <ø> (ø)
rich/markdown.py 99.45% <100.00%> (ø)
rich/markup.py 100.00% <ø> (ø)
rich/panel.py 98.29% <ø> (ø)
rich/progress.py 92.92% <ø> (ø)
... and 2 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@darrenburns darrenburns marked this pull request as ready for review November 1, 2023 17:56
Copy link
Contributor

@rodrigogiraoserrao rodrigogiraoserrao left a comment

Choose a reason for hiding this comment

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

I suggested a couple of tweaks but they are fairly subjective, so I won't block the merge because of them.

rich/_wrap.py Outdated Show resolved Hide resolved
divides: List[int] = []
append = divides.append
line_position = 0
def divide_line(text: str, width: int, fold: bool = True) -> list[int]:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming cell_offset is keeping track of how much space is already occupied in the line we're currently filling, right?
I personally don't love the name but maybe that could be mitigated with a short comment next to it?

rich/_wrap.py Show resolved Hide resolved
rich/text.py Outdated Show resolved Hide resolved
tests/test_cells.py Outdated Show resolved Hide resolved
@darrenburns darrenburns merged commit 59b1aca into master Nov 14, 2023
22 checks passed
@darrenburns darrenburns deleted the double-width-wrapping-fix branch November 14, 2023 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants