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

ignore ANSI escape codes in the bar length calculation #592

Merged
merged 2 commits into from Aug 20, 2018

Conversation

jnhansen
Copy link

Fixes #591.

This strips ANSI escape codes from the description string before calculating the remaining length of the bar, such that strings containing e.g. color codes are still displayed with the correct width.

@codecov-io
Copy link

codecov-io commented Aug 11, 2018

Codecov Report

Merging #592 into master will decrease coverage by 0.13%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #592      +/-   ##
==========================================
- Coverage   99.28%   99.14%   -0.14%     
==========================================
  Files           9        9              
  Lines         698      702       +4     
  Branches      123      125       +2     
==========================================
+ Hits          693      696       +3     
  Misses          3        3              
- Partials        2        3       +1

CUR_OS = _curos()
IS_WIN = CUR_OS in ['Windows', 'cli']
IS_NIX = (not IS_WIN) and any(
CUR_OS.startswith(i) for i in
['CYGWIN', 'MSYS', 'Linux', 'Darwin', 'SunOS', 'FreeBSD', 'NetBSD'])
RE_ANSI = re.compile(r"\x1b\[[;\d]*[A-Za-z]")
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

could this pick up non-ANSI codes? Maybe as per https://stackoverflow.com/a/1833984 we need \x1b.*?m or \x1b[^m]*m?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think it would, as this sequence matches Esc[ + (digits and ;) + (one letter), which should only occur in ANSI escape codes: http://ascii-table.com/ansi-escape-sequences.php

In the stackoverflow entry the problem is that the author used arbitrary character matching (.*).

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

lgtm, technically other sequences accept = as well as strings, but they shouldn't be used in a progress bar.

@casperdcl casperdcl added the p3-enhancement 🔥 Much new such feature label Aug 14, 2018
casperdcl added a commit that referenced this pull request Aug 17, 2018
@casperdcl casperdcl merged commit d6b05fb into tqdm:master Aug 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-enhancement 🔥 Much new such feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants