mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
chore: rearrange directories
This commit is contained in:
parent
d5fb7462e0
commit
4c9aac45a8
46 changed files with 11 additions and 18 deletions
153
utils/src/evals.rs
Normal file
153
utils/src/evals.rs
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
use crate::files::*;
|
||||
use regex_lite::Regex;
|
||||
|
||||
pub fn opt_regex(regex: &str, command: &mut String) -> String {
|
||||
let regex = Regex::new(regex).unwrap();
|
||||
|
||||
let mut opts = Vec::new();
|
||||
for captures in regex.captures_iter(command) {
|
||||
for cap in captures.iter().skip(1).flatten() {
|
||||
opts.push(cap.as_str().to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
for opt in opts.clone() {
|
||||
*command = command.replace(&opt, "");
|
||||
}
|
||||
opts.join(" ")
|
||||
}
|
||||
|
||||
pub fn err_regex(regex: &str, error_msg: &str) -> String {
|
||||
let regex = Regex::new(regex).unwrap();
|
||||
|
||||
let mut err = Vec::new();
|
||||
for captures in regex.captures_iter(error_msg) {
|
||||
for cap in captures.iter().skip(1).flatten() {
|
||||
err.push(cap.as_str().to_owned());
|
||||
}
|
||||
}
|
||||
err.join(" ")
|
||||
}
|
||||
|
||||
pub fn cmd_regex(regex: &str, command: &str) -> String {
|
||||
let regex = Regex::new(regex).unwrap();
|
||||
|
||||
let mut cmd = Vec::new();
|
||||
for captures in regex.captures_iter(command) {
|
||||
for cap in captures.iter().skip(1).flatten() {
|
||||
cmd.push(cap.as_str().to_owned());
|
||||
}
|
||||
}
|
||||
cmd.join(" ")
|
||||
}
|
||||
|
||||
pub fn eval_shell_command(shell: &str, command: &str) -> Vec<String> {
|
||||
let output = std::process::Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
let output = String::from_utf8_lossy(&output.stdout);
|
||||
let split_output = output.split('\n').collect::<Vec<&str>>();
|
||||
split_output
|
||||
.iter()
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
pub fn split_command(command: &str) -> Vec<String> {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("command: {command}");
|
||||
// this regex splits the command separated by spaces, except when the space
|
||||
// is escaped by a backslash or surrounded by quotes
|
||||
let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\\ )+|\\|\n"#;
|
||||
let regex = Regex::new(regex).unwrap();
|
||||
let split_command = regex
|
||||
.find_iter(command)
|
||||
.map(|cap| cap.as_str().to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
split_command
|
||||
}
|
||||
|
||||
pub fn suggest_typo(typos: &[String], candidates: Vec<String>, executables: &[String]) -> String {
|
||||
let mut suggestions = Vec::new();
|
||||
for typo in typos {
|
||||
let typo = typo.as_str();
|
||||
if candidates.len() == 1 {
|
||||
match candidates[0].as_str() {
|
||||
"path" => {
|
||||
if let Some(suggest) = find_similar(typo, executables, Some(2)) {
|
||||
suggestions.push(suggest);
|
||||
} else {
|
||||
suggestions.push(typo.to_string());
|
||||
}
|
||||
}
|
||||
"file" => {
|
||||
if let Some(suggest) = get_best_match_file(typo) {
|
||||
suggestions.push(suggest);
|
||||
} else {
|
||||
suggestions.push(typo.to_string());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if let Some(suggest) = find_similar(typo, &candidates, Some(2)) {
|
||||
suggestions.push(suggest);
|
||||
} else {
|
||||
suggestions.push(typo.to_string());
|
||||
}
|
||||
}
|
||||
suggestions.join(" ")
|
||||
}
|
||||
|
||||
pub fn best_match_path(typo: &str, executables: &[String]) -> Option<String> {
|
||||
find_similar(typo, executables, Some(3))
|
||||
}
|
||||
|
||||
// higher the threshold, the stricter the comparison
|
||||
// 1: anything
|
||||
// 2: 50%
|
||||
// 3: 33%
|
||||
// ... etc
|
||||
pub fn find_similar(typo: &str, candidates: &[String], threshold: Option<usize>) -> Option<String> {
|
||||
let threshold = threshold.unwrap_or(2);
|
||||
let mut min_distance = typo.chars().count() / threshold + 1;
|
||||
let mut min_distance_index = None;
|
||||
for (i, candidate) in candidates.iter().enumerate() {
|
||||
if candidate.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let distance = compare_string(typo, candidate);
|
||||
if distance < min_distance {
|
||||
min_distance = distance;
|
||||
min_distance_index = Some(i);
|
||||
}
|
||||
}
|
||||
if let Some(min_distance_index) = min_distance_index {
|
||||
return Some(candidates[min_distance_index].to_string());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
pub fn compare_string(a: &str, b: &str) -> usize {
|
||||
let mut matrix = vec![vec![0; b.chars().count() + 1]; a.chars().count() + 1];
|
||||
|
||||
for i in 0..a.chars().count() + 1 {
|
||||
matrix[i][0] = i;
|
||||
}
|
||||
for j in 0..b.chars().count() + 1 {
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
|
||||
for (i, ca) in a.chars().enumerate() {
|
||||
for (j, cb) in b.chars().enumerate() {
|
||||
let cost = if ca == cb { 0 } else { 1 };
|
||||
matrix[i + 1][j + 1] = std::cmp::min(
|
||||
std::cmp::min(matrix[i][j + 1] + 1, matrix[i + 1][j] + 1),
|
||||
matrix[i][j] + cost,
|
||||
);
|
||||
}
|
||||
}
|
||||
matrix[a.chars().count()][b.chars().count()]
|
||||
}
|
||||
166
utils/src/files.rs
Normal file
166
utils/src/files.rs
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
use crate::evals::find_similar;
|
||||
|
||||
pub fn get_path_files() -> Vec<String> {
|
||||
let path_env = path_env();
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
eprintln!("path_env: {path_env}");
|
||||
}
|
||||
|
||||
let path_env_sep = path_env_sep();
|
||||
|
||||
let path = path_env.split(path_env_sep).collect::<Vec<&str>>();
|
||||
let mut all_executable = vec![];
|
||||
for p in path {
|
||||
#[cfg(windows)]
|
||||
let p = path_convert(p);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("p={p}");
|
||||
|
||||
let files = match std::fs::read_dir(p) {
|
||||
Ok(files) => files,
|
||||
Err(_) => continue,
|
||||
};
|
||||
for file in files {
|
||||
let file = file.unwrap();
|
||||
#[allow(unused_mut)]
|
||||
let mut file_name = file.file_name().into_string().unwrap();
|
||||
|
||||
#[cfg(windows)]
|
||||
strip_extension(&mut file_name);
|
||||
|
||||
all_executable.push(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let mut all_executable = all_executable.clone();
|
||||
all_executable.sort_unstable();
|
||||
eprintln!("all_executable={all_executable:?}");
|
||||
}
|
||||
|
||||
all_executable
|
||||
}
|
||||
|
||||
pub fn get_best_match_file(input: &str) -> Option<String> {
|
||||
let mut input = input.trim_matches(|c| c == '\'' || c == '"').to_owned();
|
||||
if cfg!(debug_assertions) {
|
||||
eprintln!("get_best_match_file input: {input}");
|
||||
}
|
||||
let mut exit_dirs = Vec::new();
|
||||
let mut files = loop {
|
||||
match std::fs::read_dir(&input) {
|
||||
Ok(files) => break files,
|
||||
Err(_) => {
|
||||
if let Some((dirs, exit_dir)) = input.rsplit_once(std::path::MAIN_SEPARATOR) {
|
||||
exit_dirs.push(exit_dir.to_owned());
|
||||
input = dirs.to_owned();
|
||||
} else {
|
||||
exit_dirs.push(input.to_owned());
|
||||
input = ".".to_owned();
|
||||
break std::fs::read_dir("./").unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
while let Some(exit_dir) = exit_dirs.pop() {
|
||||
let dir_files = files
|
||||
.map(|file| {
|
||||
let file = file.unwrap();
|
||||
|
||||
file.file_name().into_string().unwrap().replace(' ', "\\ ")
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let best_match = find_similar(&exit_dir, &dir_files, Some(2));
|
||||
best_match.as_ref()?;
|
||||
|
||||
input = format!("{}/{}", input, best_match.unwrap());
|
||||
files = match std::fs::read_dir(&input) {
|
||||
Ok(files) => files,
|
||||
Err(_) => return Some(input),
|
||||
};
|
||||
}
|
||||
|
||||
Some(input)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn msys2_conv_path(p: &str) -> std::io::Result<String> {
|
||||
std::process::Command::new("cygpath")
|
||||
.arg("-w")
|
||||
.arg(p)
|
||||
.output()
|
||||
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned())
|
||||
}
|
||||
#[cfg(windows)]
|
||||
fn is_msystem() -> bool {
|
||||
std::env::var("MSYSTEM").is_ok()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn path_env() -> String {
|
||||
if is_msystem() {
|
||||
String::from_utf8_lossy(
|
||||
&std::process::Command::new("bash")
|
||||
.arg("-c")
|
||||
.arg("echo $PATH")
|
||||
.output()
|
||||
.unwrap()
|
||||
.stdout,
|
||||
)
|
||||
.trim()
|
||||
.to_owned()
|
||||
} else {
|
||||
std::env::var("PATH").unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn path_env_sep() -> &'static str {
|
||||
if is_msystem() {
|
||||
":"
|
||||
} else {
|
||||
";"
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn path_convert(path: &str) -> String {
|
||||
if is_msystem() {
|
||||
msys2_conv_path(path).expect("Failed to convert path for msys")
|
||||
} else {
|
||||
path.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn strip_extension(file_name: &str) -> String {
|
||||
let mut file_name = file_name.to_owned();
|
||||
let suffixies = [".exe", ".sh", ".ps1"];
|
||||
for suffix in suffixies {
|
||||
if let Some(file_name_strip) = file_name.strip_suffix(suffix) {
|
||||
file_name = file_name_strip.to_owned();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !file_name.contains(".") {
|
||||
file_name = file_name.to_owned();
|
||||
}
|
||||
|
||||
file_name
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn path_env() -> String {
|
||||
std::env::var("PATH").unwrap()
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn path_env_sep() -> &'static str {
|
||||
":"
|
||||
}
|
||||
2
utils/src/lib.rs
Normal file
2
utils/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod evals;
|
||||
pub mod files;
|
||||
Loading…
Add table
Add a link
Reference in a new issue