use crate::theme::{self, COLORS};
use ratatui::{
style::{Color, Style},
widgets::Gauge,
};
pub struct ProgressBar {
pub progress: f64,
pub label: Option<String>,
pub color: Color,
}
impl ProgressBar {
pub fn new(progress: f64) -> Self {
ProgressBar {
progress: progress.clamp(0.0, 1.0),
label: None,
color: theme::current_accent(),
}
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(label.to_string());
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn update(&mut self, progress: f64) {
self.progress = progress.clamp(0.0, 1.0);
}
pub fn render(&self) -> Gauge<'_> {
let label = match &self.label {
Some(lbl) => format!("{} {:.0}%", lbl, self.progress * 100.0),
None => format!("{:.0}%", self.progress * 100.0),
};
Gauge::default()
.gauge_style(Style::default().fg(self.color).bg(COLORS.bg_dark))
.label(label)
.ratio(self.progress)
}
}