Skip to content

Commit d0b442c

Browse files
committed
fix(Help Wrapping): fixes a bug where help is wrapped incorrectly and causing a panic with some non-English characters
Closes #626
1 parent bd704e0 commit d0b442c

File tree

1 file changed

+33
-86
lines changed

1 file changed

+33
-86
lines changed

src/app/help.rs

+33-86
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod term_size {
2525

2626
use unicode_width::UnicodeWidthStr;
2727

28-
use strext::_StrExt;
28+
// use strext::_StrExt;
2929

3030
fn str_width(s: &str) -> usize {
3131
UnicodeWidthStr::width(s)
@@ -336,7 +336,6 @@ impl<'a> Help<'a> {
336336
let width = self.term_w;
337337
debugln!("Term width...{}", width);
338338
let too_long = str_width(h) >= width;
339-
debugln!("Too long...{:?}", too_long);
340339

341340
debug!("Too long...");
342341
if too_long {
@@ -355,41 +354,7 @@ impl<'a> Help<'a> {
355354
}
356355
lw
357356
};
358-
debugln!("Longest word...{}", longest_w);
359-
debug!("Enough space to wrap...");
360-
if longest_w < width {
361-
sdebugln!("Yes");
362-
let mut indices = vec![];
363-
let mut idx = 0;
364-
loop {
365-
idx += width - 1;
366-
if idx >= help.len() {
367-
break;
368-
}
369-
// 'a' arbitrary non space char
370-
if help.chars().nth(idx).unwrap_or('a') != ' ' {
371-
idx = find_idx_of_space(&*help, idx);
372-
}
373-
debugln!("Adding idx: {}", idx);
374-
debugln!("At {}: {:?}", idx, help.chars().nth(idx));
375-
indices.push(idx);
376-
if str_width(&help[idx..]) <= width {
377-
break;
378-
}
379-
}
380-
for (i, idx) in indices.iter().enumerate() {
381-
debugln!("iter;i={},idx={}", i, idx);
382-
let j = idx + (2 * i);
383-
debugln!("removing: {}", j);
384-
debugln!("at {}: {:?}", j, help.chars().nth(j));
385-
help.remove(j);
386-
help.insert(j, '{');
387-
help.insert(j + 1, 'n');
388-
help.insert(j + 2, '}');
389-
}
390-
} else {
391-
sdebugln!("No");
392-
}
357+
wrap_help(&mut help, longest_w, width);
393358
} else {
394359
sdebugln!("No");
395360
}
@@ -427,15 +392,15 @@ impl<'a> Help<'a> {
427392
let width = self.term_w;
428393
debugln!("Term width...{}", width);
429394
let too_long = spcs + str_width(h) + str_width(&*spec_vals) >= width;
430-
debugln!("Too long...{:?}", too_long);
395+
debugln!("Spaces: {}", spcs);
431396

432397
// Is help on next line, if so newline + 2x tab
433398
if self.next_line_help || arg.is_set(ArgSettings::NextLineHelp) {
434399
try!(write!(self.writer, "\n{}{}", TAB, TAB));
435400
}
436401

437402
debug!("Too long...");
438-
if too_long {
403+
if too_long && spcs <= width {
439404
sdebugln!("Yes");
440405
help.push_str(h);
441406
help.push_str(&*spec_vals);
@@ -453,35 +418,7 @@ impl<'a> Help<'a> {
453418
}
454419
lw
455420
};
456-
debugln!("Longest word...{}", longest_w);
457-
debug!("Enough space to wrap...");
458-
if longest_w < avail_chars {
459-
sdebugln!("Yes");
460-
let mut prev_space = 0;
461-
let mut j = 0;
462-
let mut i = 0;
463-
for (idx, g) in (&*help.clone()).grapheme_indices(true) {
464-
debugln!("iter;idx={},g={}", idx, g);
465-
if g != " " { continue; }
466-
if str_width(&help[j..idx]) < avail_chars {
467-
debugln!("Still enough space...");
468-
prev_space = idx;
469-
continue;
470-
}
471-
debugln!("Adding Newline...");
472-
j = prev_space + (2 * i);
473-
debugln!("i={},prev_space={},j={}", i, prev_space, j);
474-
debugln!("removing: {}", j);
475-
debugln!("char at {}: {}", j, &help[j..j]);
476-
help.remove(j);
477-
help.insert(j, '{');
478-
help.insert(j + 1, 'n');
479-
help.insert(j + 2, '}');
480-
i += 1;
481-
}
482-
} else {
483-
sdebugln!("No");
484-
}
421+
wrap_help(&mut help, longest_w, avail_chars);
485422
} else {
486423
sdebugln!("No");
487424
}
@@ -946,24 +883,34 @@ impl<'a> Help<'a> {
946883
}
947884
}
948885

949-
950-
fn find_idx_of_space(full: &str, mut start: usize) -> usize {
951-
debugln!("fn=find_idx_of_space;");
952-
let haystack = if full._is_char_boundary(start) {
953-
&full[..start]
954-
} else {
955-
while !full._is_char_boundary(start) {
956-
start -= 1;
957-
}
958-
&full[..start]
959-
};
960-
debugln!("haystack: {}", haystack);
961-
for (i, c) in haystack.chars().rev().enumerate() {
962-
debugln!("iter;c={},i={}", c, i);
963-
if c == ' ' {
964-
debugln!("Found space returning start-i...{}", start - (i + 1));
965-
return start - (i + 1);
886+
fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) {
887+
debugln!("fn=wrap_help;longest_w={},avail_chars={}", longest_w, avail_chars);
888+
debug!("Enough space to wrap...");
889+
if longest_w < avail_chars {
890+
sdebugln!("Yes");
891+
let mut prev_space = 0;
892+
let mut j = 0;
893+
let mut i = 0;
894+
for (idx, g) in (&*help.clone()).grapheme_indices(true) {
895+
debugln!("iter;idx={},g={}", idx, g);
896+
if g != " " { continue; }
897+
if str_width(&help[j..idx + (2 * i)]) < avail_chars {
898+
debugln!("Still enough space...");
899+
prev_space = idx;
900+
continue;
901+
}
902+
debugln!("Adding Newline...");
903+
j = prev_space + (2 * i);
904+
debugln!("i={},prev_space={},j={}", i, prev_space, j);
905+
debugln!("removing: {}", j);
906+
debugln!("char at {}: {}", j, &help[j..j]);
907+
help.remove(j);
908+
help.insert(j, '{');
909+
help.insert(j + 1, 'n');
910+
help.insert(j + 2, '}');
911+
i += 1;
966912
}
913+
} else {
914+
sdebugln!("No");
967915
}
968-
0
969916
}

0 commit comments

Comments
 (0)