parent
41083cd0dc
commit
8b91a26409
@ -0,0 +1 @@
|
|||||||
|
avril@flan-laptop.204627:1596751578
|
@ -0,0 +1,26 @@
|
|||||||
|
pub trait JoinStrsExt: Sized
|
||||||
|
{
|
||||||
|
/// Join an iterator of `str` with a seperator
|
||||||
|
fn join(self, with: &str) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T,I> JoinStrsExt for I
|
||||||
|
where I: Iterator<Item=T>,
|
||||||
|
T: AsRef<str>
|
||||||
|
{
|
||||||
|
fn join(self, with: &str) -> String
|
||||||
|
{
|
||||||
|
let mut output = String::new();
|
||||||
|
let mut first=true;
|
||||||
|
for string in self
|
||||||
|
{
|
||||||
|
if !first {
|
||||||
|
output.push_str(with);
|
||||||
|
}
|
||||||
|
let string = string.as_ref();
|
||||||
|
output.push_str(string);
|
||||||
|
first=false;
|
||||||
|
}
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
use super::*;
|
||||||
|
use std::{
|
||||||
|
iter::FromIterator as _,
|
||||||
|
};
|
||||||
|
use tokio::{
|
||||||
|
sync::{
|
||||||
|
oneshot,
|
||||||
|
watch,
|
||||||
|
},
|
||||||
|
task::{
|
||||||
|
self,
|
||||||
|
JoinHandle,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use termprogress::{
|
||||||
|
Display as _,
|
||||||
|
WithTitle,
|
||||||
|
ProgressBar,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum CommandKind
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Command
|
||||||
|
{
|
||||||
|
comm: CommandKind,
|
||||||
|
comp: oneshot::Sender<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ProgressSender
|
||||||
|
{
|
||||||
|
shutdown: watch::Receiver<Result<(), ()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create the async progress counter and return a sender object and a handle to join the worker task.
|
||||||
|
pub async fn create_progress<P: ProgressBar + WithTitle,
|
||||||
|
I: IntoIterator<Item=String>>(high: usize, tasks: I) -> (ProgressSender, JoinHandle<()>)
|
||||||
|
{
|
||||||
|
let list = task_list::TaskList::from_iter(tasks);
|
||||||
|
let mut progress = P::with_title(50, &list);
|
||||||
|
|
||||||
|
todo!()
|
||||||
|
}
|
@ -0,0 +1,118 @@
|
|||||||
|
use super::*;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
|
||||||
|
#[cfg(nightly)] type ListAbs<T> = std::collections::LinkedList<T>;
|
||||||
|
#[cfg(not(nightly))] type ListAbs<T> = Vec<T>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TaskList {
|
||||||
|
list: ListAbs<(usize, String)>,
|
||||||
|
buffer: String,
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find<T,F>(list: &ListAbs<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 {
|
||||||
|
list: ListAbs::new(),
|
||||||
|
buffer: String::new(),
|
||||||
|
index: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of tasks in the list
|
||||||
|
pub fn len(&self) -> usize
|
||||||
|
{
|
||||||
|
self.list.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Push a new task string, and return its ID.
|
||||||
|
pub fn push(&mut self, string: impl Into<String>) -> usize
|
||||||
|
{
|
||||||
|
let idx = {
|
||||||
|
self.index+=1;
|
||||||
|
self.index
|
||||||
|
};
|
||||||
|
#[cfg(nightly)] self.list.push_front((idx,string.into()));
|
||||||
|
#[cfg(not(nightly))] self.list.push((idx,string.into()));
|
||||||
|
|
||||||
|
self.recalc();
|
||||||
|
idx
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pop a task off the string, returns `true` if successful, `false` if wasn't found.
|
||||||
|
pub fn pop(&mut self, idx: usize) -> bool
|
||||||
|
{
|
||||||
|
if let Some(idx) = find(&self.list, |&(i, _)| idx == i)
|
||||||
|
{
|
||||||
|
self.list.remove(idx);
|
||||||
|
self.recalc();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear the `TaskList`
|
||||||
|
pub fn clear(&mut self)
|
||||||
|
{
|
||||||
|
self.list.clear();
|
||||||
|
self.buffer = String::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// As a single `str`
|
||||||
|
pub fn as_str(&self) -> &str
|
||||||
|
{
|
||||||
|
&self.buffer[..]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recalc(&mut self)
|
||||||
|
{
|
||||||
|
#[cfg(nightly)] {
|
||||||
|
self.buffer = self.list.iter().map(|(_, s)| s.as_str()).join(", ");
|
||||||
|
}
|
||||||
|
#[cfg(not(nightly))] {
|
||||||
|
self.buffer = self.list.iter().rev().map(|(_, s)| s.as_str()).join(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<str> for TaskList
|
||||||
|
{
|
||||||
|
#[inline] fn as_ref(&self) -> &str
|
||||||
|
{
|
||||||
|
self.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> FromIterator<T> for TaskList
|
||||||
|
where T: Into<String>
|
||||||
|
{
|
||||||
|
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self
|
||||||
|
{
|
||||||
|
let mut i=0usize;
|
||||||
|
let mut this = Self {
|
||||||
|
list: iter.into_iter().map(|x| ((i, x.into()), i+=1).0).collect(),
|
||||||
|
buffer: Default::default(),
|
||||||
|
index: 0,
|
||||||
|
};
|
||||||
|
this.index = i;
|
||||||
|
this.recalc();
|
||||||
|
this
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue