Skip to content

Commit 304afd5

Browse files
committed
refactor: don't pass too many args to a function
1 parent 4dfee37 commit 304afd5

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use std::process;
2+
use std::{process, sync::Arc};
33

44
use anyhow::Result;
55
use zman::{
@@ -16,9 +16,9 @@ fn main() {
1616
}
1717

1818
fn run() -> Result<()> {
19-
let opts = Opts::parse();
19+
let opts = Arc::new(Opts::parse());
2020

21-
let mut printer = Printer::new(opts.width, &opts.full_bar, &opts.rest_bar, opts.json);
21+
let mut printer = Printer::new(Arc::clone(&opts));
2222
match opts.period {
2323
Period::Year => {
2424
let ratio = progress::year()?;

src/output.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
use std::io::{self, Write};
1+
use std::{
2+
io::{self, Write},
3+
sync::Arc,
4+
};
25

36
use owo_colors::OwoColorize;
47

8+
use crate::cli::Opts;
9+
510
pub struct Printer {
11+
opts: Arc<Opts>,
612
ratio: f64,
7-
width: i32,
8-
full_bar: String,
9-
rest_bar: String,
10-
json_format: bool,
1113
/// only used in status bar
1214
ratio_char: String,
1315
}
1416

1517
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 {
1719
Self {
20+
opts,
1821
ratio: 0.0,
19-
width,
20-
full_bar: full_bar.to_string(),
21-
rest_bar: rest_bar.to_string(),
22-
json_format,
2322
ratio_char: "y".to_string(),
2423
}
2524
}
@@ -34,13 +33,13 @@ impl Printer {
3433
/// Show progress-bar
3534
pub fn print(&self) {
3635
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;
3938

4039
let mut progress_fmt = format!(
4140
"{}{} {}%",
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),
4443
ratio_int
4544
);
4645
let state = {
@@ -51,11 +50,11 @@ impl Printer {
5150
}
5251
};
5352
// color
54-
if state == "Critical" && !self.json_format {
53+
if state == "Critical" && !self.opts.json {
5554
progress_fmt = format!("{}", progress_fmt.red());
5655
}
5756
// JSON
58-
if self.json_format {
57+
if self.opts.json {
5958
progress_fmt = format!(
6059
r#"{{"icon": "{}", "state": "{}", "text": "{}: {}"}}"#,
6160
"zman", state, self.ratio_char, progress_fmt

0 commit comments

Comments
 (0)