parent
184276f449
commit
5b975333f1
@ -0,0 +1 @@
|
||||
Remove Cargo.lock when termprogress is uploaded and Cargo.toml is properly formatted
|
@ -0,0 +1,225 @@
|
||||
use super::*;
|
||||
use tokio::{
|
||||
sync::{
|
||||
mpsc::{
|
||||
channel,
|
||||
unbounded_channel,
|
||||
Sender,
|
||||
Receiver,
|
||||
UnboundedReceiver,
|
||||
UnboundedSender
|
||||
},
|
||||
Mutex,
|
||||
},
|
||||
task::{
|
||||
self,
|
||||
JoinHandle,
|
||||
},
|
||||
};
|
||||
|
||||
use termprogress::{
|
||||
spinner,
|
||||
Display,
|
||||
Spinner,
|
||||
};
|
||||
|
||||
use std::{
|
||||
sync::Arc,
|
||||
fmt,
|
||||
};
|
||||
|
||||
enum CommandInternal
|
||||
{
|
||||
PrintLine(String),
|
||||
Bump,
|
||||
Kill(Option<String>),
|
||||
PushTask(String),
|
||||
PopTask(String),
|
||||
}
|
||||
|
||||
struct Command
|
||||
{
|
||||
internal: CommandInternal,
|
||||
callback: UnboundedSender<()>,
|
||||
}
|
||||
|
||||
impl std::ops::Drop for Command
|
||||
{
|
||||
fn drop(&mut self)
|
||||
{
|
||||
let _ = self.callback.send(());
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsyncProgressCounter
|
||||
{
|
||||
writer: Sender<Command>,
|
||||
reader: Receiver<Command>,
|
||||
title: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CommandSender(Sender<Command>);
|
||||
|
||||
pub struct CommandCallback(UnboundedReceiver<()>);
|
||||
|
||||
pub mod error;
|
||||
|
||||
impl Command
|
||||
{
|
||||
pub fn new(internal: CommandInternal) -> (Self, CommandCallback)
|
||||
{
|
||||
let (tx, rx) = unbounded_channel();
|
||||
(Self {
|
||||
internal,
|
||||
callback: tx,
|
||||
}, CommandCallback(rx))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! prog_send {
|
||||
(try link unwind $expression:expr) => {
|
||||
{
|
||||
$expression.await.expect("fatal").wait().await.expect("fatal")
|
||||
}
|
||||
};
|
||||
(try link $expression:expr) => {
|
||||
{
|
||||
$expression.await?.wait().await?
|
||||
}
|
||||
};
|
||||
(try unind $expression:expr) => {
|
||||
{
|
||||
let _ = $expression.await.expect("fatal");
|
||||
}
|
||||
};
|
||||
(try $expression:expr) => {
|
||||
{
|
||||
let _ =$expression.await?;
|
||||
}
|
||||
};
|
||||
($expression:expr) => {
|
||||
{
|
||||
let _ = $expression.await;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl CommandSender
|
||||
{
|
||||
/// Print a line
|
||||
pub async fn println(&mut self, string: impl Into<String>) -> Result<CommandCallback, error::Error>
|
||||
{
|
||||
let (com, call) = Command::new(CommandInternal::PrintLine(string.into()));
|
||||
self.0.send(com).await?;
|
||||
Ok(call)
|
||||
}
|
||||
|
||||
/// Finalise the progress counter
|
||||
pub async fn bump(&mut self) -> Result<CommandCallback, error::Error>
|
||||
{
|
||||
let (com, call) = Command::new(CommandInternal::Bump);
|
||||
self.0.send(com).await?;
|
||||
Ok(call)
|
||||
}
|
||||
|
||||
/// Finalise the progress counter
|
||||
pub async fn kill(&mut self) -> Result<CommandCallback, error::Error>
|
||||
{
|
||||
let (com, call) = Command::new(CommandInternal::Kill(None));
|
||||
self.0.send(com).await?;
|
||||
Ok(call)
|
||||
}
|
||||
|
||||
/// Finalise the progress counter and print a line
|
||||
pub async fn kill_with(&mut self, string: String) -> Result<CommandCallback, error::Error>
|
||||
{
|
||||
let (com, call) = Command::new(CommandInternal::Kill(Some(string)));
|
||||
self.0.send(com).await?;
|
||||
Ok(call)
|
||||
}
|
||||
|
||||
|
||||
/// Remove a task
|
||||
pub async fn pop_task(&mut self, string: impl Into<String>) -> Result<CommandCallback, error::Error>
|
||||
{
|
||||
let (com, call) = Command::new(CommandInternal::PopTask(string.into()));
|
||||
self.0.send(com).await?;
|
||||
Ok(call)
|
||||
}
|
||||
|
||||
/// Add a task
|
||||
pub async fn push_task(&mut self, string: impl Into<String>) -> Result<CommandCallback, error::Error>
|
||||
{
|
||||
let (com, call) = Command::new(CommandInternal::PushTask(string.into()));
|
||||
self.0.send(com).await?;
|
||||
Ok(call)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandCallback
|
||||
{
|
||||
/// Wait until the command has been processed.
|
||||
pub async fn wait(mut self) -> Result<(), error::Error>
|
||||
{
|
||||
self.0.recv().await.ok_or(error::Error::RecvError)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncProgressCounter
|
||||
{
|
||||
/// Create a new `AsyncProgressCounter`
|
||||
pub fn new(title: impl Into<String>) -> Self
|
||||
{
|
||||
let (tx, rx) = channel(16);
|
||||
|
||||
Self {
|
||||
reader: rx,
|
||||
writer: tx,
|
||||
title: title.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new writer
|
||||
pub fn writer(&self) -> CommandSender
|
||||
{
|
||||
CommandSender(self.writer.clone())
|
||||
}
|
||||
|
||||
/// Consume the instance and host it's receiver
|
||||
pub fn host(mut self) -> JoinHandle<()>
|
||||
{
|
||||
let mut spin = spinner::Spin::with_title(&self.title[..], Default::default());
|
||||
|
||||
let mut task = tasklist::TaskList::new();
|
||||
|
||||
task::spawn(async move {
|
||||
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::Kill(Some(line)) => {
|
||||
spin.complete_with(line.as_str());
|
||||
self.reader.close();
|
||||
break;
|
||||
},
|
||||
CommandInternal::Kill(_) => {
|
||||
spin.complete();
|
||||
self.reader.close();
|
||||
break;
|
||||
},
|
||||
CommandInternal::PushTask(tstr) => {
|
||||
task.push(tstr);
|
||||
spin.set_title(task.as_str());
|
||||
},
|
||||
CommandInternal::PopTask(tstr) => {
|
||||
task.pop_value(tstr);
|
||||
spin.set_title(task.as_str());
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
use super::*;
|
||||
use tokio::{
|
||||
sync::{
|
||||
mpsc::{
|
||||
error::SendError,
|
||||
},
|
||||
},
|
||||
};
|
||||
use std::{
|
||||
fmt,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error
|
||||
{
|
||||
SendError,
|
||||
RecvError,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl std::error::Error for Error{}
|
||||
impl fmt::Display for Error
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "sync (fatal): ")?;
|
||||
match self {
|
||||
Error::SendError => write!(f, "there was an mpsc send error"),
|
||||
Error::RecvError => write!(f, "there was an mpsc recv error"),
|
||||
_ => write!(f, "unknown error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<SendError<T>> for Error
|
||||
{
|
||||
fn from(_er: SendError<T>) -> Self
|
||||
{
|
||||
Error::SendError
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
|
||||
use super::*;
|
||||
use std::{
|
||||
collections::LinkedList,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct TaskList(LinkedList<(usize, String)>, String, usize);
|
||||
|
||||
fn find<T,F>(list: &LinkedList<T>, mut fun: F) -> Option<usize>
|
||||
where F: FnMut(&T) -> bool
|
||||
{
|
||||
for (i, x) in (0..).zip(list.iter())
|
||||
{
|
||||
if fun(x) {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
impl TaskList
|
||||
{
|
||||
/// Create a new tasklist
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self(LinkedList::new(), String::new(), 0)
|
||||
}
|
||||
|
||||
pub fn push(&mut self, string: impl Into<String>) -> usize
|
||||
{
|
||||
let idx = {
|
||||
self.2 += 1;
|
||||
self.2
|
||||
};
|
||||
self.0.push_front((idx, string.into()));
|
||||
self.recalc_buffer();
|
||||
idx
|
||||
}
|
||||
|
||||
pub fn pop(&mut self, idx: usize) -> bool
|
||||
{
|
||||
if let Some(idx) = find(&self.0, |&(i, _)| idx == i)
|
||||
{
|
||||
self.0.remove(idx);
|
||||
self.recalc_buffer();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_value(&mut self, string: impl AsRef<str>) -> bool
|
||||
{
|
||||
let string = string.as_ref();
|
||||
if let Some(idx) = find(&self.0, |(_, s)| s.as_str() == string)
|
||||
{
|
||||
self.0.remove(idx);
|
||||
self.recalc_buffer();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recalc_buffer(&mut self)
|
||||
{
|
||||
self.1 = self.0.iter().map(|(_, s)| s.as_str()).collect();
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str
|
||||
{
|
||||
&self.1[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for TaskList
|
||||
{
|
||||
fn as_ref(&self) -> &str
|
||||
{
|
||||
self.as_str()
|
||||
}
|
||||
}
|
Loading…
Reference in new issue