Skip to content

Commit

Permalink
fix(Unicode): fixes two bugs where non-English characters were stripp…
Browse files Browse the repository at this point in the history
…ed or caused a panic with help wrapping

Closes #626
  • Loading branch information
kbknapp committed Aug 25, 2016
1 parent 881a647 commit 763a5c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
37 changes: 16 additions & 21 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::cmp;
use std::usize;

use vec_map::VecMap;
use unicode_segmentation::UnicodeSegmentation;

use errors::{Error, Result as ClapResult};

Expand Down Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 763a5c9

Please sign in to comment.