mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
fix(Unicode): fixes two bugs where non-English characters were stripped or caused a panic with help wrapping
Closes #626
This commit is contained in:
parent
881a647ee9
commit
763a5c920e
3 changed files with 20 additions and 22 deletions
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue