Add curt progressor and progressor selection
This commit is contained in:
+21
-5
@@ -5,7 +5,7 @@ use std::io::IsTerminal;
|
|||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use zfsbackup::job::JobBuilder;
|
use zfsbackup::job::JobBuilder;
|
||||||
use zfsbackup::progress::{Progressor, log, terminal};
|
use zfsbackup::progress::{Progressor, curt, log, terminal};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
@@ -29,6 +29,9 @@ struct Args {
|
|||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
retain: Option<usize>,
|
retain: Option<usize>,
|
||||||
|
|
||||||
|
#[arg(short, long)]
|
||||||
|
progressor: Option<String>,
|
||||||
|
|
||||||
datasets: Vec<String>,
|
datasets: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,10 +52,23 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut pr: Box<dyn Progressor> = if std::io::stdout().is_terminal() {
|
let mut pr: Box<dyn Progressor> = match args.progressor {
|
||||||
Box::new(terminal::Progressor::new(rx))
|
Some(name) => match name.as_str() {
|
||||||
} else {
|
"curt" => Box::new(curt::Progressor::new(rx)),
|
||||||
Box::new(log::Progressor::new(rx))
|
"terminal" => Box::new(terminal::Progressor::new(rx)),
|
||||||
|
"log" => Box::new(log::Progressor::new(rx)),
|
||||||
|
_ => {
|
||||||
|
eprintln!("progressor {} not found, defaulting to 'log'", name);
|
||||||
|
Box::new(log::Progressor::new(rx))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
if std::io::stdout().is_terminal() {
|
||||||
|
Box::new(terminal::Progressor::new(rx))
|
||||||
|
} else {
|
||||||
|
Box::new(log::Progressor::new(rx))
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let handle = thread::spawn(move || pr.run());
|
let handle = thread::spawn(move || pr.run());
|
||||||
builder = builder.sender(tx);
|
builder = builder.sender(tx);
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
use super::{BackupEvent, human_bytes};
|
||||||
|
use std::io::Write;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
|
|
||||||
|
pub struct Progressor {
|
||||||
|
receiver: Receiver<BackupEvent>,
|
||||||
|
source: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl super::Progressor for Progressor {
|
||||||
|
fn run(&mut self) {
|
||||||
|
while let Ok(event) = self.receiver.recv() {
|
||||||
|
match event {
|
||||||
|
BackupEvent::Estimate(_) => {}
|
||||||
|
BackupEvent::FullBackupStart {
|
||||||
|
source,
|
||||||
|
dest: _,
|
||||||
|
index: _,
|
||||||
|
total: _,
|
||||||
|
} => {
|
||||||
|
self.source = source;
|
||||||
|
print!("{}\r", self.source);
|
||||||
|
}
|
||||||
|
BackupEvent::IncrementalBackupStart {
|
||||||
|
source,
|
||||||
|
dest: _,
|
||||||
|
index: _,
|
||||||
|
total: _,
|
||||||
|
} => {
|
||||||
|
self.source = source;
|
||||||
|
print!("{}\r", self.source);
|
||||||
|
}
|
||||||
|
BackupEvent::SnapshotCreate(_) => {}
|
||||||
|
BackupEvent::SnapshotDelete(_) => {}
|
||||||
|
BackupEvent::BytesTransferred {
|
||||||
|
bytes,
|
||||||
|
estimated_total,
|
||||||
|
} => {
|
||||||
|
match estimated_total {
|
||||||
|
Some(total) => {
|
||||||
|
let percent: f64 = if total > 0 {
|
||||||
|
bytes as f64 / total as f64
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
|
print!("\x1b[2K{}: {:>3.0}%\r", self.source, percent * 100.0,);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
print!(
|
||||||
|
"\x1b[2K{}: {} transferred\r",
|
||||||
|
self.source,
|
||||||
|
human_bytes(bytes as f64)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::io::stdout().flush().ok();
|
||||||
|
}
|
||||||
|
BackupEvent::SendComplete(bytes) => {
|
||||||
|
print!(
|
||||||
|
"\x1b[2K{}: send complete ({})\r",
|
||||||
|
self.source,
|
||||||
|
human_bytes(bytes as f64)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
BackupEvent::DatasetComplete(_) => {
|
||||||
|
println!("\x1b[2K{}: complete", self.source);
|
||||||
|
}
|
||||||
|
BackupEvent::DryrunComplete(_) => {
|
||||||
|
println!("\x1b[2K{}: complete", self.source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Progressor {
|
||||||
|
pub fn new(receiver: Receiver<BackupEvent>) -> Self {
|
||||||
|
Self {
|
||||||
|
receiver,
|
||||||
|
source: String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod curt;
|
||||||
pub mod filter;
|
pub mod filter;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
mod rate;
|
mod rate;
|
||||||
|
|||||||
Reference in New Issue
Block a user