aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-07-06 10:25:41 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2023-07-06 10:25:41 +0200
commit7aad1e8f5ecdef2bd8317a538350d43ba00d048d (patch)
treee20c36bdf3cf3adff6ea61f9e9d3301bb8c22838
parent851f175eb33a77f6442b2843194d7ad30e4e1f1c (diff)
add filecounter
-rw-r--r--src/filecounter.rs10
-rw-r--r--src/main.rs47
2 files changed, 45 insertions, 12 deletions
diff --git a/src/filecounter.rs b/src/filecounter.rs
new file mode 100644
index 0000000..1ed58e3
--- /dev/null
+++ b/src/filecounter.rs
@@ -0,0 +1,10 @@
+use walkdir::*;
+
+pub fn filecount(path : &str) -> u64 {
+ let mut counter : u64 = 0;
+ for _ in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
+ counter += 1;
+ }
+
+ counter
+}
diff --git a/src/main.rs b/src/main.rs
index f0975ad..5275321 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,38 +4,61 @@ pub mod text;
pub mod splitter;
pub mod filecache;
pub mod searchresult;
+pub mod filecounter;
use vector::FileVector;
use dictionary::Dictionary;
use filecache::FileCache;
use searchresult::SearchResult;
+use filecounter::filecount;
use std::fs::File;
use std::io::{Write, BufReader, BufRead};
use walkdir::*;
+use std::thread;
+use std::option::Option::None;
fn generate_index(input_path : &str, index_path : &str) {
let mut index_file = File::create(index_path).unwrap();
let mut dict = Dictionary::new();
+ let mut nof = 0;
+ let mut counter = 0;
- for entry in WalkDir::new(input_path).into_iter().filter_map(|e| e.ok()) {
- if entry.path().is_file() {
- let content : String = text::extract_text(entry.path().to_str().unwrap());
- if !content.is_empty() {
+ thread::scope(|s| {
+ let mut nof_handle : Option<_> = Some(s.spawn(|| filecount(input_path)));
+
+ for entry in WalkDir::new(input_path).into_iter().filter_map(|e| e.ok()) {
+ if entry.path().is_file() {
+ let content : String = text::extract_text(entry.path().to_str().unwrap());
let words : Vec<String> = splitter::split_to_words(content);
for word in words.iter() {
- let w = word.clone();
- dict.set(w);
+ dict.set(word.clone());
}
- let fv : FileVector = dict.vectorize_word_list(words);
+ let fv = dict.vectorize_word_list(words.clone());
writeln!(index_file, "{}, {}", entry.path().to_str().unwrap().replace(",", "\0"), fv.to_hex()).ok();
+
+ counter += 1;
+
+
+ match nof_handle {
+ Some(t) => {
+ nof = t.join().unwrap();
+ nof_handle = None;
+ }
+ None => {
+ print!("\r\x1b[2K{} of {} files indexed ({}%)", counter, nof, (counter * 100) / nof);
+ std::io::stdout().flush().ok();
+ }
+ }
}
}
- }
- let dict_list : Vec<String> = dict.to_list();
- writeln!(index_file, "#{}", dict_list.join(",")).ok();
+ println!("\r\x1b[2K{} of {} files indexed ({}%)", counter, nof, (counter * 100) / nof);
+
+ let dict_list : Vec<String> = dict.to_list();
+ writeln!(index_file, "#{}", dict_list.join(",")).ok();
+ });
}
fn search(index_path : &str, search_args : Vec<&str>) {
@@ -82,6 +105,6 @@ fn search(index_path : &str, search_args : Vec<&str>) {
fn main() {
println!("Generating Index...");
generate_index("/home/n8", "index.idxs");
- println!("Searching...");
- search("index.idxs", vec!["one", "difficult", "under", "linux"]);
+ //println!("Searching...");
+ //search("index.idxs", vec!["one", "difficult", "under", "linux"]);
}