2020-08-17 17:38:29 +05:30
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-18 16:17:42 +05:30
|
|
|
#[derive(Clone, PartialEq)]
|
2020-08-17 17:38:29 +05:30
|
|
|
pub enum ReportOutcome {
|
|
|
|
Success,
|
|
|
|
NonFatal,
|
|
|
|
Fatal,
|
|
|
|
}
|
|
|
|
|
|
|
|
use ReportOutcome::*;
|
|
|
|
|
|
|
|
impl ReportOutcome {
|
|
|
|
pub fn combine(lhs: &ReportOutcome, rhs: &ReportOutcome) -> ReportOutcome {
|
|
|
|
match (lhs, rhs) {
|
|
|
|
(Success, rhs) => rhs.clone(),
|
|
|
|
(lhs, Success) => lhs.clone(),
|
|
|
|
(Fatal, _) => Fatal,
|
|
|
|
(_, Fatal) => Fatal,
|
|
|
|
(_, _) => NonFatal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Report {
|
|
|
|
outcome: Mutex<ReportOutcome>,
|
|
|
|
inner: Mutex<Box<dyn ReportInner + Send>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
trait ReportInner {
|
2020-08-17 20:06:21 +05:30
|
|
|
fn set_title(&mut self, txt: &str);
|
|
|
|
fn set_sub_title(&mut self, txt: &str);
|
|
|
|
fn progress(&mut self, percent: u8);
|
|
|
|
fn log(&mut self, txt: &str);
|
|
|
|
fn complete(&mut self);
|
2020-08-17 17:38:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
impl Report {
|
|
|
|
fn new(inner: Box<dyn ReportInner + Send>) -> Report {
|
|
|
|
Report {
|
|
|
|
outcome: Mutex::new(Success),
|
|
|
|
inner: Mutex::new(inner),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_outcome(&self, rhs: ReportOutcome) {
|
|
|
|
let mut lhs = self.outcome.lock().unwrap();
|
|
|
|
*lhs = ReportOutcome::combine(&lhs, &rhs);
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
pub fn set_title(&self, txt: &str) {
|
2020-08-17 17:38:29 +05:30
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
inner.set_title(txt)
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
pub fn set_sub_title(&self, txt: &str) {
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
inner.set_sub_title(txt)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn progress(&self, percent: u8) {
|
2020-08-17 17:38:29 +05:30
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
inner.progress(percent)
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
pub fn info(&self, txt: &str) {
|
2020-08-17 17:38:29 +05:30
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
inner.log(txt)
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
pub fn non_fatal(&self, txt: &str) {
|
2020-08-17 17:38:29 +05:30
|
|
|
self.update_outcome(NonFatal);
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
inner.log(txt)
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
pub fn fatal(&self, txt: &str) {
|
2020-08-17 17:38:29 +05:30
|
|
|
self.update_outcome(Fatal);
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
inner.log(txt)
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
pub fn complete(&mut self) {
|
2020-08-17 17:38:29 +05:30
|
|
|
let mut inner = self.inner.lock().unwrap();
|
2020-08-17 20:06:21 +05:30
|
|
|
inner.complete();
|
2020-08-17 17:38:29 +05:30
|
|
|
}
|
2020-08-18 16:17:42 +05:30
|
|
|
|
|
|
|
pub fn get_outcome(&self) -> ReportOutcome {
|
|
|
|
let outcome = self.outcome.lock().unwrap();
|
|
|
|
outcome.clone()
|
|
|
|
}
|
2020-08-17 17:38:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-02-09 20:04:26 +05:30
|
|
|
#[allow(dead_code)]
|
2020-08-17 17:38:29 +05:30
|
|
|
struct PBInner {
|
2020-08-17 20:06:21 +05:30
|
|
|
title: String,
|
2020-08-17 17:38:29 +05:30
|
|
|
bar: ProgressBar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReportInner for PBInner {
|
2020-08-17 20:06:21 +05:30
|
|
|
fn set_title(&mut self, txt: &str) {
|
|
|
|
self.title = txt.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_sub_title(&mut self, txt: &str) {
|
|
|
|
//let mut fmt = "".to_string(); //Checking thin metadata".to_string(); //self.title.clone();
|
2020-08-17 17:38:29 +05:30
|
|
|
let mut fmt = "Checking thin metadata [{bar:40}] Remaining {eta}, ".to_string();
|
|
|
|
fmt.push_str(&txt);
|
|
|
|
self.bar.set_style(
|
|
|
|
ProgressStyle::default_bar()
|
|
|
|
.template(&fmt)
|
|
|
|
.progress_chars("=> "),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn progress(&mut self, percent: u8) {
|
2020-08-17 17:38:29 +05:30
|
|
|
self.bar.set_position(percent as u64);
|
|
|
|
self.bar.tick();
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn log(&mut self, txt: &str) {
|
2020-08-17 17:38:29 +05:30
|
|
|
self.bar.println(txt);
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn complete(&mut self) {
|
2020-08-17 17:38:29 +05:30
|
|
|
self.bar.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_progress_bar_report() -> Report {
|
|
|
|
Report::new(Box::new(PBInner {
|
2020-08-17 20:06:21 +05:30
|
|
|
title: "".to_string(),
|
2020-08-17 17:38:29 +05:30
|
|
|
bar: ProgressBar::new(100),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
struct SimpleInner {
|
|
|
|
last_progress: std::time::SystemTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SimpleInner {
|
|
|
|
fn new() -> SimpleInner {
|
|
|
|
SimpleInner {
|
|
|
|
last_progress: std::time::SystemTime::now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReportInner for SimpleInner {
|
2020-08-17 20:06:21 +05:30
|
|
|
fn set_title(&mut self, txt: &str) {
|
2020-11-04 18:08:35 +05:30
|
|
|
eprintln!("{}", txt);
|
2020-08-17 20:06:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fn set_sub_title(&mut self, txt: &str) {
|
2020-11-04 18:08:35 +05:30
|
|
|
eprintln!("{}", txt);
|
2020-08-17 17:38:29 +05:30
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn progress(&mut self, percent: u8) {
|
2020-08-17 17:38:29 +05:30
|
|
|
let elapsed = self.last_progress.elapsed().unwrap();
|
|
|
|
if elapsed > std::time::Duration::from_secs(5) {
|
2020-11-04 18:08:35 +05:30
|
|
|
eprintln!("Progress: {}%", percent);
|
2020-08-17 17:38:29 +05:30
|
|
|
self.last_progress = std::time::SystemTime::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn log(&mut self, txt: &str) {
|
2020-08-17 17:38:29 +05:30
|
|
|
eprintln!("{}", txt);
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn complete(&mut self) {}
|
2020-08-17 17:38:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_simple_report() -> Report {
|
|
|
|
Report::new(Box::new(SimpleInner::new()))
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
struct QuietInner {}
|
2020-08-17 17:38:29 +05:30
|
|
|
|
|
|
|
impl ReportInner for QuietInner {
|
2020-08-17 20:06:21 +05:30
|
|
|
fn set_title(&mut self, _txt: &str) {}
|
2020-08-17 17:38:29 +05:30
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn set_sub_title(&mut self, _txt: &str) {}
|
2020-08-17 17:38:29 +05:30
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn progress(&mut self, _percent: u8) {}
|
2020-08-17 17:38:29 +05:30
|
|
|
|
2020-08-17 20:06:21 +05:30
|
|
|
fn log(&mut self, _txt: &str) {}
|
|
|
|
|
|
|
|
fn complete(&mut self) {}
|
2020-08-17 17:38:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_quiet_report() -> Report {
|
|
|
|
Report::new(Box::new(QuietInner {}))
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|