progress
Avril 4 years ago
parent 88defc530e
commit faece04d4e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -27,9 +27,10 @@ use std::{
}; };
mod tasklist; mod tasklist;
pub use tasklist::TaskId;
/// Command to send to worker task. /// Command to send to worker task.
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum CommandKind pub enum CommandKind
{ {
Line(String), Line(String),
@ -38,6 +39,9 @@ pub enum CommandKind
Bump(isize), Bump(isize),
BumpHigh(isize), BumpHigh(isize),
AddTask(String),
RemoveTask(TaskId),
Shutdown, Shutdown,
Many(Vec<CommandKind>), Many(Vec<CommandKind>),
@ -219,6 +223,7 @@ pub fn host<B: ProgressBar + Send + Sync + 'static>(bar: B) -> (Handle<B>, JoinH
}; };
(handle, tokio::spawn(async move { (handle, tokio::spawn(async move {
({ ({
let mut tasks = tasklist::TaskList::new();
macro_rules! update_bar { macro_rules! update_bar {
(to $state:ident $($tt:tt)*) => { (to $state:ident $($tt:tt)*) => {
{ {
@ -243,6 +248,27 @@ pub fn host<B: ProgressBar + Send + Sync + 'static>(bar: B) -> (Handle<B>, JoinH
update_bar!($($tt)*); 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 { while let Some(Command(command, response)) = rx.recv().await {
@ -261,10 +287,13 @@ pub fn host<B: ProgressBar + Send + Sync + 'static>(bar: B) -> (Handle<B>, JoinH
($value:expr) => (send_response!(@ response => Some(Box::new($value)))); ($value:expr) => (send_response!(@ response => Some(Box::new($value))));
(@ $response:ident => $value:expr) => { (@ $response:ident => $value:expr) => {
{ {
if let Some(response) = $response.lock().unwrap().take() { let value: Response = $value;
Some(response.send($value)) {
} else { if let Some(response) = $response.lock().unwrap().take() {
None Some(response.send(value))
} else {
None
}
} }
} }
}; };
@ -306,6 +335,13 @@ pub fn host<B: ProgressBar + Send + Sync + 'static>(bar: B) -> (Handle<B>, JoinH
CommandKind::Line(line) => update_bar!(write line), CommandKind::Line(line) => update_bar!(write line),
CommandKind::LineErr(line) => update_bar!(write error 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 //TODO: Title
CommandKind::Many(_) => unimplemented!(), CommandKind::Many(_) => unimplemented!(),
} }

@ -65,10 +65,11 @@ impl TaskList
} }
/// Add a task to the end of the list /// 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<String>) -> TaskId
{ {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
let task = task.into();
self.push_buf_one(&task[..]); self.push_buf_one(&task[..]);
self.tasks.push_back((id.clone(), task)); self.tasks.push_back((id.clone(), task));

Loading…
Cancel
Save