Skip to content

Commit 441119d

Browse files
bors[bot]Razaloc
andauthored
Merge #44
44: Implement progress bar r=obbardc a=Razaloc Display a progress bar for the copy process Closes: #33 Signed-off-by: Rafael Garcia Ruiz <rafael.garcia@collabora.com> Co-authored-by: Rafael Garcia Ruiz <rafael.garcia@collabora.com>
2 parents a5e2b86 + 4aa1ef0 commit 441119d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

bmap-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ anyhow = "1.0.66"
1212
nix = "0.25.0"
1313
flate2 = "1.0.24"
1414
clap = { version = "4.0.18", features = ["derive"] }
15+
indicatif = "0.17.1"

bmap-rs/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use anyhow::{anyhow, bail, Context, Result};
22
use bmap::{Bmap, Discarder, SeekForward};
33
use clap::Parser;
44
use flate2::read::GzDecoder;
5+
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
56
use nix::unistd::ftruncate;
67
use std::ffi::OsStr;
8+
use std::fmt::Write;
79
use std::fs::File;
810
use std::io::Read;
911
use std::os::unix::io::AsRawFd;
@@ -100,7 +102,7 @@ fn copy(c: Copy) -> Result<()> {
100102
b.read_to_string(&mut xml)?;
101103

102104
let bmap = Bmap::from_xml(&xml)?;
103-
let mut output = std::fs::OpenOptions::new()
105+
let output = std::fs::OpenOptions::new()
104106
.write(true)
105107
.create(true)
106108
.open(c.dest)?;
@@ -111,7 +113,14 @@ fn copy(c: Copy) -> Result<()> {
111113
}
112114

113115
let mut input = setup_input(&c.image)?;
114-
bmap::copy(&mut input, &mut output, &bmap)?;
116+
let pb = ProgressBar::new(bmap.total_mapped_size());
117+
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
118+
.unwrap()
119+
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
120+
.progress_chars("#>-"));
121+
bmap::copy(&mut input, &mut pb.wrap_write(&output), &bmap)?;
122+
pb.finish_and_clear();
123+
115124
println!("Done: Syncing...");
116125
output.sync_all().expect("Sync failure");
117126

bmap/src/bmap.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ impl Bmap {
9292
pub fn block_map(&self) -> impl ExactSizeIterator + Iterator<Item = &BlockRange> {
9393
self.blockmap.iter()
9494
}
95+
pub fn total_mapped_size(&self) -> u64 {
96+
self.block_size * self.mapped_blocks
97+
}
9598
}
9699

97100
#[derive(Clone, Debug, Error)]

0 commit comments

Comments
 (0)