From 61ac9375b4a35878576ac2727c5210cd9fc51a92 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 26 Jul 2023 21:19:59 +0200 Subject: add gtk3 gui --- src/index.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/index.rs') diff --git a/src/index.rs b/src/index.rs index cc86f0c..be23e0e 100644 --- a/src/index.rs +++ b/src/index.rs @@ -16,7 +16,7 @@ use crate::vector; /// or read from a file. #[derive(Clone, Debug)] pub struct Index { - filecache : Vec, + pub filecache : Vec, } impl Default for Index { @@ -69,7 +69,7 @@ impl Index { if content.is_empty() { result_tx.send(FileCache { - path: "".to_string(), + path: "[is_empty]".to_string(), vector : FileVector::default() }).ok(); continue; @@ -78,7 +78,7 @@ impl Index { let words : Vec = splitter::split_to_words(content); let fv = FileVector::from_words(words); result_tx.send(FileCache { - path: "".to_string(), + path: path.to_string(), vector : fv }).ok(); } @@ -128,7 +128,7 @@ impl Index { Self { filecache } } - pub fn search(&self, search_args : Vec) -> Vec { + pub fn search(&self, search_args : Vec, callback : impl Fn(u8)) -> Vec { let mut v : HashMap = HashMap::new(); let mut opt : HashMap = HashMap::new(); @@ -149,13 +149,20 @@ impl Index { let mut results : Vec = Vec::new(); - for filecache in self.filecache.iter() { + let mut last_p = 0; + for (i, filecache) in self.filecache.iter().enumerate() { let mut r = SearchResult { priority : 0, path : filecache.path.clone() }; r.priority = vector::match_vector(&v, &filecache.vector); if r.priority > 0 { r.priority += vector::scalar_product(&opt, &filecache.vector); results.push(r); } + + let p = i * 100 / self.filecache.len(); + if last_p < p { + callback(p as u8); + last_p = p; + } } results.sort_by(|a, b| b.priority.cmp(&a.priority)); results @@ -170,4 +177,8 @@ impl Index { pub fn num_files(&self) -> usize { self.filecache.len() } + + pub fn import(&mut self, index : Index) { + self.filecache = index.filecache; + } } -- cgit v1.2.3-70-g09d2