Skip to content

Commit

Permalink
Add piece hashing progress bar
Browse files Browse the repository at this point in the history
type: added
  • Loading branch information
rrybarczyk authored and casey committed Apr 8, 2020
1 parent c6cd78f commit bdaec27
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
96 changes: 48 additions & 48 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,5 @@
use crate::common::*;

impl Div<Bytes> for Bytes {
type Output = u64;

fn div(self, rhs: Bytes) -> u64 {
self.0 / rhs.0
}
}

impl Div<u64> for Bytes {
type Output = Bytes;

fn div(self, rhs: u64) -> Bytes {
Bytes::from(self.0 / rhs)
}
}

impl Mul<u64> for Bytes {
type Output = Bytes;

fn mul(self, rhs: u64) -> Self {
Bytes::from(self.0 * rhs)
}
}

impl DivAssign<u64> for Bytes {
fn div_assign(&mut self, rhs: u64) {
self.0 /= rhs;
}
}

impl MulAssign<u64> for Bytes {
fn mul_assign(&mut self, rhs: u64) {
self.0 *= rhs;
}
}

impl AddAssign<Bytes> for Bytes {
fn add_assign(&mut self, rhs: Bytes) {
self.0 += rhs.0;
}
}

impl SubAssign<u64> 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;
Expand Down Expand Up @@ -141,6 +93,54 @@ impl FromStr for Bytes {
}
}

impl Div<Bytes> for Bytes {
type Output = u64;

fn div(self, rhs: Bytes) -> u64 {
self.0 / rhs.0
}
}

impl Div<u64> for Bytes {
type Output = Bytes;

fn div(self, rhs: u64) -> Bytes {
Bytes::from(self.0 / rhs)
}
}

impl Mul<u64> for Bytes {
type Output = Bytes;

fn mul(self, rhs: u64) -> Self {
Bytes::from(self.0 * rhs)
}
}

impl DivAssign<u64> for Bytes {
fn div_assign(&mut self, rhs: u64) {
self.0 /= rhs;
}
}

impl MulAssign<u64> for Bytes {
fn mul_assign(&mut self, rhs: u64) {
self.0 *= rhs;
}
}

impl AddAssign<Bytes> for Bytes {
fn add_assign(&mut self, rhs: Bytes) {
self.0 += rhs.0;
}
}

impl SubAssign<u64> for Bytes {
fn sub_assign(&mut self, rhs: u64) {
self.0 -= rhs;
}
}

impl Sum for Bytes {
fn sum<I>(iter: I) -> Self
where
Expand Down
9 changes: 7 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ pub(crate) struct Hasher {
piece_length: usize,
pieces: PieceList,
sha1: Sha1,
progress_bar: ProgressBar,
}

impl Hasher {
pub(crate) fn hash(
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,
Expand All @@ -28,6 +30,7 @@ impl Hasher {
sha1: Sha1::new(),
piece_length,
md5sum,
progress_bar,
}
}

Expand Down Expand Up @@ -122,6 +125,8 @@ impl Hasher {
}

remaining -= buffer.len().into_u64();

self.progress_bar.inc(to_buffer.into_u64());
}

self.length += length;
Expand Down
12 changes: 11 additions & 1 deletion src/subcommand/torrent/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bdaec27

Please sign in to comment.