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(); } pub 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)) } } pub struct DownloadBar { name: String, total: Option, downloaded: u64, start_time: std::time::Instant, last_render: std::time::Instant, width: usize, } impl DownloadBar { pub fn new(name: &str, total: Option) -> Self { let bar = Self { name: name.to_string(), total, downloaded: 0, start_time: std::time::Instant::now(), last_render: std::time::Instant::now(), width: 25, }; bar.render(); bar } pub fn update(&mut self, delta: u64) { self.downloaded += delta; if self.last_render.elapsed().as_millis() >= 80 { self.render(); self.last_render = std::time::Instant::now(); } } pub fn render(&self) { let elapsed = self.start_time.elapsed().as_secs_f64(); let speed = if elapsed > 0.05 { format!("{}/s", format_bytes((self.downloaded as f64 / elapsed) as u64)) } else { "-- B/s".to_string() }; if let Some(total) = self.total { if total > 0 { let pct = ((self.downloaded as f64 / total as f64) * 100.0).min(100.0); let pos = ((self.downloaded as usize * self.width) / total as usize).min(self.width); let runner = if (self.downloaded / 65536) % 2 == 0 { "R" } else { "r" }; let mut bar = String::new(); for i in 0..self.width { if i < pos { bar.push('═'); } else if i == pos { bar.push_str("\x1b[1;36m"); bar.push_str(runner); bar.push_str("\x1b[0m"); } else if i % 3 == 0 { bar.push_str("\x1b[1;37mo\x1b[0m"); } else { bar.push(' '); } } eprint!( "\r\x1b[K\x1b[1;34m[ror]\x1b[0m {} \x1b[36m[{}]\x1b[0m \x1b[32m{:>3.0}%\x1b[0m {:>8} / {:<8} \x1b[90m({})\x1b[0m", self.name, bar, pct, format_bytes(self.downloaded), format_bytes(total), speed ); io::stderr().flush().ok(); return; } } let pos = ((self.start_time.elapsed().as_millis() / 150) as usize) % self.width; let runner = if (pos % 2) == 0 { "R" } else { "r" }; let mut bar = String::new(); for i in 0..self.width { if i < pos { bar.push('═'); } else if i == pos { bar.push_str("\x1b[1;36m"); bar.push_str(runner); bar.push_str("\x1b[0m"); } else if i % 3 == 0 { bar.push_str("\x1b[1;37mo\x1b[0m"); } else { bar.push(' '); } } eprint!( "\r\x1b[K\x1b[1;34m[ror]\x1b[0m {} \x1b[36m[{}]\x1b[0m {:>8} downloaded \x1b[90m({})\x1b[0m", self.name, bar, format_bytes(self.downloaded), speed ); io::stderr().flush().ok(); } pub fn finish(&self) { let elapsed = self.start_time.elapsed().as_secs_f64(); let speed = if elapsed > 0.05 { format!("{}/s", format_bytes((self.downloaded as f64 / elapsed) as u64)) } else { String::new() }; eprintln!( "\r\x1b[K\x1b[1;32m[ror] ✓\x1b[0m Downloaded {} ({}{})", self.name, format_bytes(self.downloaded), if speed.is_empty() { String::new() } else { format!(", {}", speed) } ); } }