diff --git a/src/app/help.rs b/src/app/help.rs index 56cabba33ab..ad677756367 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -948,7 +948,7 @@ fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { continue; } debugln!("Help::wrap_help:iter: Reached the end of the line and we're over..."); - } else if str_width(&help[j..idx]) < avail_chars { + } else if str_width(&help[j..idx]) <= avail_chars { debugln!("Help::wrap_help:iter: Space found with room..."); prev_space = idx; continue; @@ -957,11 +957,24 @@ fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { j = prev_space; debugln!("Help::wrap_help:iter: prev_space={}, j={}", prev_space, j); debugln!("Help::wrap_help:iter: Removing...{}", j); - debugln!("Help::wrap_help:iter: Char at {}...{}", j, &help[j..j]); + debugln!("Help::wrap_help:iter: Char at {}: {:?}", j, &help[j..j+1]); help.remove(j); help.insert(j, '\n'); + prev_space = idx; } } else { sdebugln!("No"); } } + +#[cfg(test)] +mod test { + use super::wrap_help; + + #[test] + fn wrap_help_last_word() { + let mut help = String::from("foo bar baz"); + wrap_help(&mut help, 3, 5); + assert_eq!(help, "foo\nbar\nbaz"); + } +} diff --git a/tests/help.rs b/tests/help.rs index 00e14f6805e..0393f057717 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -113,9 +113,8 @@ OPTIONS: latte, cappuccino, espresso), tea, and other hot beverages. Some coffeehouses also serve cold beverages such as iced coffee and iced - tea. Many cafés also serve some type of - food, such as light snacks, muffins, or - pastries."; + tea. Many cafés also serve some type of food, + such as light snacks, muffins, or pastries."; static ISSUE_626_PANIC: &'static str = "ctest 0.1 @@ -149,6 +148,20 @@ OPTIONS: -c, --cafe A coffeehouse, coffee shop, or café. -p, --pos Some vals [values: fast, slow]"; +static FINAL_WORD_WRAPPING: &'static str = "ctest 0.1 + +USAGE: + ctest + +FLAGS: + -h, --help + Prints help + information + -V, --version + Prints + version + information"; + static OLD_NEWLINE_CHARS: &'static str = "ctest 0.1 USAGE: @@ -410,6 +423,12 @@ fn issue_626_variable_panic() { } } +#[test] +fn final_word_wrapping() { + let app = App::new("ctest").version("0.1").set_term_width(24); + assert!(test::compare_output(app, "ctest --help", FINAL_WORD_WRAPPING, false)); +} + #[test] fn old_newline_chars() { let app = App::new("ctest") @@ -514,4 +533,4 @@ fn issue_777_wrap_all_things() { .about("Show how the about text is not wrapped") .set_term_width(35); assert!(test::compare_output(app, "ctest --help", ISSUE_777, false)); -} \ No newline at end of file +}