use crate::{layout::Rect, render::Frame, style::Style};
pub struct ProgressBar {
total: u64,
current: u64,
width: u16,
style: Style,
filled_style: Style,
}
impl ProgressBar {
pub fn new(total: u64, width: u16) -> Self {
Self { total, current: 0, width, style: Style::new(), filled_style: Style::new().reversed() }
}
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn filled_style(mut self, style: Style) -> Self {
self.filled_style = style;
self
}
pub fn update(&mut self, current: u64) {
self.current = current;
}
pub fn inc(&mut self, delta: u64) {
self.current += delta;
}
pub fn finish(&mut self) {
self.current = self.total;
}
pub fn percentage(&self) -> f64 {
if self.total > 0 { (self.current as f64 / self.total as f64) * 100.0 } else { 0.0 }
}
}
impl super::Component for ProgressBar {
fn render(&self, frame: &mut Frame, area: Rect) {
frame.render_progress_bar(self.current, self.total, self.width, area, self.style.clone(), self.filled_style.clone());
}
fn handle_event(&mut self, _event: &crate::event::Event) -> bool {
false
}
}