1
- use std:: io:: { self , Write } ;
1
+ use std:: {
2
+ io:: { self , Write } ,
3
+ sync:: Arc ,
4
+ } ;
2
5
3
6
use owo_colors:: OwoColorize ;
4
7
8
+ use crate :: cli:: Opts ;
9
+
5
10
pub struct Printer {
11
+ opts : Arc < Opts > ,
6
12
ratio : f64 ,
7
- width : i32 ,
8
- full_bar : String ,
9
- rest_bar : String ,
10
- json_format : bool ,
11
13
/// only used in status bar
12
14
ratio_char : String ,
13
15
}
14
16
15
17
impl Printer {
16
- pub fn new ( width : i32 , full_bar : & str , rest_bar : & str , json_format : bool ) -> Self {
18
+ pub fn new ( opts : Arc < Opts > ) -> Self {
17
19
Self {
20
+ opts,
18
21
ratio : 0.0 ,
19
- width,
20
- full_bar : full_bar. to_string ( ) ,
21
- rest_bar : rest_bar. to_string ( ) ,
22
- json_format,
23
22
ratio_char : "y" . to_string ( ) ,
24
23
}
25
24
}
@@ -34,13 +33,13 @@ impl Printer {
34
33
/// Show progress-bar
35
34
pub fn print ( & self ) {
36
35
let ratio_int = ( self . ratio * 100.0 ) as i32 ;
37
- let progress_int = ( self . ratio * f64:: from ( self . width ) ) . round ( ) as i32 ;
38
- let rest_int = self . width - progress_int;
36
+ let progress_int = ( self . ratio * f64:: from ( self . opts . width ) ) . round ( ) as i32 ;
37
+ let rest_int = self . opts . width - progress_int;
39
38
40
39
let mut progress_fmt = format ! (
41
40
"{}{} {}%" ,
42
- self . full_bar. repeat( progress_int as usize ) ,
43
- self . rest_bar. repeat( rest_int as usize ) ,
41
+ self . opts . full_bar. repeat( progress_int as usize ) ,
42
+ self . opts . rest_bar. repeat( rest_int as usize ) ,
44
43
ratio_int
45
44
) ;
46
45
let state = {
@@ -51,11 +50,11 @@ impl Printer {
51
50
}
52
51
} ;
53
52
// color
54
- if state == "Critical" && !self . json_format {
53
+ if state == "Critical" && !self . opts . json {
55
54
progress_fmt = format ! ( "{}" , progress_fmt. red( ) ) ;
56
55
}
57
56
// JSON
58
- if self . json_format {
57
+ if self . opts . json {
59
58
progress_fmt = format ! (
60
59
r#"{{"icon": "{}", "state": "{}", "text": "{}: {}"}}"# ,
61
60
"zman" , state, self . ratio_char, progress_fmt
0 commit comments