diff --git a/Cargo.toml b/Cargo.toml index 8381bcd3af0..43671d2ffcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ strsim = { version = "~0.5.1", optional = true } yaml-rust = { version = "~0.3.2", optional = true } clippy = { version = "~0.0.79", optional = true } unicode-width = { version = "~0.1.3", optional = true } +unicode-segmentation = { version = "~0.1.2", optional = true } term_size = { version = "~0.1.0", optional = true } [dev-dependencies] @@ -30,7 +31,7 @@ default = ["suggestions", "color", "wrap_help"] suggestions = ["strsim"] color = ["ansi_term", "libc"] yaml = ["yaml-rust"] -wrap_help = ["libc", "unicode-width", "term_size"] +wrap_help = ["libc", "unicode-width", "term_size", "unicode-segmentation"] lints = ["clippy", "nightly"] nightly = [] # for building with nightly and unstable features unstable = [] # for building with unstable features on stable Rust diff --git a/src/app/help.rs b/src/app/help.rs index 66d850657b1..f303cad5e9d 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -5,6 +5,7 @@ use std::cmp; use std::usize; use vec_map::VecMap; +use unicode_segmentation::UnicodeSegmentation; use errors::{Error, Result as ClapResult}; @@ -463,33 +464,27 @@ impl<'a> Help<'a> { debug!("Enough space to wrap..."); if longest_w < avail_chars { sdebugln!("Yes"); - let mut indices = vec![]; - let mut idx = 0; - loop { - idx += avail_chars - 1; - if idx >= help.len() { - break; + let mut prev_space = 0; + let mut j = 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]) < avail_chars { + debugln!("Still enough space..."); + prev_space = idx; + continue; } - // 'a' arbitrary non space char - if help.chars().nth(idx).unwrap_or('a') != ' ' { - idx = find_idx_of_space(&*help, idx); - } - debugln!("Adding idx: {}", idx); - debugln!("At {}: {:?}", idx, help.chars().nth(idx)); - indices.push(idx); - if str_width(&help[idx..]) <= avail_chars { - break; - } - } - for (i, idx) in indices.iter().enumerate() { - debugln!("iter;i={},idx={}", i, idx); - let j = idx + (2 * i); + debugln!("Adding Newline..."); + j = prev_space + (2 * i); + debugln!("i={},prev_space={},j={}", i, prev_space, j); debugln!("removing: {}", j); - debugln!("at {}: {:?}", j, help.chars().nth(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; } } else { sdebugln!("No"); diff --git a/src/lib.rs b/src/lib.rs index 5b64b6ed411..20ad117c266 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -416,6 +416,8 @@ extern crate bitflags; extern crate vec_map; #[cfg(feature = "wrap_help")] extern crate term_size; +#[cfg(feature = "wrap_help")] +extern crate unicode_segmentation; #[cfg(feature = "yaml")] pub use yaml_rust::YamlLoader;