1
1
use std:: fmt:: { Display , Formatter } ;
2
+ use std:: io:: StdoutLock ;
2
3
3
4
#[ derive( Clone ) ]
4
5
enum Usage {
@@ -519,26 +520,7 @@ pub fn show_progress() -> anyhow::Result<()> {
519
520
write ! ( stdout, "{icon} {config: <50}: " , icon = usage. icon( ) ) ?;
520
521
521
522
if let Some ( width) = width {
522
- let icon_and_config_width = 55 ;
523
- let width_after_config = width - icon_and_config_width;
524
- let usage = usage. to_string ( ) ;
525
- let mut idx = 0 ;
526
- for word in usage. split ( ' ' ) {
527
- // +1 for the space after each word
528
- let word_len = word. chars ( ) . count ( ) + 1 ;
529
-
530
- if idx + word_len > width_after_config {
531
- writeln ! ( stdout) ?;
532
- for _ in 0 ..icon_and_config_width {
533
- write ! ( stdout, " " ) ?;
534
- }
535
- idx = 0 ;
536
- }
537
-
538
- write ! ( stdout, "{word} " ) ?;
539
- idx += word_len;
540
- }
541
- writeln ! ( stdout) ?;
523
+ write_with_linewrap ( & mut stdout, & usage. to_string ( ) , width) ?;
542
524
} else {
543
525
writeln ! ( stdout, "{usage}" ) ?;
544
526
}
@@ -554,3 +536,27 @@ pub fn show_progress() -> anyhow::Result<()> {
554
536
) ;
555
537
Ok ( ( ) )
556
538
}
539
+
540
+ fn write_with_linewrap ( stdout : & mut StdoutLock < ' _ > , text : & str , width : usize ) -> Result < ( ) , std:: io:: Error > {
541
+ use std:: io:: Write ;
542
+ let icon_and_config_width = 55 ;
543
+ let width_after_config = width. saturating_sub ( icon_and_config_width) ;
544
+ let mut idx = 0 ;
545
+ for word in text. split ( ' ' ) {
546
+ // +1 for the space after each word
547
+ let word_len = word. chars ( ) . count ( ) + 1 ;
548
+
549
+ if idx + word_len > width_after_config {
550
+ writeln ! ( stdout) ?;
551
+ for _ in 0 ..icon_and_config_width {
552
+ write ! ( stdout, " " ) ?;
553
+ }
554
+ idx = 0 ;
555
+ }
556
+
557
+ write ! ( stdout, "{word} " ) ?;
558
+ idx += word_len;
559
+ }
560
+ writeln ! ( stdout) ?;
561
+ Ok ( ( ) )
562
+ }
0 commit comments