use crate::styles::*;
use crate::term;
pub struct Summary {
sections: Vec<SummarySection>,
}
struct SummarySection {
entries: Vec<SummaryEntry>,
}
enum SummaryEntry {
Done(String),
Warn(String),
Stat { key: String, val: String },
Blank,
}
impl Summary {
pub fn new() -> Self {
Self {
sections: vec![SummarySection {
entries: Vec::new(),
}],
}
}
pub fn done(mut self, msg: &str) -> Self {
self.cur().entries.push(SummaryEntry::Done(msg.to_string()));
self
}
pub fn warn(mut self, msg: &str) -> Self {
self.cur().entries.push(SummaryEntry::Warn(msg.to_string()));
self
}
pub fn stat(mut self, key: &str, val: &str) -> Self {
self.cur().entries.push(SummaryEntry::Stat {
key: key.to_string(),
val: val.to_string(),
});
self
}
pub fn blank(mut self) -> Self {
self.cur().entries.push(SummaryEntry::Blank);
self
}
pub fn section(mut self) -> Self {
self.sections.push(SummarySection {
entries: Vec::new(),
});
self
}
pub fn print(self) {
let w = term::width().min(80);
let rule = paint(DIM, &"─".repeat(w));
eprintln!();
eprintln!(" {rule}");
eprintln!();
for section in &self.sections {
let max_key = section
.entries
.iter()
.filter_map(|e| match e {
SummaryEntry::Stat { key, .. } => Some(key.len()),
_ => None,
})
.max()
.unwrap_or(0);
for entry in §ion.entries {
match entry {
SummaryEntry::Done(msg) => {
let badge = paint(
anstyle::Style::new()
.bg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green)))
.fg_color(Some(anstyle::Color::Ansi(
anstyle::AnsiColor::BrightWhite,
)))
.bold(),
" DONE ",
);
eprintln!(" {} {}", badge, paint(OK, msg));
}
SummaryEntry::Warn(msg) => {
let badge = paint(
anstyle::Style::new()
.bg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow)))
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Black)))
.bold(),
" WARN ",
);
eprintln!(" {} {}", badge, paint(YELLOW, msg));
}
SummaryEntry::Stat { key, val } => {
let pad = " ".repeat(max_key - key.len());
eprintln!(" {}{} {}", pad, paint(DIM, key), val);
}
SummaryEntry::Blank => eprintln!(),
}
}
}
eprintln!();
eprintln!(" {rule}");
eprintln!();
}
fn cur(&mut self) -> &mut SummarySection {
self.sections.last_mut().unwrap()
}
}
impl Default for Summary {
fn default() -> Self {
Self::new()
}
}