rpb 0.1.5

A simple implementation of progress bar for rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use crate::color::Colorizer;
use crate::iterator::BarIter;
use crate::spinner::Spinner;
use crate::styles::{Styles, Themes};
use crate::type_spinner::Spinners;
use crate::{format, type_spinner};
use std::fmt::{Debug, Formatter};
use std::io;
use std::time::Instant;

macro_rules! unit_fmt {
    ($n: ident) => {{
        let kilo_bytes = 1024_f64;
        match $n {
            $n if $n as f64 >= kilo_bytes.powf(4_f64) => {
                format!("{:.*} TB", 2, $n as f64 / kilo_bytes.powf(4_f64))
            }
            $n if $n as f64 >= kilo_bytes.powf(3_f64) => {
                format!("{:.*} GB", 2, $n as f64 / kilo_bytes.powf(3_f64))
            }
            $n if $n as f64 >= kilo_bytes.powf(2_f64) => {
                format!("{:.*} MB", 2, $n as f64 / kilo_bytes.powf(2_f64))
            }
            $n if $n as f64 >= kilo_bytes => format!("{:.*} KB", 2, $n as f64 / kilo_bytes),
            _ => format!("{:.*} B", 0, $n),
        }
    }};
}

#[derive(Clone)]
pub struct Bar {
    desc: String,
    state: State,
    option: Option,
    theme: Styles,
}

#[derive(Clone)]
struct State {
    percent: f64,
    current: i64,
    current_graph_rate: isize,
}

// Output type format, indicate which format wil be used in
// the speed box.
#[derive(Debug, Clone)]
pub enum Units {
    Default,
    Bytes,
}

#[derive(Clone)]
struct Option {
    total: i64,
    unit: Units,
    start_time: Instant,
    spinner: Spinner,
    front_colored: String,
    back_colored: String,
    position: u32,
}

impl State {
    fn new(max: i64) -> State {
        Self {
            current: 0,
            percent: get_percent(&0, &max),
            current_graph_rate: 0,
        }
    }
}

impl Option {
    fn new(total: i64, time: Instant) -> Option {
        Self {
            total,
            unit: Units::Default,
            start_time: time,
            spinner: Spinner::new(type_spinner::get_spinner(Spinners::Point)),
            front_colored: "".to_string(),
            back_colored: "".to_string(),
            position: 0,
        }
    }
}

impl Debug for Bar {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Bar").finish()
    }
}

/// Core implementation of console progress bar.
///
/// # Example
///
/// A simple progress bar with a max value.
///
/// ```rust
///
/// fn main() {
///     use rpb::bar::Bar;
///     let mut bar = Bar::new(100);
///
///     for _ in 0..100 {
///         bar.add(1);
///     }
/// }
/// ```
impl Bar {
    /// Create a new instance of [Bar] with a max value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rpb::bar::Bar;
    /// let mut bar = Bar::new(100);
    /// ```
    pub fn new(max: i64) -> Self {
        Self {
            desc: "".to_string(),
            state: State::new(max),
            option: Option::new(max, Instant::now()),
            theme: Styles::new(),
        }
    }

    /// DefaultBytes provides a progressbar to measure byte
    /// throughput with recommended defaults.
    ///
    /// # Example:
    /// ```rust
    /// use rpb::bar::Bar;
    /// let mut bar = Bar::default_bytes(100, "downloading");
    /// ```
    pub fn default_bytes(max_bytes: i64, desc: &str) -> Bar {
        Self {
            desc: desc.to_string(),
            state: State::new(max_bytes),
            option: Option {
                total: max_bytes,
                unit: Units::Bytes,
                start_time: Instant::now(),
                spinner: Spinner::new(type_spinner::get_spinner(Spinners::Point)),
                front_colored: "".to_string(),
                back_colored: "".to_string(),
                position: 0,
            },
            theme: Default::default(),
        }
    }

    /// Instantiate a new theme.
    ///
    ///  # Example
    ///
    /// ```rust
    /// use rpb::bar::Bar;
    /// use rpb::styles::Themes;
    /// let mut bar = Bar::new(100);
    /// bar.set_theme(Themes::Small);
    /// ```
    pub fn set_theme(&mut self, theme: Themes) {
        match theme {
            Themes::Basic => {
                self.theme.bar_type = '';
                self.theme.white_space = " ".to_string();
                self.theme.bar_start = '|';
                self.theme.bar_end = '|';
                self.theme.bar_width = 80;
                self.set_spinner(Spinners::Point);
            }
            Themes::ColoredMedium => {
                self.theme.bar_type = '';
                self.theme.white_space = "".to_string();
                self.theme.bar_start = '|';
                self.theme.bar_end = '|';
                self.theme.bar_width = 80;
                self.option.front_colored = "#021B79".to_string();
                self.set_spinner(Spinners::Weather);
            }
            Themes::Small => {
                self.theme.bar_type = '';
                self.theme.white_space = " ".to_string();
                self.theme.bar_start = ' ';
                self.theme.bar_end = ' ';
                self.theme.bar_width = 80;
                self.set_spinner(Spinners::Dots3);
            }
            Themes::ColoredSmall => {
                self.theme.bar_type = '';
                self.theme.white_space = "".to_string();
                self.theme.bar_start = ' ';
                self.theme.bar_end = ' ';
                self.theme.bar_width = 80;
                self.option.front_colored = "#0f3443".to_string();
                self.set_spinner(Spinners::Dots4);
            }
        }
    }

    /// Sets the position of the progress bar.
    /// By default, the progress bar are position 0.
    ///
    ///  # Example
    ///
    /// ```rust
    /// use rpb::bar::Bar;
    /// let mut bar = Bar::new(100);
    /// bar.set_position(1)
    /// ```
    pub fn set_position(&mut self, position: u32) {
        self.option.position = position
    }

    /// Sets description of progress bar.
    pub fn set_description(&mut self, desc: &str) {
        self.desc = desc.to_string()
    }

    /// Sets spinner of progress bar.
    pub fn set_spinner(&mut self, spinner: Spinners) {
        self.option.spinner = Spinner::new(type_spinner::get_spinner(spinner))
    }

    /// Increment current value of one.
    pub fn inc(&mut self) {
        self.add(1)
    }

    /// Set units, default is `Units::Default`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rpb::bar::{Bar, Units};
    ///
    /// let n_bytes = 100;
    /// let mut bar = Bar::new(n_bytes);
    /// bar.set_unit(Units::Bytes);
    /// ```
    pub fn set_unit(&mut self, u: Units) {
        self.option.unit = u;
    }

    /// Wraps an [`io::Read`] with the progress bar
    ///
    /// # Example:
    ///
    /// ```rust, no_run
    /// use std::fs::File;
    /// use std::io;
    /// use rpb::bar::Bar;
    /// use rpb::bar::Units;
    ///
    /// fn main () -> io::Result<()> {
    ///     let src = File::open("src.txt")?;
    ///     let mut tgt = File::create("tgt.txt")?;
    ///     let mut bar = Bar::new(src.metadata()?.len() as i64);
    ///     bar.set_unit(Units::Bytes);
    ///     io::copy(&mut bar.reader(src), &mut tgt).unwrap();
    ///     Ok(())
    /// }
    /// ```
    pub fn reader<R: io::Read>(&self, read: R) -> BarIter<R> {
        BarIter {
            it: read,
            bar: self.clone(),
        }
    }

    /// Wraps an [`io::Write`] with the progress bar
    ///
    /// # Example:
    ///
    /// ```rust, no_run
    /// use std::fs::File;
    /// use std::io;
    /// use rpb::bar::Bar;
    /// use rpb::bar::Units;
    ///
    /// fn main () -> io::Result<()> {
    ///     let mut src = File::open("src.txt")?;
    ///     let mut tgt = File::create("tgt.txt")?;
    ///     let mut bar = Bar::new(src.metadata()?.len() as i64);
    ///     bar.set_unit(Units::Bytes);
    ///     io::copy(&mut src, &mut bar.writer(tgt)).unwrap();
    ///     Ok(())
    /// }
    /// ```
    pub fn writer<R: io::Write>(&self, write: R) -> BarIter<R> {
        BarIter {
            it: write,
            bar: self.clone(),
        }
    }

    fn render_left_bar(&mut self) -> String {
        self.state.percent = get_percent(&self.state.current, &self.option.total);

        let desc_spacing = if self.desc == "" { "" } else { ": " };
        let mut spacing = if self.state.percent >= 10.0 {
            " "
        } else {
            "  "
        };

        if self.state.percent >= 100.0 {
            spacing = "";
        }

        format!(
            "{}{}{}{}%{}",
            self.desc, desc_spacing, spacing, self.state.percent as u64, self.theme.bar_start
        )
    }

    fn render_right_bar(&mut self) -> String {
        let mut white_space = self.theme.bar_width;
        let mut units = Vec::new();
        let mut current = Vec::new();
        let mut total = Vec::new();

        if self.state.current >= 1 {
            white_space -= self.state.current_graph_rate as usize;
        }
        let time_elapsed = self.option.start_time.elapsed().as_secs();
        let remaining_time = time_elapsed * (self.option.total - self.state.current) as u64
            / self.state.current as u64;
        let mut it_per_s: u64 = 0;
        if time_elapsed >= 1 {
            it_per_s = (self.state.current as u64) / time_elapsed;
        }
        let background: String;
        if self.option.back_colored != "" {
            background = self
                .theme
                .white_space
                .repeat(white_space)
                .colorize(self.option.back_colored.as_str());
        } else {
            background = self.theme.white_space.repeat(white_space);
        }

        match self.option.unit {
            Units::Default => {
                let curr = self.state.current;
                let tot = self.option.total;
                units.push(format!("{:.*}it/s", 2, it_per_s));
                current.push(format!("{:.*}", 2, curr));
                total.push(format!("{:.*}", 2, tot));
            }
            Units::Bytes => {
                let curr = self.state.current;
                let tot = self.option.total;
                units.push(format!("{}/s", unit_fmt!(it_per_s)));
                current.push(format!("{}", unit_fmt!(curr)));
                total.push(format!("{}", unit_fmt!(tot)));
            }
        };

        format!(
            "{}{} {}  [{}-{}, {}, {}/{}]",
            background,
            self.theme.bar_end.to_string().as_str(),
            self.option
                .spinner
                .spinning_cursor(self.state.current as usize),
            format::convert(time_elapsed),
            format::convert(remaining_time),
            units.into_iter().map(|x| x).collect::<String>(),
            current.into_iter().map(|x| x).collect::<String>(),
            total.into_iter().map(|x| x).collect::<String>(),
        )
    }

    fn render_middle_bar(&mut self) -> String {
        self.state.percent = get_percent(&self.state.current, &self.option.total);
        self.state.current_graph_rate =
            (self.state.percent / 100.0 * (self.theme.bar_width as f64)).round() as isize;

        let n: usize = (self.state.current_graph_rate) as usize;
        if self.option.front_colored != "" {
            self.theme.rate = format!("{}", self.theme.bar_type)
                .repeat(n)
                .colorize(self.option.front_colored.as_str());
        } else {
            self.theme.rate = format!("{}", self.theme.bar_type).repeat(n);
        }

        format!("{}", self.theme.rate)
    }

    fn render(&mut self) -> (String, String, String) {
        let lbar = self.render_left_bar();
        let mbar = self.render_middle_bar();
        let rbar = self.render_right_bar();

        return (lbar, mbar, rbar);
    }

    fn print_bar(&self, string: String) {
        if self.option.position == 0 {
            eprint!("{}", format_args!("\r{}", string));
        } else {
            eprint!(
                "{}",
                format_args!(
                    "{}{}{}",
                    "\n".repeat(self.option.position as usize),
                    string,
                    format!("\x1B[{}A", self.option.position)
                )
            );
        }
    }

    /// Manually update the progress bar, advances the position by `num`.
    pub fn add(&mut self, num: usize) {
        assert!(self.option.total > 0, "the max must be greater than zero");
        self.state.current += num as i64;
        assert!(
            self.state.current <= self.option.total,
            "current exceeds total"
        );
        let (lbar, mbar, rbar) = self.render();
        self.print_bar(format!("{}{}{}", lbar, mbar, rbar))
    }
}

fn get_percent(current: &i64, total: &i64) -> f64 {
    100.0 * (*current as f64) / (*total as f64)
}