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
use better_term::{Color, flush_styles};
#[cfg(feature="crossterm")]
use crossterm::{cursor, execute};
#[cfg(feature="crossterm")]
use std::io::stdout;

#[derive(Debug, Clone)]
pub enum BarType {
    Bar,    // [██████████]
    RawBar, // ██████
    Dots,   // ...
    Line,   // |
}

/// The struct for handling progress bars
///
/// # Example:
/// ```rust
/// use pbars::{PBar, BarType};
/// use std::thread::sleep;
/// use std::time::Duration;
///
/// fn main() {
///     // using crossterm, this will create a pbar at 0,0
///     // without crossterm, this is the only way to create a bar
///     let mut pbar = PBar::new(BarType::Bar, true, true, 20);
///
///     for x in 0..1000 {
///         // get the percentage complete as a decimal
///         let percentage_decimal = x as f32 / 1000.0;
///         // scale the percentage from 0..1 to 0..100 and convert to a u8
///         let percent = (percentage_decimal * 100.0) as u8;
///         // update the pbar
///         pbar.update(percent);
///         // draw the pbar
///         pbar.draw();
///         // delay for 10ms, making this run in 10 seconds
///         sleep(Duration::from_millis(10));
///     }
/// }
/// ```
///
/// # crossterm specific creation
/// Creating at a location
/// ```rust
/// use pbars::{PBar, BarType};
///
/// PBar::new_at(0, 0, BarType::Bar, true, true, 20);
/// ```
///
/// Creating at the cursor's current location
/// ```rust
/// use pbars::{PBar, BarType};
///
/// PBar::new_at_cursor(BarType::Bar, true, true, 20);
/// ```
#[derive(Debug, Clone)]
pub struct PBar {
    // crossterm position handling
    #[cfg(feature="crossterm")]
    x: u16,
    #[cfg(feature="crossterm")]
    y: u16,

    // settings
    color: bool,
    bar_type: BarType,
    show_percent: bool,
    bar_length: u16,

    // for special types
    repeat_at: u8,

    // value the bar is at
    percent: u8,
}

impl PBar {
    /// create a new progress bar when not using crossterm
    /// # parameters
    /// bar_type: the type of bar to display
    /// color: if it should output in color
    /// show_percent: if it should show the percentage next to the bar
    /// bar_length: How long the bar should be
    #[cfg(not(feature="crossterm"))]
    pub fn new(bar_type: BarType, color: bool, show_percent: bool, bar_length: u16) -> Self {
        Self {
            percent: 0,
            color, bar_type, bar_length, show_percent,
            repeat_at: 0,
        }
    }

    #[cfg(not(feature="crossterm"))]
    fn set_pos(&mut self) {
        print!("\r");
    }

    /// create a new progress bar when using crossterm
    /// # parameters
    /// bar_type: the type of bar to display
    /// color: if it should output in color
    /// show_percent: if it should show the percentage next to the bar
    /// bar_length: How long the bar should be
    #[cfg(feature="crossterm")]
    pub fn new(bar_type: BarType, color: bool, show_percent: bool, bar_length: u16) -> Self {
        Self::new_at(0, 0, bar_type, color, show_percent, bar_length)
    }

    /// Create a new bar using crossterm at a location
    /// # parameters
    /// x: the x location to put the bar (0 is the left)
    /// y: the y location to put the bar (0 is the top)
    /// bar_type: the type of bar to display
    /// color: if it should output in color
    /// show_percent: if it should show the percentage next to the bar
    /// bar_length: How long the bar should be
    #[cfg(feature="crossterm")]
    pub fn new_at(x: u16, y: u16, bar_type: BarType, color: bool, show_percent: bool, bar_length: u16) -> Self {
        Self {
            x, y, percent: 0,
            color, bar_type, bar_length, show_percent,
            repeat_at: 0,
        }
    }

    /// Creates a new bar using crossterm at the cursor's current position
    /// # parameters
    /// bar_type: the type of bar to display
    /// color: if it should output in color
    /// show_percent: if it should show the percentage next to the bar
    /// bar_length: How long the bar should be
    #[cfg(feature="crossterm")]
    pub fn new_at_cursor(bar_type: BarType, color: bool, show_percent: bool, bar_length: u16) -> Result<Self, String> {
        let pos = crossterm::cursor::position();
        if pos.is_err() {
            return Err(pos.unwrap_err().to_string());
        }

        let (x, y) = pos.unwrap();

        Ok(Self::new_at(x, y, bar_type, color, show_percent, bar_length))
    }

    #[cfg(feature="crossterm")]
    fn set_pos(&mut self) -> Result<(), String> {
        let r = execute!(stdout(), cursor::MoveTo(self.x, self.y));
        if r.is_err() {
            return Err(r.unwrap_err().to_string());
        }
        Ok(())
    }

    /// Update the bar's progress
    /// # Parameters
    /// percent: the new percentage (0 to 100)
    pub fn update(&mut self, mut percent: u8) {
        if percent > 100 {
            percent = 100;
        }
        self.percent = percent;
    }

    fn draw_dots_and_line(&mut self, output: String, color: Color) -> String {
        format!("{}{}", output, if self.show_percent {
            if self.color {
                format!(" {}{}%", color, self.percent)
            } else {
                format!(" {}%", self.percent)
            }
        } else {
            format!("")
        })
    }

    /// Draw the bar at its set location
    pub fn draw(&mut self) {
        #[cfg(feature="crossterm")]
        {
            let r = self.set_pos();
            if r.is_err() {
                panic!("Failed to set cursor position!");
            }
        }

        #[cfg(not(feature="crossterm"))]
        self.set_pos();

        // get the current completion color of the bar
        // only used if self.color is true
        let red = 255 - ((self.percent as f32 / 100.0) * 200.0) as u8;
        let green = (self.percent as f32 / 100.0 * 200.0) as u8;
        let color = Color::RGB(red, green, 25);

        let output = match self.bar_type {
            BarType::RawBar => {
                let chunk_weight = 100 / self.bar_length;
                // set how complete the bar should be
                let bar_completion = self.percent as usize / chunk_weight as usize;

                if self.color {
                    // create the colored bar
                    let bar = format!("{}{}", color, "█".repeat(bar_completion));

                    format!("{}{}", bar, if self.show_percent {
                        format!(" {}{}{}%", color, self.percent, Color::White)
                    } else { format!("") })
                } else {
                    // create the bar
                    let bar = format!("{}", "█".repeat(bar_completion));

                    format!("{}{}", bar, if self.show_percent {
                        format!(" {}%", self.percent)
                    } else { format!("") })
                }
            }
            BarType::Bar => {
                let chunk_weight = 100 / self.bar_length;
                // set how complete the bar should be
                let bar_completion = self.percent as usize / chunk_weight as usize;
                let mut bar_uncomplete = (100 - (self.percent)) as usize / chunk_weight as usize;
                // handle if the bar needs to be resized because of rounding issues
                let add = bar_completion + bar_uncomplete;
                if add < self.bar_length as usize {
                    bar_uncomplete += 1;
                }
                if add > self.bar_length as usize {
                    bar_uncomplete -= 1;
                }

                if self.color {
                    // create the main bar
                    let completed_bar = format!("{}{}", color, "█".repeat(bar_completion));
                    let uncompleted_bar = format!("{}{}", Color::BrightBlack, "█".repeat(bar_uncomplete));

                    // format the bar and return it
                    format!("{dc}[{}{}{dc}]{}", completed_bar, uncompleted_bar, if self.show_percent {
                        format!(" {}{}{}%", color, self.percent, Color::White)
                    } else { format!("") }, dc = Color::White)
                } else {
                    // create the main bar
                    let completed_bar = format!("{}", "█".repeat(bar_completion));
                    let uncompleted_bar = format!("{}", "=".repeat(bar_uncomplete));

                    // format the bar and return it
                    format!("[{}{}]{}", completed_bar, uncompleted_bar, if self.show_percent {
                        format!(" {}%", self.percent)
                    } else { format!("") })
                }
            }
            BarType::Dots => {
                if self.percent % 2 == 0 {
                    self.repeat_at += 1;
                    if self.repeat_at == 3 {
                        self.repeat_at = 0;
                    }
                }

                let output = match self.repeat_at {
                    0 => {
                        format!(".  ")
                    }
                    1 => {
                        format!(".. ")
                    }
                    _  => {
                        format!("...")
                    }
                };

                self.draw_dots_and_line(output, color)
            }
            BarType::Line => {
                if self.percent % 2 == 0 {
                    self.repeat_at += 1;
                    if self.repeat_at == 4 {
                        self.repeat_at = 0;
                    }
                }

                let output = match self.repeat_at {
                    0 => {
                        format!("|")
                    }
                    1 => {
                        format!("/")
                    }
                    2 => {
                        format!("-")
                    }
                    _ => {
                        format!("\\")
                    }
                };

                self.draw_dots_and_line(output, color)
            }
        };

        #[cfg(feature="crossterm")]
        print!("{}", output);
        #[cfg(not(feature="crossterm"))]
        print!("\r{}", output);

        if self.color {
            flush_styles()
        }
    }

    pub fn is_complete(&self) -> bool {
        self.percent == 100
    }
}

/// Hides the cursor using crossterm
#[cfg(feature="crossterm")]
pub fn hide_cursor() {
    execute!(stdout(), crossterm::cursor::Hide).expect("Failed to hide cursor!");
}

/// Shows the cursor using crossterm
#[cfg(feature="crossterm")]
pub fn show_cursor() {
    execute!(stdout(), crossterm::cursor::Show).expect("Failed to show cursor!");
}

impl Default for PBar {
    /// Creates a bar using default values
    #[cfg(feature="crossterm")]
    fn default() -> Self {
        Self::new_at_cursor(BarType::Bar, true, true, 20).expect("Failed to make default PBar")
    }

    /// Creates a bar using default values
    #[cfg(not(feature="crossterm"))]
    fn default() -> Self {
        Self::new(BarType::Bar, true, true, 20)
    }
}

#[cfg(test)]
mod tests {
    use std::thread;
    use std::time::Duration;
    use crossterm::execute;
    use crossterm::terminal::ClearType;
    use crate::{BarType, PBar};
    use crate::{hide_cursor, show_cursor};

    #[test]
    fn t1() {
        use std::io::stdout;
        execute!(stdout(), crossterm::terminal::Clear(ClearType::All)).expect("Failed to clear screen!");
        let mut pbar = PBar::new_at(0, 1, BarType::RawBar,
                                    true, true, 20);
        let mut pbar2 = PBar::new_at(0, 3, BarType::Bar,
                                     true, true, 20);
        let mut pbar3 = PBar::new_at(7, 5, BarType::Dots,
                                     true, true, 20);
        let mut pbar4 = PBar::new_at(8, 7, BarType::Line,
                                     true, true, 20);

        hide_cursor();

        execute!(stdout(), crossterm::cursor::MoveTo(0, 5)).expect("Failed to move!");
        print!("Loading");

        execute!(stdout(), crossterm::cursor::MoveTo(0, 7)).expect("Failed to move!");
        print!("Loading");

        let max = 1000;
        for x in 0..max {
            let percent = ((x as f32 / (max - 1) as f32) * 100.0) as u8;
            pbar.update(percent);
            pbar.draw();

            pbar2.update(percent);
            pbar2.draw();

            pbar3.update(percent);
            pbar3.draw();

            pbar4.update(percent);
            pbar4.draw();
            thread::sleep(Duration::from_millis(10));
        }
        println!();
        show_cursor();
    }
}