Skip to content

Commit

Permalink
Merge pull request #22 from naaive/traditional_chinese
Browse files Browse the repository at this point in the history
Traditional chinese
  • Loading branch information
naaive authored Apr 5, 2022
2 parents 302e45c + 38cb222 commit 889b2f3
Show file tree
Hide file tree
Showing 17 changed files with 642 additions and 167 deletions.
422 changes: 418 additions & 4 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "app"
version = "0.1.0"
version = "0.6.0"
description = "A Tauri App"
authors = ["you"]
license = ""
Expand Down Expand Up @@ -45,7 +45,7 @@ jwalk = "0.6.0"
convert_case = "0.1.0"
lazy_static = "1.4.0"
webbrowser = "0.6.0"

zhconv = "0.1.0-beta.2"
[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
Expand All @@ -54,5 +54,5 @@ default = [ "custom-protocol" ]
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]

#[profile.test]
#opt-level=3
[profile.test]
opt-level=3
6 changes: 5 additions & 1 deletion src-tauri/src/file_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ pub struct FileView {
pub size: u64,
pub is_dir: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SearchResult {
pub file_view: Vec<FileView>,
pub tokenized: String,
}
#[test]
fn t1() {
let file = FileView {
Expand Down
48 changes: 37 additions & 11 deletions src-tauri/src/idx_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use tantivy::query::{BooleanQuery, FuzzyTermQuery, Occur, QueryParser, TermQuery
use tantivy::schema::*;
use tantivy::{doc, Index, IndexReader, IndexWriter, ReloadPolicy};

use crate::file_view::FileView;
use crate::file_view::{FileView, SearchResult};
use crate::utils;
use crate::utils::is_ascii_alphanumeric;
use zhconv::{zhconv, Variant};

lazy_static! {
pub static ref IDX_STORE: IdxStore = {
Expand Down Expand Up @@ -47,7 +48,11 @@ impl IdxStore {
return self.ascii_tokenize(hans);
}
let space = " ";
let hans = hans.replace("-", space).replace("_", space);
let hans = hans
.replace("-", space)
.replace("+", space)
.replace("_", space);
let hans = zhconv(&hans, Variant::ZhHans);
let words = self.tokenizer.cut(&hans, false);

let mut token_text: HashSet<String> = vec![].into_iter().collect();
Expand All @@ -56,12 +61,23 @@ impl IdxStore {
token_text.insert(word.to_string());
}
token_text.insert(hans.clone());

token_text.into_iter().collect::<Vec<String>>().join(" ")
}

pub fn search_tokenized(&self, hans: String) -> String {
let s = self.search_tokenize(hans);
let t = zhconv(&s, Variant::ZhHant);
format!("{} {}", s, t)
}

fn ascii_tokenize(&self, asc: String) -> String {
let string = asc.to_case(Case::Title).to_lowercase();
return format!("{} {}", string, asc.to_lowercase());
let title_lowercase = asc.to_case(Case::Title).to_lowercase();
let raw_lowercase = asc.to_lowercase();
// if title_lowercase.eq(&raw_lowercase) {
// return title_lowercase;
// }
return format!("{} {}", title_lowercase, raw_lowercase);
}
pub fn tokenize(&self, hans: String) -> String {
// return hans;
Expand All @@ -71,6 +87,8 @@ impl IdxStore {
}
let space = " ";
let hans = hans.replace("-", space).replace("_", space);
let hans = zhconv(&hans, Variant::ZhHans);

let words = self.tokenizer.cut(&hans, false);

let mut token_text: HashSet<String> = vec![].into_iter().collect();
Expand Down Expand Up @@ -132,14 +150,11 @@ impl IdxStore {
limit: usize,
is_dir_opt: Option<bool>,
ext_opt: Option<String>,
) -> Vec<FileView> {
) -> SearchResult {
let searcher = self.reader.searcher();

let kw_query = self
.query_parser
.parse_query(&self.search_tokenize(kw))
.ok()
.unwrap();
let tokens = self.search_tokenize(kw.clone());
let kw_query = self.query_parser.parse_query(&tokens).ok().unwrap();
let mut subqueries = vec![(Occur::Must, kw_query)];

if let Some(is_dir) = is_dir_opt {
Expand Down Expand Up @@ -190,7 +205,10 @@ impl IdxStore {
// }
let file_views = self.parse_file_views(paths);

file_views
SearchResult {
file_view: file_views,
tokenized: self.search_tokenized(kw),
}
}

pub fn suggest(&self, kw: String, limit: usize) -> Vec<FileView> {
Expand Down Expand Up @@ -488,4 +506,12 @@ mod tests {
let string = idx_store.tokenize("DataPatchController.java".to_string());
println!("{}", string);
}

#[test]
fn t6() {
let hans = zhconv("安全浏览器", Variant::ZhHans);
println!("{}", hans);
let hans = zhconv("安全瀏覽器", Variant::ZhHans);
println!("{}", hans);
}
}
8 changes: 4 additions & 4 deletions src-tauri/src/indexing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::idx_store::IDX_STORE;
use crate::kv_store::CONF_STORE;
use crate::walk_metrics::WALK_METRICS;
use crate::{utils, walk_exec, watch_exec};
use log::info;

Expand All @@ -22,7 +23,7 @@ use crate::usn_journal_watcher::Watcher;
const STORE_PATH: &'static str = "orangecachedata";
#[cfg(windows)]
const RECYCLE_PATH: &'static str = "$RECYCLE.BIN";
const VERSION: &'static str = "0.4.0";
const VERSION: &'static str = "0.6.0";
const LAST_INDEX_TS: &'static str = "last_index_ts";

pub fn run() {
Expand All @@ -43,19 +44,18 @@ fn do_run() {
};

IDX_STORE.disable_full_indexing();

WALK_METRICS.write().unwrap().end_of_no_reindex();
info!("start fs watch");

#[cfg(windows)]
if cfg!(target_os = "windows") {
if reindex {
info!("use watcher due to reindex");
watch_exec::run();
}else {
} else {
info!("try use usn");
win_watch();
}

}
#[cfg(unix)]
watch_exec::run();
Expand Down
12 changes: 5 additions & 7 deletions src-tauri/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::file_view::FileView;

use crate::file_view::{FileView, SearchResult};
use crate::idx_store::IDX_STORE;
use log::info;

use crate::walk_metrics::WalkMatrixView;
use crate::{indexing, utils, walk_exec};
Expand Down Expand Up @@ -85,15 +85,13 @@ fn upgrade() {
}
#[tauri::command]
async fn suggest(kw: String) -> Vec<FileView> {
info!("suggest kw :{}", kw);
IDX_STORE.suggest(kw, 20)
}

#[tauri::command]
async fn search(
mut kw: String,
is_dir_opt: Option<bool>,
ext_opt: Option<String>,
) -> Vec<FileView> {
async fn search(mut kw: String, is_dir_opt: Option<bool>, ext_opt: Option<String>) -> SearchResult {
info!("search kw :{}", kw);
if kw.eq("") {
kw = "*".to_string();
}
Expand Down
39 changes: 8 additions & 31 deletions src-tauri/src/walk_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,23 @@ use jwalk::{DirEntry, WalkDir};
use log::info;
use std::sync::{Arc, Mutex};

use crate::walk_metrics::WALK_METRICS;
use std::time::SystemTime;

static mut WALK_METRICS: Option<Arc<Mutex<WalkMetrics>>> = None;

pub fn home_dir() -> String {
let option = dirs::home_dir();
option.unwrap().to_str().unwrap().to_string()
}

pub unsafe fn get_walk_matrix() -> WalkMatrixView {
if WALK_METRICS.is_none() && !IDX_STORE.is_full_indexing() {
return WalkMatrixView::new(100, IDX_STORE.num_docs());
}
WALK_METRICS
.clone()
.unwrap()
.lock()
.read()
.unwrap()
.view(move || IDX_STORE.num_docs())
}

use crate::user_setting::USER_SETTING;
pub fn run() {
init_walk_matrix();
let home = utils::norm(&home_dir());

start_walk_home_matrix();
Expand Down Expand Up @@ -69,23 +62,11 @@ fn need_skip_home(home: &String) -> bool {
}

fn end_walk_home_matrix() {
unsafe {
let walk_matrix0 = WALK_METRICS.clone().unwrap();
walk_matrix0.lock().unwrap().end_home();
}
WALK_METRICS.read().unwrap().end_home();
}

fn start_walk_home_matrix() {
unsafe {
let walk_matrix0 = WALK_METRICS.clone().unwrap();
walk_matrix0.lock().unwrap().start_home();
}
}

fn init_walk_matrix() {
unsafe {
WALK_METRICS = Some(Arc::new(Mutex::new(WalkMetrics::default())));
}
WALK_METRICS.write().unwrap().start_home();
}

#[cfg(unix)]
Expand All @@ -107,14 +88,10 @@ fn unix_walk_root(home: String) {
}

fn inc_root_walk_metrics(sz: usize, i: usize) {
unsafe {
WALK_METRICS
.clone()
.unwrap()
.lock()
.unwrap()
.root_inc_percent((i + 1) as u32, sz as u32);
}
WALK_METRICS
.write()
.unwrap()
.root_inc_percent((i + 1) as u32, sz as u32);
}

#[cfg(windows)]
Expand Down
9 changes: 9 additions & 0 deletions src-tauri/src/walk_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;

lazy_static! {
pub static ref WALK_METRICS: RwLock<WalkMetrics> = RwLock::new(WalkMetrics::default());
}

#[derive(Debug)]
pub struct WalkMetrics {
percent: Arc<RwLock<u32>>,
Expand Down Expand Up @@ -70,6 +74,11 @@ impl WalkMetrics {
self.home_over.store(true, Ordering::Relaxed);
}

pub fn end_of_no_reindex(&self) {
self.home_over.store(true, Ordering::Relaxed);
*self.percent.write().unwrap() = 100;
}

pub fn root_inc_percent(&mut self, walked_dir: u32, total_dir: u32) {
*self.percent.write().unwrap() = (walked_dir * 70 / total_dir) + 30;
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"package": {
"productName": "Orange",
"version": "0.5.0"
"version": "0.6.0"
},
"build": {
"distDir": "../build",
Expand Down
38 changes: 0 additions & 38 deletions src/App.css

This file was deleted.

Loading

0 comments on commit 889b2f3

Please sign in to comment.