diff --git a/src/work_async.rs b/src/work_async.rs index 191d959..17824a0 100644 --- a/src/work_async.rs +++ b/src/work_async.rs @@ -67,7 +67,7 @@ pub async fn work(conf: config::Config) -> Result<(), Box for path in conf.output.into_iter() { let url = url::parse(&rating); - let mut prog = prog_writer.clone(); + let mut prog = prog_writer.clone_with(format!("-> {:?}", path)); children.push(tokio::task::spawn(async move { //println!("Starting download ({})...", url); diff --git a/src/work_async/progress.rs b/src/work_async/progress.rs index b49c4ea..1a3d702 100644 --- a/src/work_async/progress.rs +++ b/src/work_async/progress.rs @@ -61,7 +61,7 @@ pub struct AsyncProgressCounter } #[derive(Clone)] -pub struct CommandSender(Sender); +pub struct CommandSender(Sender, Option); pub struct CommandCallback(UnboundedReceiver<()>); @@ -116,10 +116,34 @@ macro_rules! prog_send { impl CommandSender { + /// Clone with a new opt string + pub fn clone_with(&self, opt: impl Into) -> Self + { + Self ( + self.0.clone(), + Some(opt.into()) + ) + } + + fn opt_str(&self, string: impl AsRef) -> String + { + if let Some(opt) = &self.1 { + format!("[{}] {}", opt, string.as_ref()) + } else { + string.as_ref().into() + } + } + + /// Get or set an opt string + pub fn opt(&mut self) -> &mut Option + { + &mut self.1 + } + /// Print a line - pub async fn println(&mut self, string: impl Into) -> Result + pub async fn println(&mut self, string: impl AsRef) -> Result { - let (com, call) = Command::new(CommandInternal::PrintLine(string.into())); + let (com, call) = Command::new(CommandInternal::PrintLine(self.opt_str(string))); self.0.send(com).await?; Ok(call) } @@ -141,9 +165,9 @@ impl CommandSender } /// Finalise the progress counter and print a line - pub async fn kill_with(&mut self, string: String) -> Result + pub async fn kill_with(&mut self, string: impl AsRef) -> Result { - let (com, call) = Command::new(CommandInternal::Kill(Some(string))); + let (com, call) = Command::new(CommandInternal::Kill(Some(self.opt_str(string)))); self.0.send(com).await?; Ok(call) } @@ -188,11 +212,16 @@ impl AsyncProgressCounter title: title.into(), } } - + + /// Create a new writer with an opt string + pub fn writer_with(&self, opt: impl Into) -> CommandSender + { + CommandSender(self.writer.clone(), Some(opt.into())) + } /// Create a new writer pub fn writer(&self) -> CommandSender { - CommandSender(self.writer.clone()) + CommandSender(self.writer.clone(), None) } /// Consume the instance and host it's receiver diff --git a/src/work_async/tasklist.rs b/src/work_async/tasklist.rs index 0daba12..e8c4619 100644 --- a/src/work_async/tasklist.rs +++ b/src/work_async/tasklist.rs @@ -65,7 +65,7 @@ impl TaskList pub fn recalc_buffer(&mut self) { - self.1 = self.0.iter().map(|(_, s)| s.as_str()).collect(); + self.1 = self.0.iter().map(|(_, s)| s.as_str()).collect(); //TODO: Iterator extension join method for strings } pub fn as_str(&self) -> &str