-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Final word in help text is not wrapped #828
Comments
Interesting, I wonder if it's counting the space? Could you post a gist of the output when you compile clap with |
Before, inserting a newline did not move the prev_space index forward. This meant that the next word was measured incorrectly since the length was measured back to the word before the newly inserted linebreak. Fixes clap-rs#828.
I've put up a PR for fixing this and a small related bug. As explained, I hope to do a drop-in replacement later with my textwrap crate, but wanted to make sure the output didn't change first. So I compared the existing code with the wrapping done by that crate and found small problems. |
Before, inserting a newline did not move the prev_space index forward. This meant that the next word was measured incorrectly since the length was measured back to the word before the newly inserted linebreak. Fixes clap-rs#828.
Fix wrapping bugs I've been working towards integrating my [textwrap][1] crate and along the way, I found some small problems in the existing `wrap_help` function in clap. I basically added new code that calls both `textwrap::fill` and `wrap_help` and panicked on any difference: ``` fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { let input = help.clone(); let mut old = help.clone(); old_wrap_help(&mut old, longest_w, avail_chars); let mut wrapped = String::with_capacity(help.len()); for (i, line) in help.lines().enumerate() { if i > 0 { wrapped.push('\n'); } wrapped.push_str(&textwrap::fill(line, avail_chars)); } // TODO: move up, This keeps old behavior of not wrapping at all // if one of the words would overflow the line if longest_w < avail_chars { *help = wrapped; } if *old != *help { println!("********************************"); println!("longest_w: {}, avail_chars: {}", longest_w, avail_chars); println!("help: {:3} bytes: {:?}", input.len(), input); println!("old: {:3} bytes: {:?}", old.len(), old); println!("new: {:3} bytes: {:?}", help.len(), help); println!("********************************"); panic!("bad wrap"); } } fn old_wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { // ... as before ``` This PR fixes two small problems discovered this way, one of which became #828. [1]: https://crates.io/crates/textwrap
Fix wrapping bugs I've been working towards integrating my [textwrap][1] crate and along the way, I found some small problems in the existing `wrap_help` function in clap. I basically added new code that calls both `textwrap::fill` and `wrap_help` and panicked on any difference: ``` fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { let input = help.clone(); let mut old = help.clone(); old_wrap_help(&mut old, longest_w, avail_chars); let mut wrapped = String::with_capacity(help.len()); for (i, line) in help.lines().enumerate() { if i > 0 { wrapped.push('\n'); } wrapped.push_str(&textwrap::fill(line, avail_chars)); } // TODO: move up, This keeps old behavior of not wrapping at all // if one of the words would overflow the line if longest_w < avail_chars { *help = wrapped; } if *old != *help { println!("********************************"); println!("longest_w: {}, avail_chars: {}", longest_w, avail_chars); println!("help: {:3} bytes: {:?}", input.len(), input); println!("old: {:3} bytes: {:?}", old.len(), old); println!("new: {:3} bytes: {:?}", help.len(), help); println!("********************************"); panic!("bad wrap"); } } fn old_wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { // ... as before ``` This PR fixes two small problems discovered this way, one of which became #828. [1]: https://crates.io/crates/textwrap
Affected Version of clap
Tested with latest
master
, d29d7cc.Expected Behavior Summary
The final word in the help text should be wrapped.
Actual Behavior Summary
The final word remain with the next to final word, that is, the last space is ignored.
Steps to Reproduce the issue
The
"version information"
line is longer than 12 characters, despiteinformation
being only 11 characters wide.I have a fix for this that I'll post next.
The text was updated successfully, but these errors were encountered: