diff --git a/src/bytes.rs b/src/bytes.rs index aa0f5d1..3d062a8 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -1,53 +1,5 @@ use crate::common::*; -impl Div for Bytes { - type Output = u64; - - fn div(self, rhs: Bytes) -> u64 { - self.0 / rhs.0 - } -} - -impl Div for Bytes { - type Output = Bytes; - - fn div(self, rhs: u64) -> Bytes { - Bytes::from(self.0 / rhs) - } -} - -impl Mul for Bytes { - type Output = Bytes; - - fn mul(self, rhs: u64) -> Self { - Bytes::from(self.0 * rhs) - } -} - -impl DivAssign for Bytes { - fn div_assign(&mut self, rhs: u64) { - self.0 /= rhs; - } -} - -impl MulAssign for Bytes { - fn mul_assign(&mut self, rhs: u64) { - self.0 *= rhs; - } -} - -impl AddAssign for Bytes { - fn add_assign(&mut self, rhs: Bytes) { - self.0 += rhs.0; - } -} - -impl SubAssign for Bytes { - fn sub_assign(&mut self, rhs: u64) { - self.0 -= rhs; - } -} - const KI: u64 = 1 << 10; const MI: u64 = KI << 10; const GI: u64 = MI << 10; @@ -141,6 +93,54 @@ impl FromStr for Bytes { } } +impl Div for Bytes { + type Output = u64; + + fn div(self, rhs: Bytes) -> u64 { + self.0 / rhs.0 + } +} + +impl Div for Bytes { + type Output = Bytes; + + fn div(self, rhs: u64) -> Bytes { + Bytes::from(self.0 / rhs) + } +} + +impl Mul for Bytes { + type Output = Bytes; + + fn mul(self, rhs: u64) -> Self { + Bytes::from(self.0 * rhs) + } +} + +impl DivAssign for Bytes { + fn div_assign(&mut self, rhs: u64) { + self.0 /= rhs; + } +} + +impl MulAssign for Bytes { + fn mul_assign(&mut self, rhs: u64) { + self.0 *= rhs; + } +} + +impl AddAssign for Bytes { + fn add_assign(&mut self, rhs: Bytes) { + self.0 += rhs.0; + } +} + +impl SubAssign for Bytes { + fn sub_assign(&mut self, rhs: u64) { + self.0 -= rhs; + } +} + impl Sum for Bytes { fn sum(iter: I) -> Self where diff --git a/src/hasher.rs b/src/hasher.rs index 0b574f6..9893158 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -8,6 +8,7 @@ pub(crate) struct Hasher { piece_length: usize, pieces: PieceList, sha1: Sha1, + progress_bar: ProgressBar, } impl Hasher { @@ -15,11 +16,12 @@ impl Hasher { files: &Files, md5sum: bool, piece_length: usize, + progress_bar: ProgressBar, ) -> Result<(Mode, PieceList), Error> { - Self::new(md5sum, piece_length).hash_files(files) + Self::new(md5sum, piece_length, progress_bar).hash_files(files) } - fn new(md5sum: bool, piece_length: usize) -> Self { + fn new(md5sum: bool, piece_length: usize, progress_bar: ProgressBar) -> Self { Self { buffer: vec![0; piece_length], length: 0, @@ -28,6 +30,7 @@ impl Hasher { sha1: Sha1::new(), piece_length, md5sum, + progress_bar, } } @@ -122,6 +125,8 @@ impl Hasher { } remaining -= buffer.len().into_u64(); + + self.progress_bar.inc(to_buffer.into_u64()); } self.length += length; diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index e407744..663cc9c 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -199,7 +199,7 @@ impl Create { errln!(env, "[1/3] \u{1F9FF} Searching for files…"); - let style = ProgressStyle::default_spinner().template("{spinner} {msg}…"); + let style = ProgressStyle::default_spinner().template("{spinner:.green} {msg:.bold}…"); let spinner = ProgressBar::new_spinner().with_style(style); @@ -277,10 +277,20 @@ impl Create { errln!(env, "[2/3] \u{1F9EE} Hashing pieces…"); + let style = ProgressStyle::default_bar() + .template( + "{spinner:.green} [{elapsed_precise}] \u{2588}{bar:40.cyan/blue}\u{2588} \ + {bytes}/{total_bytes} ({bytes_per_sec}, {eta})", + ) + .progress_chars("\u{2593}\u{2592}\u{2591}"); + + let progress_bar = ProgressBar::new(files.total_size().count()).with_style(style); + let (mode, pieces) = Hasher::hash( &files, self.md5sum, piece_length.as_piece_length()?.into_usize(), + progress_bar, )?; errln!(env, "[3/3] \u{1F4BE} Writing metainfo to {}…", output);