diff options
| author | rorka900 <rorka900@gmail.com> | 2026-03-23 06:45:05 +0000 |
|---|---|---|
| committer | rorka900 <rorka900@gmail.com> | 2026-03-23 06:45:37 +0000 |
| commit | 8eff76834457baab7dc9e2da9e966111c45787c8 (patch) | |
| tree | 9795fe0dfbac1de202c45d7844fb56621030ac2f /src/progress.rs | |
| parent | 3c052d789162be533ab5159bbcb888fc26b01bc5 (diff) | |
| download | ror-8eff76834457baab7dc9e2da9e966111c45787c8.tar.gz ror-8eff76834457baab7dc9e2da9e966111c45787c8.zip | |
feat: add progress bar, fix rootfs creation, and apply FHS layout
Diffstat (limited to 'src/progress.rs')
| -rw-r--r-- | src/progress.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/progress.rs b/src/progress.rs new file mode 100644 index 0000000..f6f2227 --- /dev/null +++ b/src/progress.rs @@ -0,0 +1,94 @@ +use std::io::{self, Write}; + +pub struct ProgressBar { + total: usize, + current: usize, + message: String, + width: usize, +} + +impl ProgressBar { + pub fn new(total: usize, message: &str) -> Self { + Self { + total, + current: 0, + message: message.to_string(), + width: 38, + } + } + + + pub fn inc(&mut self, msg: &str) { + self.current += 1; + self.message = msg.to_string(); + self.render(); + } + + fn render(&self) { + let filled = if self.total == 0 { + 0 + } else { + self.current * self.width / self.total + }; + let arrow = if filled < self.width { ">" } else { "=" }; + let bar = format!( + "{}{}{}", + "=".repeat(filled.saturating_sub(1)), + arrow, + " ".repeat(self.width.saturating_sub(filled)) + ); + eprint!( + "\r\x1b[K\x1b[36m[{}]\x1b[0m \x1b[32m{}/{}\x1b[0m {}", + bar, + self.current, + self.total, + self.message + ); + io::stderr().flush().ok(); + } + + pub fn finish(&self, msg: &str) { + let bar = "=".repeat(self.width); + eprintln!( + "\r\x1b[K\x1b[36m[{}]\x1b[0m \x1b[32m{}/{}\x1b[0m {}", + bar, self.total, self.total, msg + ); + } +} + +pub struct Spinner { + frames: &'static [&'static str], + idx: usize, + message: String, +} + +impl Spinner { + pub fn new(message: &str) -> Self { + Self { + frames: &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"], + idx: 0, + message: message.to_string(), + } + } + + pub fn tick(&mut self, msg: &str) { + self.message = msg.to_string(); + eprint!("\r\x1b[K\x1b[36m{}\x1b[0m {}", self.frames[self.idx], self.message); + io::stderr().flush().ok(); + self.idx = (self.idx + 1) % self.frames.len(); + } + + pub fn finish(&self, msg: &str) { + eprintln!("\r\x1b[K\x1b[32m✓\x1b[0m {}", msg); + } +} + +pub fn format_bytes(bytes: u64) -> String { + if bytes < 1024 { + format!("{} B", bytes) + } else if bytes < 1024 * 1024 { + format!("{:.1} KB", bytes as f64 / 1024.0) + } else { + format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0)) + } +} |