File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -8,17 +8,23 @@ use tokio::time::interval;
8
8
pub struct Spinner {
9
9
is_active : Arc < AtomicBool > ,
10
10
message : String ,
11
+ disabled : bool ,
11
12
}
12
13
13
14
impl Spinner {
14
15
pub fn new ( message : & str ) -> Self {
16
+ let disabled = !atty:: is ( atty:: Stream :: Stdout ) ;
15
17
Self {
16
18
is_active : Arc :: new ( AtomicBool :: new ( false ) ) ,
17
19
message : message. to_string ( ) ,
20
+ disabled,
18
21
}
19
22
}
20
23
21
24
pub async fn start ( & self ) {
25
+ if self . disabled {
26
+ return ;
27
+ }
22
28
self . is_active . store ( true , Ordering :: Relaxed ) ;
23
29
let is_active = Arc :: clone ( & self . is_active ) ;
24
30
let message = self . message . clone ( ) ;
@@ -50,13 +56,20 @@ impl Spinner {
50
56
}
51
57
52
58
pub fn stop ( & self ) {
59
+ if self . disabled {
60
+ return ;
61
+ }
53
62
self . is_active . store ( false , Ordering :: Relaxed ) ;
54
63
// Clear the line
55
64
print ! ( "\r \x1b [K" ) ;
56
65
io:: stdout ( ) . flush ( ) . unwrap_or ( ( ) ) ;
57
66
}
58
67
59
68
pub fn stop_and_replace ( & self , replacement : & str ) {
69
+ if self . disabled {
70
+ println ! ( "{replacement}" ) ;
71
+ return ;
72
+ }
60
73
self . is_active . store ( false , Ordering :: Relaxed ) ;
61
74
// Clear the line and print replacement
62
75
print ! ( "\r \x1b [K{replacement}" ) ;
You can’t perform that action at this time.
0 commit comments