From aff4ba18da8147e1259b04b0bfbc1fcb5c78a3c0 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sun, 8 Jan 2017 12:51:20 +0100 Subject: [PATCH 1/4] fix: include final character in line lenght Before, wrapping the help text at, say, 80 characters really meant that every line could be at most 79 characters wide. Lines can now be up to and including avail_chars columns wide. If needed, a desired margin or padding can be subtracted from the avail_chars argument at a later point. --- src/app/help.rs | 2 +- tests/help.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/help.rs b/src/app/help.rs index 56cabba33ab..f0743d30e79 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; diff --git a/tests/help.rs b/tests/help.rs index 00e14f6805e..09737d26f17 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 @@ -514,4 +513,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 +} From 84d8c5476de95b7f37d61888bc4f13688b712434 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 28 Jan 2017 11:00:52 +0100 Subject: [PATCH 2/4] fix: actually show character in debug output The &help[j..j] string slice was empty so nothing was shown. --- src/app/help.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/help.rs b/src/app/help.rs index f0743d30e79..edb1560b5eb 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -957,7 +957,7 @@ 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'); } From 563a539a8ee99afd51bc381dfb1be09761315aaa Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sun, 29 Jan 2017 20:54:47 +0100 Subject: [PATCH 3/4] tests: show how final word is not always wrapped This is #828. --- tests/help.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/help.rs b/tests/help.rs index 09737d26f17..859f5ef9626 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -148,6 +148,19 @@ 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: @@ -409,6 +422,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") From 564c5f0f1730f4a2c1cdd128664f1a981c31dcd4 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sun, 29 Jan 2017 11:46:23 +0100 Subject: [PATCH 4/4] fix: allow final word to be wrapped in wrap_help 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 #828. --- src/app/help.rs | 13 +++++++++++++ tests/help.rs | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/app/help.rs b/src/app/help.rs index edb1560b5eb..ad677756367 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -960,8 +960,21 @@ fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) { 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 859f5ef9626..0393f057717 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -159,7 +159,8 @@ FLAGS: information -V, --version Prints - version information"; + version + information"; static OLD_NEWLINE_CHARS: &'static str = "ctest 0.1