use crate::console::{Console, Renderable};
use crate::progress::{ProgressColumn, Task};
use crate::progress_bar::ProgressBar;
use crate::text::Text;
#[derive(Debug, Clone)]
pub struct BarColumn {
pub bar_width: Option<usize>,
pub style: String,
pub complete_style: String,
pub finished_style: String,
pub pulse_style: String,
}
impl BarColumn {
pub fn new() -> Self {
BarColumn {
bar_width: Some(40),
style: "bar.back".to_string(),
complete_style: "bar.complete".to_string(),
finished_style: "bar.finished".to_string(),
pulse_style: "bar.pulse".to_string(),
}
}
#[must_use]
pub fn with_bar_width(mut self, width: Option<usize>) -> Self {
self.bar_width = width;
self
}
}
impl Default for BarColumn {
fn default() -> Self {
Self::new()
}
}
impl ProgressColumn for BarColumn {
fn render(&self, task: &Task) -> Text {
let bar = ProgressBar::new()
.with_total(task.total)
.with_completed(task.completed)
.with_width(self.bar_width)
.with_style(&self.style)
.with_complete_style(&self.complete_style)
.with_finished_style(&self.finished_style)
.with_pulse_style(&self.pulse_style);
let console = Console::builder()
.width(self.bar_width.unwrap_or(40))
.color_system("truecolor")
.build();
let opts = console.options();
let segments = bar.gilt_console(&console, &opts);
let mut text = Text::empty();
for seg in &segments {
text.append_str(&seg.text, seg.style().cloned());
}
text.end = String::new();
text
}
}