@@ -5,6 +5,7 @@ use std::cmp;
5
5
use std:: usize;
6
6
7
7
use vec_map:: VecMap ;
8
+ use unicode_segmentation:: UnicodeSegmentation ;
8
9
9
10
use errors:: { Error , Result as ClapResult } ;
10
11
@@ -463,33 +464,27 @@ impl<'a> Help<'a> {
463
464
debug ! ( "Enough space to wrap..." ) ;
464
465
if longest_w < avail_chars {
465
466
sdebugln ! ( "Yes" ) ;
466
- let mut indices = vec ! [ ] ;
467
- let mut idx = 0 ;
468
- loop {
469
- idx += avail_chars - 1 ;
470
- if idx >= help. len ( ) {
471
- break ;
467
+ let mut prev_space = 0 ;
468
+ let mut j = 0 ;
469
+ let mut i = 0 ;
470
+ for ( idx, g) in ( & * help. clone ( ) ) . grapheme_indices ( true ) {
471
+ debugln ! ( "iter;idx={},g={}" , idx, g) ;
472
+ if g != " " { continue ; }
473
+ if str_width ( & help[ j..idx] ) < avail_chars {
474
+ debugln ! ( "Still enough space..." ) ;
475
+ prev_space = idx;
476
+ continue ;
472
477
}
473
- // 'a' arbitrary non space char
474
- if help. chars ( ) . nth ( idx) . unwrap_or ( 'a' ) != ' ' {
475
- idx = find_idx_of_space ( & * help, idx) ;
476
- }
477
- debugln ! ( "Adding idx: {}" , idx) ;
478
- debugln ! ( "At {}: {:?}" , idx, help. chars( ) . nth( idx) ) ;
479
- indices. push ( idx) ;
480
- if str_width ( & help[ idx..] ) <= avail_chars {
481
- break ;
482
- }
483
- }
484
- for ( i, idx) in indices. iter ( ) . enumerate ( ) {
485
- debugln ! ( "iter;i={},idx={}" , i, idx) ;
486
- let j = idx + ( 2 * i) ;
478
+ debugln ! ( "Adding Newline..." ) ;
479
+ j = prev_space + ( 2 * i) ;
480
+ debugln ! ( "i={},prev_space={},j={}" , i, prev_space, j) ;
487
481
debugln ! ( "removing: {}" , j) ;
488
- debugln ! ( "at {}: {:? }" , j, help. chars ( ) . nth ( j ) ) ;
482
+ debugln ! ( "char at {}: {}" , j, & help[ j..j ] ) ;
489
483
help. remove ( j) ;
490
484
help. insert ( j, '{' ) ;
491
485
help. insert ( j + 1 , 'n' ) ;
492
486
help. insert ( j + 2 , '}' ) ;
487
+ i += 1 ;
493
488
}
494
489
} else {
495
490
sdebugln ! ( "No" ) ;
0 commit comments