From faece04d4e4541eee7625c5909dade3ea1c35860 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 5 Nov 2020 18:48:25 +0000 Subject: [PATCH] tasklist --- src/progress/mod.rs | 46 +++++++++++++++++++++++++++++++++++----- src/progress/tasklist.rs | 3 ++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/progress/mod.rs b/src/progress/mod.rs index 9bf59bf..f43ae1e 100644 --- a/src/progress/mod.rs +++ b/src/progress/mod.rs @@ -27,9 +27,10 @@ use std::{ }; mod tasklist; +pub use tasklist::TaskId; /// Command to send to worker task. -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum CommandKind { Line(String), @@ -38,6 +39,9 @@ pub enum CommandKind Bump(isize), BumpHigh(isize), + AddTask(String), + RemoveTask(TaskId), + Shutdown, Many(Vec), @@ -219,6 +223,7 @@ pub fn host(bar: B) -> (Handle, JoinH }; (handle, tokio::spawn(async move { ({ + let mut tasks = tasklist::TaskList::new(); macro_rules! update_bar { (to $state:ident $($tt:tt)*) => { { @@ -243,6 +248,27 @@ pub fn host(bar: B) -> (Handle, JoinH update_bar!($($tt)*); } }; + (title $($tt:tt)*) => { + { + let mut bar = bar.write().await; + bar.set_title(tasks.as_str()); + update_bar!($($tt)*); + } + }; + (+task $task:ident $($tt:tt)*) => { + { + let id = tasks.add($task); + update_bar!($($tt)*); + id + } + }; + (-task $id:ident $($tt:tt)*) => { + { + let res = tasks.remove(&$id); + update_bar!($($tt)*); + res + } + }; () => {}; } while let Some(Command(command, response)) = rx.recv().await { @@ -261,10 +287,13 @@ pub fn host(bar: B) -> (Handle, JoinH ($value:expr) => (send_response!(@ response => Some(Box::new($value)))); (@ $response:ident => $value:expr) => { { - if let Some(response) = $response.lock().unwrap().take() { - Some(response.send($value)) - } else { - None + let value: Response = $value; + { + if let Some(response) = $response.lock().unwrap().take() { + Some(response.send(value)) + } else { + None + } } } }; @@ -306,6 +335,13 @@ pub fn host(bar: B) -> (Handle, JoinH CommandKind::Line(line) => update_bar!(write line), CommandKind::LineErr(line) => update_bar!(write error line), + CommandKind::AddTask(string) => { + send_response!(update_bar!(+task string title)); + }, + CommandKind::RemoveTask(id) => { + send_response!(update_bar!(-task id title)); + }, + //TODO: Title CommandKind::Many(_) => unimplemented!(), } diff --git a/src/progress/tasklist.rs b/src/progress/tasklist.rs index 99ebfdd..ef65937 100644 --- a/src/progress/tasklist.rs +++ b/src/progress/tasklist.rs @@ -65,10 +65,11 @@ impl TaskList } /// Add a task to the end of the list - pub fn add(&mut self, task: String) -> TaskId + pub fn add(&mut self, task: impl Into) -> TaskId { let id = Uuid::new_v4(); + let task = task.into(); self.push_buf_one(&task[..]); self.tasks.push_back((id.clone(), task));