File tree 3 files changed +55
-6
lines changed
3 files changed +55
-6
lines changed Original file line number Diff line number Diff line change @@ -496,11 +496,15 @@ impl DrawState {
496
496
let len = self . lines . len ( ) ;
497
497
let mut real_len = 0 ;
498
498
for ( idx, line) in self . lines . iter ( ) . enumerate ( ) {
499
- // Calculate real length based on terminal width
500
- // This take in account linewrap from terminal
501
- real_len +=
502
- ( console:: measure_text_width ( line) as f64 / term. width ( ) as f64 ) . ceil ( ) as usize ;
503
-
499
+ if line. is_empty ( ) {
500
+ // Empty line are new line
501
+ real_len += 1 ;
502
+ } else {
503
+ // Calculate real length based on terminal width
504
+ // This take in account linewrap from terminal
505
+ real_len += ( console:: measure_text_width ( line) as f64 / term. width ( ) as f64 ) . ceil ( )
506
+ as usize ;
507
+ }
504
508
if idx + 1 != len {
505
509
term. write_line ( line) ?;
506
510
} else {
Original file line number Diff line number Diff line change @@ -143,7 +143,13 @@ impl BarState {
143
143
} ;
144
144
145
145
let mut draw_state = drawable. state ( ) ;
146
- draw_state. lines . extend ( msg. lines ( ) . map ( Into :: into) ) ;
146
+ let lines: Vec < String > = msg. lines ( ) . map ( Into :: into) . collect ( ) ;
147
+ // Empty msg should trigger newline as we are in println
148
+ if lines. is_empty ( ) {
149
+ draw_state. lines . push ( String :: new ( ) ) ;
150
+ } else {
151
+ draw_state. lines . extend ( lines) ;
152
+ }
147
153
draw_state. orphan_lines_count = draw_state. lines . len ( ) ;
148
154
if !matches ! ( self . state. status, Status :: DoneHidden ) {
149
155
self . style
Original file line number Diff line number Diff line change @@ -1044,3 +1044,42 @@ n terminal width"#
1044
1044
. trim( )
1045
1045
) ;
1046
1046
}
1047
+
1048
+ #[ test]
1049
+ fn basic_progress_bar_newline ( ) {
1050
+ let in_mem = InMemoryTerm :: new ( 10 , 80 ) ;
1051
+ let pb = ProgressBar :: with_draw_target (
1052
+ Some ( 10 ) ,
1053
+ ProgressDrawTarget :: term_like ( Box :: new ( in_mem. clone ( ) ) ) ,
1054
+ ) ;
1055
+
1056
+ assert_eq ! ( in_mem. contents( ) , String :: new( ) ) ;
1057
+
1058
+ pb. println ( "\n hello" ) ;
1059
+ pb. tick ( ) ;
1060
+ assert_eq ! (
1061
+ in_mem. contents( ) ,
1062
+ r#"
1063
+ hello
1064
+ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10"#
1065
+ ) ;
1066
+
1067
+ pb. inc ( 1 ) ;
1068
+ pb. println ( "" ) ;
1069
+ assert_eq ! (
1070
+ in_mem. contents( ) ,
1071
+ r#"
1072
+ hello
1073
+
1074
+ ███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/10"#
1075
+ ) ;
1076
+
1077
+ pb. finish ( ) ;
1078
+ assert_eq ! (
1079
+ in_mem. contents( ) ,
1080
+ "
1081
+ hello
1082
+
1083
+ ██████████████████████████████████████████████████████████████████████████ 10/10"
1084
+ ) ;
1085
+ }
You can’t perform that action at this time.
0 commit comments