2020-10-15 05:41:37 +13:00
|
|
|
use std::path::Path;
|
2020-09-27 03:52:13 +13:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2021-11-28 08:49:20 +13:00
|
|
|
use crate::common::Common;
|
|
|
|
use crate::common_messages::Messages;
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
#[derive(Clone, Default)]
|
2020-09-27 03:52:13 +13:00
|
|
|
pub struct ExcludedItems {
|
|
|
|
pub items: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExcludedItems {
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2020-10-07 21:34:15 +13:00
|
|
|
/// Setting excluded items which needs to contains * wildcard
|
2020-09-27 03:52:13 +13:00
|
|
|
/// Are a lot of slower than absolute path, so it should be used to heavy
|
2021-01-11 08:44:10 +13:00
|
|
|
pub fn set_excluded_items(&mut self, excluded_items: Vec<String>, text_messages: &mut Messages) {
|
2020-09-27 03:52:13 +13:00
|
|
|
let start_time: SystemTime = SystemTime::now();
|
|
|
|
|
|
|
|
if excluded_items.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:44:10 +13:00
|
|
|
let expressions: Vec<String> = excluded_items;
|
2020-09-27 03:52:13 +13:00
|
|
|
let mut checked_expressions: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
for expression in expressions {
|
|
|
|
let expression: String = expression.trim().to_string();
|
|
|
|
|
2021-01-01 07:53:49 +13:00
|
|
|
if expression.is_empty() {
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-14 18:34:43 +13:00
|
|
|
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
let expression = expression.replace("/", "\\");
|
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
if expression == "DEFAULT" {
|
2020-12-24 10:17:04 +13:00
|
|
|
if cfg!(target_family = "unix") {
|
2021-01-04 04:54:07 +13:00
|
|
|
checked_expressions.push("*/.git/*,*/node_modules/*,*/lost+found/*,*/Trash/*,*/.Trash-*/*,*/snap/*,/home/*/.cache/*".to_string());
|
2020-12-24 10:17:04 +13:00
|
|
|
}
|
|
|
|
if cfg!(target_family = "windows") {
|
2021-05-09 07:54:01 +12:00
|
|
|
checked_expressions.push("*\\.git\\*,*\\node_modules\\*,*\\lost+found\\*,*:\\windows\\*".to_string());
|
2020-12-24 10:17:04 +13:00
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if !expression.contains('*') {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages
|
|
|
|
.warnings
|
|
|
|
.push("Excluded Items Warning: Wildcard * is required in expression, ignoring ".to_string() + expression.as_str());
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_expressions.push(expression);
|
|
|
|
}
|
|
|
|
self.items = checked_expressions;
|
2023-01-29 06:54:02 +13:00
|
|
|
Common::print_time(start_time, SystemTime::now(), "set_excluded_items");
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2020-10-15 05:41:37 +13:00
|
|
|
|
|
|
|
/// Checks whether a specified path is excluded from searching
|
|
|
|
pub fn is_excluded(&self, path: impl AsRef<Path>) -> bool {
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
let path = Common::normalize_windows_path(path);
|
|
|
|
|
|
|
|
for expression in &self.items {
|
|
|
|
if Common::regex_check(expression, &path) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|