1
+ use parking_lot:: Mutex ;
1
2
use std:: cmp;
2
3
use std:: env;
4
+ use std:: sync:: Arc ;
3
5
use std:: time:: { Duration , Instant } ;
4
6
5
7
use crate :: core:: shell:: Verbosity ;
8
+ use crate :: core:: Shell ;
6
9
use crate :: util:: config:: ProgressWhen ;
7
10
use crate :: util:: { CargoResult , Config } ;
8
11
use cargo_util:: is_ci;
9
12
use unicode_width:: UnicodeWidthChar ;
10
13
11
- pub struct Progress < ' cfg > {
12
- state : Option < State < ' cfg > > ,
14
+ pub struct Progress {
15
+ state : Option < State > ,
13
16
}
14
17
15
18
pub enum ProgressStyle {
@@ -23,8 +26,8 @@ struct Throttle {
23
26
last_update : Instant ,
24
27
}
25
28
26
- struct State < ' cfg > {
27
- config : & ' cfg Config ,
29
+ struct State {
30
+ shell : Arc < Mutex < Shell > > ,
28
31
format : Format ,
29
32
name : String ,
30
33
done : bool ,
@@ -39,8 +42,8 @@ struct Format {
39
42
max_print : usize ,
40
43
}
41
44
42
- impl < ' cfg > Progress < ' cfg > {
43
- pub fn with_style ( name : & str , style : ProgressStyle , cfg : & ' cfg Config ) -> Progress < ' cfg > {
45
+ impl Progress {
46
+ pub fn with_style ( name : & str , style : ProgressStyle , cfg : & Config ) -> Progress {
44
47
// report no progress when -q (for quiet) or TERM=dumb are set
45
48
// or if running on Continuous Integration service like Travis where the
46
49
// output logs get mangled.
@@ -60,15 +63,15 @@ impl<'cfg> Progress<'cfg> {
60
63
Progress :: new_priv ( name, style, cfg)
61
64
}
62
65
63
- fn new_priv ( name : & str , style : ProgressStyle , cfg : & ' cfg Config ) -> Progress < ' cfg > {
66
+ fn new_priv ( name : & str , style : ProgressStyle , cfg : & Config ) -> Progress {
64
67
let progress_config = cfg. progress_config ( ) ;
65
68
let width = progress_config
66
69
. width
67
70
. or_else ( || cfg. shell ( ) . err_width ( ) . progress_max_width ( ) ) ;
68
71
69
72
Progress {
70
73
state : width. map ( |n| State {
71
- config : cfg,
74
+ shell : cfg. shell_detached ( ) ,
72
75
format : Format {
73
76
style,
74
77
max_width : n,
@@ -93,7 +96,7 @@ impl<'cfg> Progress<'cfg> {
93
96
self . state . is_some ( )
94
97
}
95
98
96
- pub fn new ( name : & str , cfg : & ' cfg Config ) -> Progress < ' cfg > {
99
+ pub fn new ( name : & str , cfg : & Config ) -> Progress {
97
100
Self :: with_style ( name, ProgressStyle :: Percentage , cfg)
98
101
}
99
102
@@ -180,7 +183,7 @@ impl Throttle {
180
183
}
181
184
}
182
185
183
- impl < ' cfg > State < ' cfg > {
186
+ impl State {
184
187
fn tick ( & mut self , cur : usize , max : usize , msg : & str ) -> CargoResult < ( ) > {
185
188
if self . done {
186
189
return Ok ( ( ) ) ;
@@ -215,29 +218,32 @@ impl<'cfg> State<'cfg> {
215
218
}
216
219
217
220
// Only update if the line has changed.
218
- if self . config . shell ( ) . is_cleared ( ) || self . last_line . as_ref ( ) != Some ( & line) {
219
- let mut shell = self . config . shell ( ) ;
220
- shell. set_needs_clear ( false ) ;
221
- shell. status_header ( & self . name ) ?;
222
- write ! ( shell. err( ) , "{}\r " , line) ?;
223
- self . last_line = Some ( line) ;
224
- shell. set_needs_clear ( true ) ;
221
+ {
222
+ let mut shell = self . shell . lock ( ) ;
223
+ if shell. is_cleared ( ) || self . last_line . as_ref ( ) != Some ( & line) {
224
+ shell. set_needs_clear ( false ) ;
225
+ shell. status_header ( & self . name ) ?;
226
+ write ! ( shell. err( ) , "{}\r " , line) ?;
227
+ self . last_line = Some ( line) ;
228
+ shell. set_needs_clear ( true ) ;
229
+ }
225
230
}
226
231
227
232
Ok ( ( ) )
228
233
}
229
234
230
235
fn clear ( & mut self ) {
231
236
// No need to clear if the progress is not currently being displayed.
232
- if self . last_line . is_some ( ) && !self . config . shell ( ) . is_cleared ( ) {
233
- self . config . shell ( ) . err_erase_line ( ) ;
237
+ let mut shell = self . shell . lock ( ) ;
238
+ if self . last_line . is_some ( ) && !shell. is_cleared ( ) {
239
+ shell. err_erase_line ( ) ;
234
240
self . last_line = None ;
235
241
}
236
242
}
237
243
238
244
fn try_update_max_width ( & mut self ) {
239
245
if self . fixed_width . is_none ( ) {
240
- if let Some ( n) = self . config . shell ( ) . err_width ( ) . progress_max_width ( ) {
246
+ if let Some ( n) = self . shell . lock ( ) . err_width ( ) . progress_max_width ( ) {
241
247
self . format . max_width = n;
242
248
}
243
249
}
@@ -323,7 +329,7 @@ impl Format {
323
329
}
324
330
}
325
331
326
- impl < ' cfg > Drop for State < ' cfg > {
332
+ impl Drop for State {
327
333
fn drop ( & mut self ) {
328
334
self . clear ( ) ;
329
335
}
0 commit comments