Add curt progressor and progressor selection

This commit is contained in:
2026-06-03 10:50:13 +02:00
parent 0e52fd8fde
commit 3251a97e1a
3 changed files with 106 additions and 5 deletions
+18 -2
View File
@@ -5,7 +5,7 @@ use std::io::IsTerminal;
use std::sync::mpsc::channel;
use std::thread;
use zfsbackup::job::JobBuilder;
use zfsbackup::progress::{Progressor, log, terminal};
use zfsbackup::progress::{Progressor, curt, log, terminal};
#[derive(Parser)]
#[command(version, about, long_about = None)]
@@ -29,6 +29,9 @@ struct Args {
#[arg(short, long)]
retain: Option<usize>,
#[arg(short, long)]
progressor: Option<String>,
datasets: Vec<String>,
}
@@ -49,10 +52,23 @@ fn main() -> Result<(), Box<dyn Error>> {
}
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 {
Some(name) => match name.as_str() {
"curt" => Box::new(curt::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());
builder = builder.sender(tx);
+84
View File
@@ -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
View File
@@ -1,3 +1,4 @@
pub mod curt;
pub mod filter;
pub mod log;
mod rate;