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

Fix wrapping bugs #832

Merged
merged 4 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
}
27 changes: 23 additions & 4 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -149,6 +148,20 @@ OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café.
-p, --pos <VAL> 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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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));
}
}