@@ -25,18 +25,6 @@ mod term_size {
25
25
pub fn dimensions ( ) -> Option < ( usize , usize ) > { None }
26
26
}
27
27
28
- macro_rules! find_longest {
29
- ( $help: expr) => { {
30
- let mut lw = 0 ;
31
- for l in $help. split( ' ' ) . map( |s| str_width( s) ) {
32
- if l > lw {
33
- lw = l;
34
- }
35
- }
36
- lw
37
- } } ;
38
- }
39
-
40
28
fn str_width ( s : & str ) -> usize { UnicodeWidthStr :: width ( s) }
41
29
42
30
const TAB : & ' static str = " " ;
@@ -407,9 +395,8 @@ impl<'a> Help<'a> {
407
395
// Determine how many newlines we need to insert
408
396
debugln ! ( "Help::write_before_after_help: Usable space: {}" ,
409
397
self . term_w) ;
410
- let longest_w = find_longest ! ( help) ;
411
398
help = help. replace ( "{n}" , "\n " ) ;
412
- wrap_help ( & mut help, longest_w , self . term_w ) ;
399
+ wrap_help ( & mut help, self . term_w ) ;
413
400
} else {
414
401
sdebugln ! ( "No" ) ;
415
402
}
@@ -450,9 +437,8 @@ impl<'a> Help<'a> {
450
437
// Determine how many newlines we need to insert
451
438
let avail_chars = self . term_w - spcs;
452
439
debugln ! ( "Help::help: Usable space...{}" , avail_chars) ;
453
- let longest_w = find_longest ! ( help) ;
454
440
help = help. replace ( "{n}" , "\n " ) ;
455
- wrap_help ( & mut help, longest_w , avail_chars) ;
441
+ wrap_help ( & mut help, avail_chars) ;
456
442
} else {
457
443
sdebugln ! ( "No" ) ;
458
444
}
@@ -629,9 +615,8 @@ impl<'a> Help<'a> {
629
615
macro_rules! write_name {
630
616
( ) => { {
631
617
let mut name = parser. meta. name. clone( ) ;
632
- let longest_w = find_longest!( name) ;
633
618
name = name. replace( "{n}" , "\n " ) ;
634
- wrap_help( & mut name, longest_w , self . term_w) ;
619
+ wrap_help( & mut name, self . term_w) ;
635
620
try!( color!( self , & * name, good) ) ;
636
621
} } ;
637
622
}
@@ -659,9 +644,8 @@ impl<'a> Help<'a> {
659
644
macro_rules! write_thing {
660
645
( $thing: expr) => { {
661
646
let mut owned_thing = $thing. to_owned( ) ;
662
- let longest_w = find_longest!( owned_thing) ;
663
647
owned_thing = owned_thing. replace( "{n}" , "\n " ) ;
664
- wrap_help( & mut owned_thing, longest_w , self . term_w) ;
648
+ wrap_help( & mut owned_thing, self . term_w) ;
665
649
try!( write!( self . writer, "{}\n " , & * owned_thing) )
666
650
} } ;
667
651
}
@@ -923,15 +907,12 @@ impl<'a> Help<'a> {
923
907
}
924
908
}
925
909
926
- fn wrap_help ( help : & mut String , longest_w : usize , avail_chars : usize ) {
927
- // Keep previous behavior of not wrapping at all if one of the
928
- // words would overflow the line.
929
- if longest_w < avail_chars {
930
- * help = help. lines ( )
931
- . map ( |line| textwrap:: fill ( line, avail_chars) )
932
- . collect :: < Vec < String > > ( )
933
- . join ( "\n " ) ;
934
- }
910
+ fn wrap_help ( help : & mut String , avail_chars : usize ) {
911
+ let wrapper = textwrap:: Wrapper :: new ( avail_chars) . break_words ( false ) ;
912
+ * help = help. lines ( )
913
+ . map ( |line| wrapper. fill ( line) )
914
+ . collect :: < Vec < String > > ( )
915
+ . join ( "\n " ) ;
935
916
}
936
917
937
918
#[ cfg( test) ]
@@ -941,7 +922,7 @@ mod test {
941
922
#[ test]
942
923
fn wrap_help_last_word ( ) {
943
924
let mut help = String :: from ( "foo bar baz" ) ;
944
- wrap_help ( & mut help, 3 , 5 ) ;
925
+ wrap_help ( & mut help, 5 ) ;
945
926
assert_eq ! ( help, "foo\n bar\n baz" ) ;
946
927
}
947
928
}
0 commit comments