|
|
|
@ -31,10 +31,12 @@ use std::{
|
|
|
|
|
enum CommandInternal
|
|
|
|
|
{
|
|
|
|
|
PrintLine(String),
|
|
|
|
|
Bump,
|
|
|
|
|
BumpMax(u64),
|
|
|
|
|
Bump(u64),
|
|
|
|
|
Kill(Option<String>),
|
|
|
|
|
PushTask(String),
|
|
|
|
|
PopTask(String),
|
|
|
|
|
ClearTask(Option<String>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Change from `Spinner` to `Bar`. Have push max, push current, etc.
|
|
|
|
@ -58,6 +60,9 @@ pub struct AsyncProgressCounter
|
|
|
|
|
writer: Sender<Command>,
|
|
|
|
|
reader: Receiver<Command>,
|
|
|
|
|
title: String,
|
|
|
|
|
|
|
|
|
|
small: u64,
|
|
|
|
|
large: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
@ -148,14 +153,23 @@ impl CommandSender
|
|
|
|
|
Ok(call)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finalise the progress counter
|
|
|
|
|
pub async fn bump(&mut self) -> Result<CommandCallback, error::Error>
|
|
|
|
|
/// Bump the progress counter's upper bound
|
|
|
|
|
pub async fn bump_max(&mut self, by: u64) -> Result<CommandCallback, error::Error>
|
|
|
|
|
{
|
|
|
|
|
let (com, call) = Command::new(CommandInternal::BumpMax(by));
|
|
|
|
|
self.0.send(com).await?;
|
|
|
|
|
Ok(call)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Bump the progress counter's upper bound
|
|
|
|
|
pub async fn bump(&mut self, by: u64) -> Result<CommandCallback, error::Error>
|
|
|
|
|
{
|
|
|
|
|
let (com, call) = Command::new(CommandInternal::Bump);
|
|
|
|
|
let (com, call) = Command::new(CommandInternal::Bump(by));
|
|
|
|
|
self.0.send(com).await?;
|
|
|
|
|
Ok(call)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Finalise the progress counter
|
|
|
|
|
pub async fn kill(&mut self) -> Result<CommandCallback, error::Error>
|
|
|
|
|
{
|
|
|
|
@ -188,6 +202,22 @@ impl CommandSender
|
|
|
|
|
self.0.send(com).await?;
|
|
|
|
|
Ok(call)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Clear all tasks
|
|
|
|
|
pub async fn clear_task(&mut self) -> Result<CommandCallback, error::Error>
|
|
|
|
|
{
|
|
|
|
|
let (com, call) = Command::new(CommandInternal::ClearTask(None));
|
|
|
|
|
self.0.send(com).await?;
|
|
|
|
|
Ok(call)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Clear all tasks and set new title
|
|
|
|
|
pub async fn set_title(&mut self, title: impl Into<String>) -> Result<CommandCallback, error::Error>
|
|
|
|
|
{
|
|
|
|
|
let (com, call) = Command::new(CommandInternal::ClearTask(Some(title.into())));
|
|
|
|
|
self.0.send(com).await?;
|
|
|
|
|
Ok(call)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CommandCallback
|
|
|
|
@ -202,7 +232,7 @@ impl CommandCallback
|
|
|
|
|
impl AsyncProgressCounter
|
|
|
|
|
{
|
|
|
|
|
/// Create a new `AsyncProgressCounter`
|
|
|
|
|
pub fn new(title: impl Into<String>) -> Self
|
|
|
|
|
pub fn new(title: impl Into<String>, max: u64) -> Self
|
|
|
|
|
{
|
|
|
|
|
let (tx, rx) = channel(16);
|
|
|
|
|
|
|
|
|
@ -210,6 +240,8 @@ impl AsyncProgressCounter
|
|
|
|
|
reader: rx,
|
|
|
|
|
writer: tx,
|
|
|
|
|
title: title.into(),
|
|
|
|
|
small: 0,
|
|
|
|
|
large: max,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -224,37 +256,64 @@ impl AsyncProgressCounter
|
|
|
|
|
CommandSender(self.writer.clone(), None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Consume the instance and host it's receiver
|
|
|
|
|
/// Consume the instance and host its receiver
|
|
|
|
|
pub fn host(mut self) -> JoinHandle<()>
|
|
|
|
|
{
|
|
|
|
|
let mut spin = spinner::Spin::with_title(&self.title[..], Default::default());
|
|
|
|
|
//let mut spin = spinner::Spin::with_title(&self.title[..], Default::default());
|
|
|
|
|
let mut bar = termprogress::progress::Bar::with_title(50, &self.title[..]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bar.update();
|
|
|
|
|
let mut task = tasklist::TaskList::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
task::spawn(async move {
|
|
|
|
|
|
|
|
|
|
let mut small = 0;//self.small;
|
|
|
|
|
let mut large = 0;
|
|
|
|
|
|
|
|
|
|
fn prog_calc(small: u64, large: u64) -> f64 {
|
|
|
|
|
(small as f64) / (large as f64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while let Some(com) = self.reader.recv().await {
|
|
|
|
|
match &com.internal {
|
|
|
|
|
CommandInternal::PrintLine(line) => spin.println(&line[..]),
|
|
|
|
|
CommandInternal::Bump => {
|
|
|
|
|
spin.bump(); //TODO: We'll have to change this when we change to real progress
|
|
|
|
|
CommandInternal::PrintLine(line) => bar.println(&line[..]),
|
|
|
|
|
CommandInternal::BumpMax(by) => {
|
|
|
|
|
large += by;
|
|
|
|
|
bar.set_progress(prog_calc(small,large));
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::Bump(by) => {
|
|
|
|
|
small += by;
|
|
|
|
|
bar.set_progress(prog_calc(small,large));
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::Kill(Some(line)) => {
|
|
|
|
|
spin.complete_with(line.as_str());
|
|
|
|
|
bar.println(&line[..]);
|
|
|
|
|
bar.complete();
|
|
|
|
|
self.reader.close();
|
|
|
|
|
break;
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::Kill(_) => {
|
|
|
|
|
spin.complete();
|
|
|
|
|
bar.complete();
|
|
|
|
|
self.reader.close();
|
|
|
|
|
break;
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::PushTask(tstr) => {
|
|
|
|
|
task.push(tstr);
|
|
|
|
|
spin.set_title(task.as_str());
|
|
|
|
|
bar.set_title(task.as_str());
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::PopTask(tstr) => {
|
|
|
|
|
task.pop_value(tstr);
|
|
|
|
|
spin.set_title(task.as_str());
|
|
|
|
|
bar.set_title(task.as_str());
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::ClearTask(None) => {
|
|
|
|
|
task.clear();
|
|
|
|
|
bar.set_title(&self.title[..]);
|
|
|
|
|
},
|
|
|
|
|
CommandInternal::ClearTask(Some(title)) => {
|
|
|
|
|
task.clear();
|
|
|
|
|
bar.set_title(&title[..]);
|
|
|
|
|
self.title = title.clone();
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|