progredient/
lib.rs

1pub mod config;
2
3use progressing::{clamping::Bar as ClampingBar, Baring};
4
5pub fn render(cfg: &config::Config) -> String {
6    let config::Config {
7        from,
8        to,
9        at,
10        length,
11        label,
12        label_placement,
13        style,
14        ..
15    } = cfg;
16
17    let mut progress_bar = ClampingBar::new();
18
19    progress_bar.set((at - from) as f64 / (to - from) as f64);
20
21    progress_bar.set_len(*length);
22    progress_bar.set_style(style);
23
24    match label_placement {
25        _ if label.is_empty() => format!("{progress_bar}"),
26        config::LabelPlacement::Left => format!("{label} {progress_bar}"),
27        config::LabelPlacement::Right => format!("{progress_bar} {label}"),
28    }
29}
30
31#[test]
32fn test_render_default_cfg() {
33    let cfg = config::Config::default();
34
35    let output = render(&cfg);
36
37    assert_eq!(output, "[>.................]");
38}
39
40#[test]
41fn test_render_from_to_at() {
42    let cfg = config::Config {
43        from: 0,
44        to: 100,
45        at: 50,
46        ..config::Config::default()
47    };
48
49    let output = render(&cfg);
50
51    assert_eq!(output, "[=========>........]");
52}
53
54#[test]
55fn test_render_length() {
56    let cfg = config::Config {
57        length: 10,
58        ..config::Config::default()
59    };
60
61    let output = render(&cfg);
62
63    assert_eq!(output, "[>.......]");
64}
65
66#[test]
67fn test_render_label() {
68    let cfg = config::Config {
69        label: String::from("test"),
70        ..config::Config::default()
71    };
72
73    let output = render(&cfg);
74
75    assert_eq!(output, "[>.................] test");
76}
77
78#[test]
79fn test_render_style() {
80    let cfg = config::Config {
81        from: 0,
82        to: 100,
83        at: 50,
84        style: String::from("()+ }"),
85        ..config::Config::default()
86    };
87
88    let output = render(&cfg);
89
90    assert_eq!(output, "()))))))))+        }");
91}