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
use crate::{timing, Baring};
use log::warn;
use std::{
    cmp::min,
    fmt::{self, Display},
};

#[derive(Debug)]
struct PrintController {
    last_printed_progress: Option<f64>,
    interesting_progress_step: f64,
}

impl PrintController {
    fn from(interesting_progress_step: f64) -> PrintController {
        PrintController {
            last_printed_progress: None,
            interesting_progress_step,
        }
    }

    fn map(&self, progress: f64) -> usize {
        let scale = 1_000.0;
        (progress * scale) as usize / ((scale * self.interesting_progress_step) as usize)
    }

    fn has_progressed_significantly(&self, progress: f64) -> bool {
        self.map(progress) > self.map(self.last_printed_progress.unwrap_or(0.0))
    }

    fn update(&mut self, progress: f64) {
        self.last_printed_progress = Some(progress);
    }
}

pub struct Config {
    pub bar_len: usize,
    pub style: String,
    pub interesting_progress_step: f64,
}

impl Config {
    pub fn new() -> Config {
        Config::default()
    }
}

impl Default for Config {
    fn default() -> Config {
        Config {
            bar_len: 42,
            style: String::from("[=>.]"),
            interesting_progress_step: 0.1,
        }
    }
}

/// A progress-bar clamping values to `[0, 1]`.
///
///
/// # Mini-Example
///
/// ```
/// use progressing::{clamping::Bar as ClampingBar, Baring};
///
/// /// Printing value 0.3 clamped to [0, 1]
/// /// [=====>------------]
/// fn main() {
///     println!("Printing value 0.3 clamped to [0, 1]");
///     let mut progress_bar = ClampingBar::new();
///     progress_bar.set_len(20);
///     progress_bar.set(0.3);
///     println!("{}", progress_bar);
/// }
/// ```
///
/// It is only optimized for single-length-strings, but strings are more handy than chars and hence used as implementation.
#[derive(Debug)]
pub struct Bar {
    bar_len: usize,
    style: String,
    progress: f64,
    print_controller: PrintController,
}

impl Bar {
    pub fn new() -> Bar {
        Bar::default()
    }

    pub fn with(cfg: Config) -> Bar {
        Bar {
            bar_len: cfg.bar_len,
            style: cfg.style,
            progress: 0.0,
            print_controller: PrintController::from(cfg.interesting_progress_step),
        }
    }

    pub fn timed(self) -> timing::Bar<Bar> {
        timing::Bar::with(self)
    }

    pub fn set_style<S>(&mut self, style: S)
    where
        S: Into<String>,
    {
        let style = style.into();

        if style.len() != 5 {
            warn!("The bar-style has to consist of 5 characters, e.g. [=>-]");
        };
        self.style = style;
    }

    fn inner_bar_len(&self) -> usize {
        self.len() - self.brackets_len()
    }

    fn brackets_len(&self) -> usize {
        self.left_bracket().len() + self.right_bracket().len()
    }

    fn left_bracket(&self) -> &str {
        &self.style[0..1]
    }

    fn line(&self) -> &str {
        &self.style[1..2]
    }

    fn hat(&self) -> &str {
        &self.style[2..3]
    }

    fn empty_line(&self) -> &str {
        &self.style[3..4]
    }

    fn right_bracket(&self) -> &str {
        &self.style[4..5]
    }
}

impl Default for Bar {
    fn default() -> Bar {
        Bar::with(Config::default())
    }
}

impl Baring for Bar {
    type Progress = f64;

    fn len(&self) -> usize {
        self.bar_len
    }

    /// panics if length is `< 3`
    fn set_len(&mut self, new_bar_len: usize) {
        self.bar_len = new_bar_len;
    }

    fn progress(&self) -> f64 {
        self.progress
    }

    fn set<P>(&mut self, new_progress: P)
    where
        P: Into<f64>,
    {
        let mut new_progress = new_progress.into();

        if new_progress < 0.0 {
            new_progress = 0.0;
        }
        if 1.0 < new_progress {
            new_progress = 1.0;
        }
        self.progress = new_progress;
    }

    fn start(&self) -> f64 {
        0.0
    }

    fn end(&self) -> f64 {
        1.0
    }

    fn has_progressed_significantly(&self) -> bool {
        self.print_controller
            .has_progressed_significantly(self.progress())
    }

    fn remember_significant_progress(&mut self) {
        self.print_controller.update(self.progress());
    }
}

impl Display for Bar {
    /// Progress is clamped to `[0, 1]`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // calc progress
        // -> bar needs to be calculated
        // -> no brackets involved
        let reached: usize = (self.progress * self.inner_bar_len() as f64) as usize;

        let line = self.line().repeat(reached);
        // crop hat if end of bar is reached
        let hat = &self.hat()[0..min(self.hat().len(), self.inner_bar_len() - reached)];
        // fill up rest with empty line
        let empty_line = self
            .empty_line()
            .repeat(self.inner_bar_len() - reached - hat.len());
        write!(
            f,
            "{}{}{}{}{}",
            self.left_bracket(),
            line,
            hat,
            empty_line,
            self.right_bracket()
        )
    }
}