diff --git a/src/env.rs b/src/env.rs index 65572da..d6917f7 100644 --- a/src/env.rs +++ b/src/env.rs @@ -155,6 +155,10 @@ impl Env { pub(crate) fn err_is_term(&self) -> bool { self.err_is_term } + + pub(crate) fn err_style(&self) -> Style { + self.err_style + } } #[cfg(test)] diff --git a/src/err.rs b/src/err.rs index 0b5f2bd..79594aa 100644 --- a/src/err.rs +++ b/src/err.rs @@ -1,8 +1,8 @@ macro_rules! err { ($env:expr, $fmt:expr) => { - write!($env.err, $fmt).context(crate::error::Stderr)?; + write!($env.err, $fmt).context(crate::error::Stderr) }; ($env:expr, $fmt:expr, $($arg:tt)*) => { - write!($env.err, $fmt, $($arg)*).context(crate::error::Stderr)?; + write!($env.err, $fmt, $($arg)*).context(crate::error::Stderr) }; } diff --git a/src/errln.rs b/src/errln.rs index 0e6e1c8..cda980d 100644 --- a/src/errln.rs +++ b/src/errln.rs @@ -1,11 +1,11 @@ macro_rules! errln { ($env:expr) => { - writeln!($env.err, "").context(crate::error::Stderr)?; + writeln!($env.err, "").context(crate::error::Stderr) }; ($env:expr, $fmt:expr) => { - writeln!($env.err, $fmt).context(crate::error::Stderr)?; + writeln!($env.err, $fmt).context(crate::error::Stderr) }; ($env:expr, $fmt:expr, $($arg:tt)*) => { - writeln!($env.err, $fmt, $($arg)*).context(crate::error::Stderr)?; + writeln!($env.err, $fmt, $($arg)*).context(crate::error::Stderr) }; } diff --git a/src/outln.rs b/src/outln.rs index 3be286b..dd7f94b 100644 --- a/src/outln.rs +++ b/src/outln.rs @@ -1,11 +1,11 @@ macro_rules! outln { ($env:expr) => { - writeln!($env.out, "").context(crate::error::Stderr)?; + writeln!($env.out, "").context(crate::error::Stderr) }; ($env:expr, $fmt:expr) => { - writeln!($env.out, $fmt).context(crate::error::Stderr)?; + writeln!($env.out, $fmt).context(crate::error::Stderr) }; ($env:expr, $fmt:expr, $($arg:tt)*) => { - writeln!($env.out, $fmt, $($arg)*).context(crate::error::Stderr)?; + writeln!($env.out, $fmt, $($arg)*).context(crate::error::Stderr) }; } diff --git a/src/style.rs b/src/style.rs index d4e00dc..cdc08f3 100644 --- a/src/style.rs +++ b/src/style.rs @@ -35,4 +35,12 @@ impl Style { ansi_term::Style::new() } } + + pub(crate) fn dim(self) -> ansi_term::Style { + if self.active { + ansi_term::Style::new().dimmed() + } else { + ansi_term::Style::new() + } + } } diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index 34d819d..94ce01c 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -203,7 +203,7 @@ impl Create { announce_list.push(tier); } - errln!(env, "[1/3] \u{1F9FF} Searching for files…"); + Step::Searching.print(env)?; let spinner = if env.err_is_term() { let style = ProgressStyle::default_spinner() @@ -295,7 +295,7 @@ impl Create { Some(String::from(consts::CREATED_BY_DEFAULT)) }; - errln!(env, "[2/3] \u{1F9EE} Hashing pieces…"); + Step::Hashing.print(env)?; let progress_bar = if env.err_is_term() { let style = ProgressStyle::default_bar() @@ -318,7 +318,7 @@ impl Create { progress_bar, )?; - errln!(env, "[3/3] \u{1F4BE} Writing metainfo to {}…", output); + Step::Writing { output: &output }.print(env)?; let info = Info { source: self.source, @@ -381,7 +381,7 @@ impl Create { } } - errln!(env, "\u{2728}\u{2728} Done! \u{2728}\u{2728}"); + errln!(env, "\u{2728}\u{2728} Done! \u{2728}\u{2728}")?; if self.show { TorrentSummary::from_metainfo(metainfo)?.write(env)?; @@ -418,6 +418,43 @@ impl Create { } } +#[derive(Clone, Copy)] +enum Step<'output> { + Searching, + Hashing, + Writing { output: &'output OutputTarget }, +} + +impl<'output> Step<'output> { + fn print(self, env: &mut Env) -> Result<(), Error> { + let style = env.err_style(); + let dim = style.dim(); + let message = style.message(); + + err!(env, "{}[{}/3]{} ", dim.prefix(), self.n(), dim.suffix())?; + + err!(env, "{}", message.prefix())?; + + match self { + Self::Searching => err!(env, "\u{1F9FF} Searching for files…")?, + Self::Hashing => err!(env, "\u{1F9EE} Hashing pieces…")?, + Self::Writing { output } => err!(env, "\u{1F4BE} Writing metainfo to {}…", output)?, + } + + errln!(env, "{}", message.suffix())?; + + Ok(()) + } + + fn n(self) -> usize { + match self { + Self::Searching => 1, + Self::Hashing => 2, + Self::Writing { .. } => 3, + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/subcommand/torrent/piece_length.rs b/src/subcommand/torrent/piece_length.rs index f8700ff..e122e6a 100644 --- a/src/subcommand/torrent/piece_length.rs +++ b/src/subcommand/torrent/piece_length.rs @@ -59,7 +59,7 @@ impl PieceLength { w1 = w.1, w2 = w.2, w3 = w.3, - ); + )?; } Ok(()) diff --git a/src/subcommand/torrent/stats.rs b/src/subcommand/torrent/stats.rs index e6e0b0c..448d805 100644 --- a/src/subcommand/torrent/stats.rs +++ b/src/subcommand/torrent/stats.rs @@ -61,13 +61,13 @@ impl Stats { extractor.process(entry.path()); } - errln!(env, "Torrents processed: {}", extractor.torrents); - errln!(env, "Read failed: {}", extractor.io_errors); + errln!(env, "Torrents processed: {}", extractor.torrents)?; + errln!(env, "Read failed: {}", extractor.io_errors)?; errln!( env, "Decode failed: {}", extractor.bencode_decode_errors - ); + )?; let mut paths = extractor.paths.into_iter().collect::>(); paths.sort_by_key(|(_, count)| Reverse(*count)); @@ -75,16 +75,16 @@ impl Stats { let width = max.to_string().len(); if !paths.is_empty() { - errln!(env, "Keys:"); + errln!(env, "Keys:")?; for (key, count) in &paths { if key.starts_with("info/files") { continue; } - errln!(env, "{:)>>(); - errln!(env, "Values:"); + errln!(env, "Values:")?; for (pattern, values) in values { - err!(env, "{}: ", pattern); + err!(env, "{}: ", pattern)?; for (i, value) in values.iter().enumerate() { if i > 0 { - err!(env, ", "); + err!(env, ", ")?; } - err!(env, "{}", value); + err!(env, "{}", value)?; } - errln!(env) + errln!(env)?; } } diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index 3748229..6f08e75 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -40,7 +40,7 @@ impl Verify { let status = metainfo.verify(&base)?; if status.good() { - errln!(env, "Verification succeeded."); + errln!(env, "Verification succeeded.")?; Ok(()) } else { Err(Error::Verify { status })