-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.rs
More file actions
43 lines (36 loc) · 1.11 KB
/
Copy pathprogress.rs
File metadata and controls
43 lines (36 loc) · 1.11 KB
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
//! # TechScript CLI Progress Bar Utility
//!
//! Renders visual progress bars in-place inside terminal environments.
use colored::Colorize;
use std::io::Write;
pub struct ProgressBar {
width: usize,
}
impl ProgressBar {
pub fn new() -> Self {
Self { width: 25 }
}
/// Renders the progress bar at a specific percentage and label.
pub fn update(&self, percent: usize, label: &str) {
let filled = (percent * self.width) / 100;
let empty = self.width - filled;
let color_enabled = colored::control::SHOULD_COLORIZE.should_colorize();
let bar_str = if color_enabled {
format!("{}{}", "=".repeat(filled).cyan(), " ".repeat(empty))
} else {
format!("{}{}", "=".repeat(filled), " ".repeat(empty))
};
print!(
"\r [{}] {:>3}% — Compiling: {}",
bar_str,
percent,
label.bold()
);
std::io::stdout().flush().ok();
}
/// Clears the progress line.
pub fn clear(&self) {
print!("\r{}\r", " ".repeat(75));
std::io::stdout().flush().ok();
}
}