From 6e64806b2f9bd77ec98296b4306c615880894a1c Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 8 Jul 2023 16:25:49 +0200 Subject: add multithreading to merge and generate --- src/gui/mod.rs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'src/gui/mod.rs') diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 5677db1..389fdb6 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -1,4 +1,3 @@ -use std::io::Write; use std::thread; use std::time::Duration; @@ -13,12 +12,14 @@ use std::sync::Mutex; use crate::index::Index; use crate::searchresult::SearchResult; use crate::splitter; +use crate::index::GenState; mod easing; mod circular; static SEARCH_ID: Lazy = Lazy::new(text_input::Id::unique); static GENERATE_PROGRESS : Lazy> = Lazy::new(|| { Mutex::new(0) }); +static GENERATE_STATUS : Lazy> = Lazy::new(|| { Mutex::new(GenState::Fetching) }); pub fn run() -> iced::Result { App::run(Settings { @@ -33,16 +34,11 @@ pub fn run() -> iced::Result { #[derive(Debug)] enum App { StartMenu, - Generating(GenState), + Generating(GenProgress), Loading, Search(State), } -#[derive(Debug, Default)] -struct GenState { - progress: u8, -} - #[derive(Debug, Default)] struct State { input_value: String, @@ -55,6 +51,12 @@ struct SearchState { index : Index, } +#[derive(Debug, Clone, Default)] +struct GenProgress { + state: GenState, + progress: u8 +} + impl SearchState { pub async fn search(self) -> Vec { let search_args = splitter::split_to_words(self.search.clone()); @@ -67,7 +69,7 @@ enum Message { Load, Generate, Loaded(Index), - GeneratingUpdate(u8), + GeneratingUpdate(GenProgress), InputChanged(String), SearchSubmit, SearchFinished(Vec) @@ -100,22 +102,23 @@ async fn generate() -> Index { let input = input.unwrap(); let input = input.to_str(); let input = input.unwrap(); - let index = Index::generate(input, |counter, nof| { - let p = ((counter * 100) / nof) as u8; + let index = Index::generate(input, |t, p| { *GENERATE_PROGRESS.lock().unwrap() = p; - std::io::stdout().flush().ok(); + *GENERATE_STATUS.lock().unwrap() = t; }); index.save(file.to_string()); index } -async fn generate_update_timer() -> u8 { +async fn generate_update_timer() -> GenProgress { thread::sleep(Duration::from_millis(100)); let p; + let s; { p = *GENERATE_PROGRESS.lock().unwrap(); + s = *GENERATE_STATUS.lock().unwrap(); } - p + GenProgress { state : s, progress: p } } impl Application for App { @@ -174,8 +177,9 @@ impl Application for App { *self = App::Search(State { index, ..Default::default() }); Command::none() } - Message::GeneratingUpdate(p) => { - state.progress = p; + Message::GeneratingUpdate(s) => { + state.progress = s.progress; + state.state = s.state; Command::perform(generate_update_timer(), Message::GeneratingUpdate) } _ => { @@ -225,8 +229,13 @@ impl Application for App { ].spacing(10).align_items(iced::Alignment::Center) } App::Generating(state) => { + let t = match state.state { + GenState::Fetching => { text("Fetching") } + GenState::Parsing => { text("Parsing") } + GenState::Merging => { text("Merging") } + }; column![ - text("Generating"), + t, progress_bar(0.0..=100.0, state.progress as f32) ].spacing(10).align_items(iced::Alignment::Center) } -- cgit v1.2.3-70-g09d2