Style imdl torrent create progress messages

type: changed
This commit is contained in:
Casey Rodarmor 2020-03-12 18:00:52 -07:00
parent 8c0d918607
commit 2edf8a4fab
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
9 changed files with 74 additions and 25 deletions

View File

@ -155,6 +155,10 @@ impl Env {
pub(crate) fn err_is_term(&self) -> bool { pub(crate) fn err_is_term(&self) -> bool {
self.err_is_term self.err_is_term
} }
pub(crate) fn err_style(&self) -> Style {
self.err_style
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,8 +1,8 @@
macro_rules! err { macro_rules! err {
($env:expr, $fmt:expr) => { ($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)*) => { ($env:expr, $fmt:expr, $($arg:tt)*) => {
write!($env.err, $fmt, $($arg)*).context(crate::error::Stderr)?; write!($env.err, $fmt, $($arg)*).context(crate::error::Stderr)
}; };
} }

View File

@ -1,11 +1,11 @@
macro_rules! errln { macro_rules! errln {
($env:expr) => { ($env:expr) => {
writeln!($env.err, "").context(crate::error::Stderr)?; writeln!($env.err, "").context(crate::error::Stderr)
}; };
($env:expr, $fmt:expr) => { ($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)*) => { ($env:expr, $fmt:expr, $($arg:tt)*) => {
writeln!($env.err, $fmt, $($arg)*).context(crate::error::Stderr)?; writeln!($env.err, $fmt, $($arg)*).context(crate::error::Stderr)
}; };
} }

View File

@ -1,11 +1,11 @@
macro_rules! outln { macro_rules! outln {
($env:expr) => { ($env:expr) => {
writeln!($env.out, "").context(crate::error::Stderr)?; writeln!($env.out, "").context(crate::error::Stderr)
}; };
($env:expr, $fmt:expr) => { ($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)*) => { ($env:expr, $fmt:expr, $($arg:tt)*) => {
writeln!($env.out, $fmt, $($arg)*).context(crate::error::Stderr)?; writeln!($env.out, $fmt, $($arg)*).context(crate::error::Stderr)
}; };
} }

View File

@ -35,4 +35,12 @@ impl Style {
ansi_term::Style::new() 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()
}
}
} }

View File

@ -203,7 +203,7 @@ impl Create {
announce_list.push(tier); 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 spinner = if env.err_is_term() {
let style = ProgressStyle::default_spinner() let style = ProgressStyle::default_spinner()
@ -295,7 +295,7 @@ impl Create {
Some(String::from(consts::CREATED_BY_DEFAULT)) 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 progress_bar = if env.err_is_term() {
let style = ProgressStyle::default_bar() let style = ProgressStyle::default_bar()
@ -318,7 +318,7 @@ impl Create {
progress_bar, progress_bar,
)?; )?;
errln!(env, "[3/3] \u{1F4BE} Writing metainfo to {}…", output); Step::Writing { output: &output }.print(env)?;
let info = Info { let info = Info {
source: self.source, 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 { if self.show {
TorrentSummary::from_metainfo(metainfo)?.write(env)?; 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -59,7 +59,7 @@ impl PieceLength {
w1 = w.1, w1 = w.1,
w2 = w.2, w2 = w.2,
w3 = w.3, w3 = w.3,
); )?;
} }
Ok(()) Ok(())

View File

@ -61,13 +61,13 @@ impl Stats {
extractor.process(entry.path()); extractor.process(entry.path());
} }
errln!(env, "Torrents processed: {}", extractor.torrents); errln!(env, "Torrents processed: {}", extractor.torrents)?;
errln!(env, "Read failed: {}", extractor.io_errors); errln!(env, "Read failed: {}", extractor.io_errors)?;
errln!( errln!(
env, env,
"Decode failed: {}", "Decode failed: {}",
extractor.bencode_decode_errors extractor.bencode_decode_errors
); )?;
let mut paths = extractor.paths.into_iter().collect::<Vec<(String, u64)>>(); let mut paths = extractor.paths.into_iter().collect::<Vec<(String, u64)>>();
paths.sort_by_key(|(_, count)| Reverse(*count)); paths.sort_by_key(|(_, count)| Reverse(*count));
@ -75,16 +75,16 @@ impl Stats {
let width = max.to_string().len(); let width = max.to_string().len();
if !paths.is_empty() { if !paths.is_empty() {
errln!(env, "Keys:"); errln!(env, "Keys:")?;
for (key, count) in &paths { for (key, count) in &paths {
if key.starts_with("info/files") { if key.starts_with("info/files") {
continue; continue;
} }
errln!(env, "{:<width$} - {}", count, key, width = width); errln!(env, "{:<width$} - {}", count, key, width = width)?;
} }
for (key, count) in paths { for (key, count) in paths {
if key.starts_with("info/files") { if key.starts_with("info/files") {
errln!(env, "{:<width$} - {}", count, key, width = width); errln!(env, "{:<width$} - {}", count, key, width = width)?;
} }
} }
} }
@ -95,16 +95,16 @@ impl Stats {
.into_iter() .into_iter()
.collect::<Vec<(String, Vec<String>)>>(); .collect::<Vec<(String, Vec<String>)>>();
errln!(env, "Values:"); errln!(env, "Values:")?;
for (pattern, values) in values { for (pattern, values) in values {
err!(env, "{}: ", pattern); err!(env, "{}: ", pattern)?;
for (i, value) in values.iter().enumerate() { for (i, value) in values.iter().enumerate() {
if i > 0 { if i > 0 {
err!(env, ", "); err!(env, ", ")?;
} }
err!(env, "{}", value); err!(env, "{}", value)?;
} }
errln!(env) errln!(env)?;
} }
} }

View File

@ -40,7 +40,7 @@ impl Verify {
let status = metainfo.verify(&base)?; let status = metainfo.verify(&base)?;
if status.good() { if status.good() {
errln!(env, "Verification succeeded."); errln!(env, "Verification succeeded.")?;
Ok(()) Ok(())
} else { } else {
Err(Error::Verify { status }) Err(Error::Verify { status })