diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-07-07 13:35:16 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-07-07 13:35:50 +0200 |
commit | 434c66a517f086e24f1885cfff04fe8c00bd98a7 (patch) | |
tree | 939babd1c600c7bfe04b53af64c8f881f514c4d1 /src | |
parent | 38e008d788231660f5a4c7790c18016f8e3bc7f9 (diff) |
Move State from loadinglist to util module, remove Progress type
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/loadinglist/list.rs | 99 | ||||
-rw-r--r-- | src/loadinglist/mod.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/util.rs | 46 |
4 files changed, 12 insertions, 136 deletions
diff --git a/src/loadinglist/list.rs b/src/loadinglist/list.rs deleted file mode 100644 index afbb55d..0000000 --- a/src/loadinglist/list.rs +++ /dev/null @@ -1,99 +0,0 @@ -use std::fmt::Debug; -use std::sync::Mutex; -use async_std::sync::TryRecvError; -use async_std::sync::Receiver; -use cursive::{View, Printer}; -use cursive::view::IntoBoxedView; - -pub struct LoadingList<D, RG> - where D: Debug + Sized, - RG: RowGenerator<D>, -{ - progress: cursive::views::ProgressBar, - view: Mutex<cursive::views::ListView>, - - receiver: Receiver<State<D>>, - generator: RG, - ready: Mutex<bool>, -} - -#[derive(Debug)] -pub enum State<D: Debug + Sized> { - Processing(D), - Ready -} - -impl<D> State<D> - where D: Debug + Sized -{ - pub fn processing(d: D) -> Self { - State::Processing(d) - } - - pub fn ready() -> Self { - State::Ready - } -} - - -impl<D, RG> LoadingList<D, RG> - where D: Debug + Sized, - RG: RowGenerator<D>, -{ - pub fn new(receiver: Receiver<State<D>>, generator: RG) -> Self { - LoadingList { - progress: cursive::views::ProgressBar::new().min(0), - view: Mutex::new(cursive::views::ListView::new()), - receiver, - generator, - ready: Mutex::new(false) - } - } -} - -pub trait RowGenerator<D> - where Self: Debug, - D: Debug + Sized, -{ - type Output: 'static + IntoBoxedView; - fn generate_row(&self, data: D) -> Self::Output; -} - -impl<D, RG> View for LoadingList<D, RG> - where D: Debug + Sized + 'static, - RG: RowGenerator<D> + 'static, -{ - fn draw(&self, printer: &Printer) { - if self.ready.lock().map(|g| *g).unwrap() { - self.view - .lock() - .map(|guard| { - guard.draw(printer); - }); - } else { - match self.receiver.try_recv() { - Err(_) => { - debug!("Received nothing"); - self.progress.draw(printer) - } - - Ok(State::Processing(data)) => { - let view = self.generator.generate_row(data); - self.view.lock().map(|mut guard| { - guard.add_child("", view); - }); - self.progress.draw(printer) - }, - Ok(State::Ready) => { - self.ready.lock().map(|mut g| *g = true); - self.view - .lock() - .map(|guard| { - guard.draw(printer); - }); - } - } - } - } -} - diff --git a/src/loadinglist/mod.rs b/src/loadinglist/mod.rs deleted file mode 100644 index d17e233..0000000 --- a/src/loadinglist/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod list; diff --git a/src/main.rs b/src/main.rs index 8530467..26f9a47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use env_logger::Env; mod mailstore; mod sidebar; mod main_view; -mod loadinglist; +mod util; fn main() -> Result<()> { cursive::logger::init(); diff --git a/src/util.rs b/src/util.rs index 19b50fe..1abb071 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,45 +1,21 @@ use std::fmt::Debug; -#[derive(Clone, Debug)] -pub struct Progress -{ - current: usize, - complete: usize, - is_complete: bool, +#[derive(Debug)] +pub enum State<D: Debug + Sized> { + Processing(D), + Ready } -impl Progress +impl<D> State<D> + where D: Debug + Sized { - pub fn with_complete(complete: usize) -> Progress { - Progress { current: 0, complete, is_complete: false } - } - - pub fn is(current: usize, complete: usize) -> Progress { - Progress { current, complete, current == complete } - } - - pub fn new_complete() -> Progress { - Progress { current: 0, complete: 0, is_complete: true } - } - - pub fn is_complete(&self) -> bool { - self.is_complete + pub fn processing(d: D) -> Self { + State::Processing(d) } - pub fn set_complete(&mut self, c: bool) { - self.is_complete = c; - } - - pub fn step(&mut self) { - self.current += 1; - } - - pub fn current(&self) -> usize { - self.current - } - - pub fn complete(&self) -> usize { - self.complete + pub fn ready() -> Self { + State::Ready } } + |