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

Issue 617 #650

Merged
merged 6 commits into from
Sep 7, 2016
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
52 changes: 35 additions & 17 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ impl<'a> Help<'a> {
}
lw
};
help = if help.contains("{n}") {
help.replace("{n}", "\n")
} else {
help
};
wrap_help(&mut help, longest_w, width);
} else {
sdebugln!("No");
Expand All @@ -375,7 +380,6 @@ impl<'a> Help<'a> {
help.push_str(h);
&*help
};

if help.contains("{n}") {
if let Some(part) = help.split("{n}").next() {
try!(write!(self.writer, "{}", part));
Expand Down Expand Up @@ -446,6 +450,11 @@ impl<'a> Help<'a> {
}
lw
};
help = if help.contains("{n}") {
help.replace("{n}", "\n")
} else {
help
};
wrap_help(&mut help, longest_w, avail_chars);
} else {
sdebugln!("No");
Expand All @@ -459,12 +468,11 @@ impl<'a> Help<'a> {
help.push_str(&*spec_vals);
&*help
};

if help.contains("{n}") {
if let Some(part) = help.split("{n}").next() {
if help.contains("\n") {
if let Some(part) = help.split("\n").next() {
try!(write!(self.writer, "{}", part));
}
for part in help.split("{n}").skip(1) {
for part in help.split("\n").skip(1) {
try!(write!(self.writer, "\n"));
if nlh || force_next_line {
try!(write!(self.writer, "{}{}{}", TAB, TAB, TAB));
Expand Down Expand Up @@ -922,27 +930,37 @@ fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) {
sdebugln!("Yes");
let mut prev_space = 0;
let mut j = 0;
let mut i = 0;
// let mut i = 0;
for (idx, g) in (&*help.clone()).grapheme_indices(true) {
debugln!("iter;idx={},g={}", idx, g);
if g != " " {
continue;
}
if str_width(&help[j..idx + (2 * i)]) < avail_chars {
debugln!("Still enough space...");
if g == "\n" {
debugln!("Newline found...");
debugln!("Still space...{:?}", str_width(&help[j..idx]) < avail_chars);
if str_width(&help[j..idx]) < avail_chars {
// i += 1;
j = idx; // + i;
continue;
}
} else if g != " " {
if idx != help.len() - 1 || str_width(&help[j..idx]) < avail_chars {
continue;
}
debugln!("Reached the end of the line and we're over...");
} else if str_width(&help[j..idx]) < avail_chars { //(2 * i)]) < avail_chars {
debugln!("Space found with room...");
prev_space = idx;
continue;
}
debugln!("Adding Newline...");
j = prev_space + (2 * i);
debugln!("i={},prev_space={},j={}", i, prev_space, j);
j = prev_space; // + i;//(2 * i);
debugln!("prev_space={},j={}", prev_space, j);
debugln!("removing: {}", j);
debugln!("char at {}: {}", j, &help[j..j]);
help.remove(j);
help.insert(j, '{');
help.insert(j + 1, 'n');
help.insert(j + 2, '}');
i += 1;
help.insert(j, '\n');
// help.insert(j + 1, 'n');
// help.insert(j + 2, '}');
// i += 1;
}
} else {
sdebugln!("No");
Expand Down
8 changes: 3 additions & 5 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,7 @@ impl<'a, 'b> Arg<'a, 'b> {
///
/// Any valid UTF-8 is allowed in the help text. The one exception is when one wishes to
/// include a newline in the help text and have the following text be properly aligned with all
/// the other help text. To include a newline **and** be properly aligned with all other
/// arguments help text, it must be specified via a tag inside curly braces, like so `{n}`.
/// Using the standard `\n` will produce a newline, but it will not be properly aligned.
/// the other help text.
///
/// ```rust
/// # use clap::{App, Arg};
Expand Down Expand Up @@ -2173,8 +2171,8 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .short("o")
/// .takes_value(true)
/// .value_names(&["value1", "value2"])
/// .help("Some really long help and complex{n}\
/// help that makes more sense to be{n}\
/// .help("Some really long help and complex\n\
/// help that makes more sense to be\n\
/// on a line after the option")
/// .next_line_help(true))
/// .get_matches_from(vec![
Expand Down
23 changes: 11 additions & 12 deletions tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ FLAGS:
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café is an
establishment which primarily serves hot
coffee, related coffee beverages (e.g.,
café 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.";
coffee, related coffee beverages (e.g., café
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.";

static ISSUE_626_PANIC: &'static str = "ctest 0.1

Expand All @@ -114,11 +114,10 @@ OPTIONS:
-c, --cafe <FILE>
La culture du café est très développée
dans de nombreux pays à climat chaud
d\'Amérique, d\'Afrique et d\'Asie,
dans des plantations qui sont
cultivées pour les marchés
d\'exportation. Le café est souvent
une contribution majeure aux
d\'Amérique, d\'Afrique et d\'Asie, dans
des plantations qui sont cultivées pour
les marchés d\'exportation. Le café est
souvent une contribution majeure aux
exportations des régions productrices.";

#[test]
Expand Down