console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.22k stars 238 forks source link

Fix subtract with overflow when measuring terminal line length #547

Closed foresterre closed 1 year ago

foresterre commented 1 year ago

If console::measure_text_width returns 0, the line is effectively empty, although line.is_empty() may still be false. This can for example happen when there is a line which just consists of ANSI color escape sequences.

I hope I didn't undo the work done in #533.

An alternative would be to do:

// make sure we never subtract with overflow
let real_len = max(real_len, self.orphan_lines_count + shift);

*last_line_count = real_len - self.orphan_lines_count + shift;

closes #546

djc commented 1 year ago

Sorry for the regression, and thanks for the report and fix! I'm going to merge and release this -- it would be great if you are able to add a test that covers this behavior somehow.

foresterre commented 1 year ago

it would be great if you are able to add a test that covers this behavior somehow

Agreed! I did try to create a test case, but didn't get it to fail properly yet, which is weird 🙃. I expected that pb.println("<ansii color escape sequence>"); pb.tick(); would work (as this is what I saw in my program), but somehow it didn't.

I'll have another try tonight.

foresterre commented 1 year ago

I added a test 🙂.

foresterre commented 1 year ago

I removed the commit which added the same as #549, and rebased on main.

djc commented 1 year ago

Great work, thanks!