mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-11 22:10:09 +01:00
feat: get output from commandline
This commit is contained in:
parent
cba41a78e5
commit
8ae71443d6
4 changed files with 132 additions and 44 deletions
|
|
@ -177,7 +177,7 @@ fn eval_condition(condition: &str, arg: &str) -> TokenStream2 {
|
|||
}
|
||||
},
|
||||
"err_contains" => quote!{error_msg.contains(#arg)},
|
||||
"cmd_contains" => quote!{command.contains(#arg)},
|
||||
"cmd_contains" => quote!{last_command.contains(#arg)},
|
||||
_ => unreachable!("Unknown condition when evaluation condition: {}", condition),
|
||||
}
|
||||
}
|
||||
|
|
@ -190,10 +190,13 @@ fn eval_suggest(suggest: &str) -> TokenStream2 {
|
|||
|
||||
let mut replace_list = Vec::new();
|
||||
let mut opt_list = Vec::new();
|
||||
let mut cmd_list = Vec::new();
|
||||
|
||||
replaces::opts(&mut suggest, &mut replace_list, &mut opt_list);
|
||||
replaces::shell(&mut suggest, &mut cmd_list);
|
||||
replaces::command(&mut suggest, &mut replace_list);
|
||||
replaces::typo(&mut suggest, &mut replace_list);
|
||||
replaces::shell_tag(&mut suggest, &mut replace_list, cmd_list);
|
||||
|
||||
quote! {
|
||||
#(#opt_list)*
|
||||
|
|
|
|||
|
|
@ -14,21 +14,25 @@ fn tag (name: &str, x: i32) -> String {
|
|||
tag
|
||||
}
|
||||
|
||||
fn eval_placeholder(string: &str, start: &str, end: &str) -> (std::ops::Range<usize>, std::ops::Range<usize>) {
|
||||
let start_index = string.find(start).unwrap();
|
||||
let end_index = string[start_index..].find(end).unwrap()
|
||||
+ start_index
|
||||
+ end.len();
|
||||
|
||||
let placeholder = start_index..end_index;
|
||||
|
||||
let args = start_index + start.len()..end_index - end.len();
|
||||
|
||||
(placeholder, args)
|
||||
}
|
||||
|
||||
pub fn opts(suggest: &mut String, replace_list: &mut Vec<TokenStream2>, opt_list: &mut Vec<TokenStream2>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "opts";
|
||||
while suggest.contains("{{opt::") {
|
||||
let placeholder_start = "{{opt::";
|
||||
let placeholder_end = "}}";
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{opt::", "}}");
|
||||
|
||||
let start_index = suggest.find(placeholder_start).unwrap();
|
||||
let end_index = suggest[start_index..].find(placeholder_end).unwrap()
|
||||
+ start_index
|
||||
+ placeholder_end.len();
|
||||
|
||||
let placeholder = start_index..end_index;
|
||||
|
||||
let args = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
||||
let opt = &suggest[args.to_owned()];
|
||||
let regex = opt.trim();
|
||||
let current_tag = tag(tag_name, replace_tag);
|
||||
|
|
@ -48,17 +52,8 @@ pub fn command(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
|||
let mut replace_tag = 0;
|
||||
let tag_name = "command";
|
||||
while suggest.contains("{{command") {
|
||||
let placeholder_start = "{{command";
|
||||
let placeholder_end = "}}";
|
||||
|
||||
let start_index = suggest.find(placeholder_start).unwrap();
|
||||
let end_index = suggest[start_index..].find(placeholder_end).unwrap()
|
||||
+ start_index
|
||||
+ placeholder_end.len();
|
||||
|
||||
let placeholder = start_index..end_index;
|
||||
|
||||
let args = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{command", "}}");
|
||||
|
||||
let range = suggest[args.to_owned()].trim_matches(|c| c == '[' || c == ']');
|
||||
if let Some((start, end)) = range.split_once(':') {
|
||||
let mut start_string = start.to_string();
|
||||
|
|
@ -100,16 +95,7 @@ pub fn typo(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
|||
let tag_name = "typo";
|
||||
|
||||
while suggest.contains("{{typo") {
|
||||
let placeholder_start = "{{typo";
|
||||
let placeholder_end = "}}";
|
||||
|
||||
let start_index = suggest.find(placeholder_start).unwrap();
|
||||
let end_index = suggest[start_index..].find(placeholder_end).unwrap()
|
||||
+ start_index
|
||||
+ placeholder_end.len();
|
||||
|
||||
let placeholder = start_index..end_index;
|
||||
let args = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{typo", "}}");
|
||||
|
||||
let string_index;
|
||||
if suggest.contains('[') {
|
||||
|
|
@ -118,7 +104,6 @@ pub fn typo(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
|||
.collect::<Vec<&str>>();
|
||||
let command_index = split[1].parse::<i32>().unwrap();
|
||||
if command_index < 0 {
|
||||
// command_index += split_command.len() as i32;
|
||||
string_index = format!("split_command.len() {}", command_index);
|
||||
} else {
|
||||
string_index = command_index.to_string();
|
||||
|
|
@ -126,22 +111,38 @@ pub fn typo(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
|||
} else {
|
||||
unreachable!("Typo suggestion must have a command index");
|
||||
}
|
||||
let mut match_list = Vec::new();
|
||||
let match_list;
|
||||
if suggest.contains('(') {
|
||||
let split = suggest[args.to_owned()]
|
||||
.split(&['(', ')'])
|
||||
.collect::<Vec<&str>>();
|
||||
match_list = split[1].trim().split(',').collect::<Vec<&str>>();
|
||||
.split_once("(").unwrap().1.rsplit_once(")").unwrap().0;
|
||||
match_list = split.split(',').collect::<Vec<&str>>();
|
||||
} else {
|
||||
unreachable!("Typo suggestion must have a match list");
|
||||
}
|
||||
|
||||
let match_list = match_list
|
||||
.iter()
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
let string_match_list = match_list.join(r#"".to_string(), ""#);
|
||||
let string_match_list = format!(r#""{}".to_string()"#, string_match_list);
|
||||
|
||||
let command = format!("suggest_typo(&split_command[{}], vec![{}])", string_index, string_match_list);
|
||||
let command;
|
||||
if match_list[0].starts_with("eval_shell_command("){
|
||||
let function = match_list.join(",");
|
||||
// add a " after first comma, and a " before last )
|
||||
let function = format!("{}\"{}{}",
|
||||
&function[..function.find(',').unwrap() + 1],
|
||||
&function[function.find(',').unwrap() + 1..function.len() - 1], "\")");
|
||||
command = format!("suggest_typo(&split_command[{}], {})", string_index, function);
|
||||
} else {
|
||||
let match_list = match_list
|
||||
.iter()
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
let string_match_list = match_list.join("\".to_string(), \"");
|
||||
let string_match_list = format!("\"{}\".to_string()", string_match_list);
|
||||
command = format!("suggest_typo(&split_command[{}], vec![{}])", string_index, string_match_list);
|
||||
}
|
||||
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
|
|
@ -149,3 +150,46 @@ pub fn typo(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn err(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "err";
|
||||
|
||||
while suggest.contains("{{err::") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{err::", "}}");
|
||||
|
||||
let regex = suggest[args.to_owned()].trim();
|
||||
|
||||
let command = format!("opt_regex({}, &mut error_msg)", regex);
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell(suggest: &mut String, cmd_list: &mut Vec<String>) {
|
||||
while suggest.contains("{{shell") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{shell", "}}");
|
||||
let range = suggest[args.to_owned()].trim_matches(|c| c == '(' || c == ')');
|
||||
|
||||
let command = format!("eval_shell_command(shell, {})", range);
|
||||
|
||||
suggest.replace_range(placeholder, &command);
|
||||
cmd_list.push(command);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell_tag(suggest: &mut String, replace_list: &mut Vec<TokenStream2>, cmd_list: Vec<String>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "shell";
|
||||
|
||||
for command in cmd_list {
|
||||
if suggest.contains(&command) {
|
||||
|
||||
*suggest = suggest.replace(&command, &tag(tag_name, replace_tag));
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,3 +64,14 @@ repack,
|
|||
replace,
|
||||
)}} {{command[2:]}}'''
|
||||
]
|
||||
|
||||
[[match_err]]
|
||||
pattern = [
|
||||
"did not match any file"
|
||||
]
|
||||
suggest = [
|
||||
'''
|
||||
#[cmd_contains(checkout)]
|
||||
git checkout {{typo[2]({{shell(git branch)}})}}
|
||||
'''
|
||||
]
|
||||
|
|
|
|||
|
|
@ -16,12 +16,12 @@ pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
|||
};
|
||||
|
||||
if !PRIVILEGE_LIST.contains(&executable) {
|
||||
let suggest = match_pattern("privilege", last_command, &err);
|
||||
let suggest = match_pattern("privilege", last_command, &err, shell);
|
||||
if suggest.is_some() {
|
||||
return suggest;
|
||||
}
|
||||
}
|
||||
let suggest = match_pattern(executable, last_command, &err);
|
||||
let suggest = match_pattern(executable, last_command, &err, shell);
|
||||
if let Some(suggest) = suggest {
|
||||
if PRIVILEGE_LIST.contains(&executable) {
|
||||
return Some(format!("{} {}", split_command[0], suggest));
|
||||
|
|
@ -29,14 +29,19 @@ pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
|||
return Some(suggest);
|
||||
}
|
||||
|
||||
let suggest = match_pattern("general", last_command, &err);
|
||||
let suggest = match_pattern("general", last_command, &err, shell);
|
||||
if let Some(suggest) = suggest {
|
||||
return Some(suggest);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn match_pattern(executable: &str, last_command: &str, error_msg: &str) -> Option<String> {
|
||||
fn match_pattern(
|
||||
executable: &str,
|
||||
last_command: &str,
|
||||
error_msg: &str,
|
||||
shell: &str,
|
||||
) -> Option<String> {
|
||||
parse_rules!("rules");
|
||||
}
|
||||
|
||||
|
|
@ -52,6 +57,31 @@ fn opt_regex(regex: &str, command: &mut String) -> String {
|
|||
opts.join(" ")
|
||||
}
|
||||
|
||||
fn err_regex(regex: &str, error_msg: &str) -> String {
|
||||
let regex = Regex::new(regex).unwrap();
|
||||
let err = regex
|
||||
.find_iter(error_msg)
|
||||
.map(|cap| cap.as_str().to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
err.join(" ")
|
||||
}
|
||||
|
||||
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>>();
|
||||
println!("{:?}", split_output);
|
||||
split_output
|
||||
.iter()
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
pub fn split_command(command: &str) -> Vec<String> {
|
||||
let regex = r#"([^\s"\\]+|"(?:\\.|[^"\\])*"|\\.)+"#;
|
||||
let regex = Regex::new(regex).unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue