Something went wrong. Try again.
A simple TUI Library written in Rust
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151use crate::block::Block;use crate::style::Span;
#[derive(Debug, Clone)]pub enum ProgressStyle { Bar { filled: &'static str, empty: &'static str, }, Braille, AsciiBar,}
#[derive(Debug, Clone)]pub struct ProgressBar { current: u64, total: u64, width: usize, style: ProgressStyle, label: String,}
impl ProgressBar { /// Create a new progress bar with default settings. pub fn new(total: u64) -> Self { ProgressBar { current: 0, total, width: 20, style: ProgressStyle::Bar { filled: "█", empty: "░", }, label: String::new(), } }
/// Set the progress bar style. pub fn style(self, style: ProgressStyle) -> Self { Self { style, ..self } }
/// Set the progress bar label. pub fn label(self, label: impl Into<String>) -> Self { Self { label: label.into(), ..self } }
/// Set the bar width (number of characters for the bar portion). pub fn width(self, width: usize) -> Self { Self { width, ..self } }
/// Set the current progress value. pub fn set(self, current: u64) -> Self { Self { current: current.min(self.total), ..self } }
/// Get the current percentage (0-100). pub fn percentage(&self) -> u64 { if self.total == 0 { 0 } else { (self.current * 100) / self.total } }
/// Render the progress bar as a Block. pub fn to_block(&self) -> Block { let pct_str = format!("{}%", self.percentage()); let bar_str = self.render_bar(); let line_text = if self.label.is_empty() { format!("{bar_str} {pct_str}") } else { format!("{} {bar_str} {pct_str}", self.label) }; Block::text(vec![vec![Span::plain(line_text)]]) }
fn render_bar(&self) -> String { let ratio = if self.total == 0 { 0.0 } else { self.current as f64 / self.total as f64 }; let filled_count = (ratio * self.width as f64).round() as usize; let empty_count = self.width.saturating_sub(filled_count); match &self.style { ProgressStyle::Bar { filled, empty } => { filled.repeat(filled_count) + &empty.repeat(empty_count) } ProgressStyle::Braille => "⣿".repeat(filled_count) + &"⣀".repeat(empty_count), ProgressStyle::AsciiBar => { format!("[{}{}]", "#".repeat(filled_count), "-".repeat(empty_count)) } } }}
/// Get a list of available styles for demonstration.pub fn styles() -> Vec<(&'static str, ProgressStyle)> { vec![ ( "bar", ProgressStyle::Bar { filled: "█", empty: "░", }, ), ("braille", ProgressStyle::Braille), ("ascii", ProgressStyle::AsciiBar), ]}
#[cfg(test)]mod tests { use super::*;
#[test] fn percentage_calculation() { let bar = ProgressBar::new(100).set(50); assert_eq!(bar.percentage(), 50); }
#[test] fn percentage_zero_total() { let bar = ProgressBar::new(0); assert_eq!(bar.percentage(), 0); }
#[test] fn set_clamps_to_total() { let bar = ProgressBar::new(100).set(150); assert_eq!(bar.current, 100); }
#[test] fn bar_renders() { let bar = ProgressBar::new(10).width(10).set(5); let block = bar.to_block(); let lines = block.render(40); assert_eq!(lines.len(), 1); assert!(lines[0].contains("50%")); }}