feat: allow using line continuations in templates

This commit is contained in:
iff 2023-08-11 15:29:10 +02:00
parent a193a86a5f
commit 498a134da3
4 changed files with 8 additions and 11 deletions

View file

@ -4,6 +4,6 @@ command = "touch"
pattern = [ "No such file or directory" ] pattern = [ "No such file or directory" ]
suggest = [ suggest = [
''' '''
mkdir --parents {{cmd::(?:\s)+(.*[\\\/])(?:\s)*}} mkdir --parents {{cmd::(?:\s)+(.*[\\\/])(?:\s)*}} && \
touch {{command[1:]}} ''' touch {{command[1:]}} '''
] ]

View file

@ -42,7 +42,7 @@ pub fn get_best_match_file(input: &str) -> Option<String> {
.map(|file| { .map(|file| {
let file = file.unwrap(); let file = file.unwrap();
file.file_name().into_string().unwrap().replace(" ", "\\ ") file.file_name().into_string().unwrap().replace(' ', "\\ ")
}) })
.collect::<Vec<String>>(); .collect::<Vec<String>>();

View file

@ -9,8 +9,8 @@ pub fn highlight_difference(
suggested_command: &str, suggested_command: &str,
last_command: &str, last_command: &str,
) -> Option<String> { ) -> Option<String> {
let replaced_newline = suggested_command.replace('\n', r" {{newline}} "); // let replaced_newline = suggested_command.replace('\n', r" {{newline}} ");
let mut split_suggested_command = split_command(&replaced_newline); let mut split_suggested_command = split_command(suggested_command);
let split_last_command = split_command(last_command); let split_last_command = split_command(last_command);
if split_suggested_command == split_last_command { if split_suggested_command == split_last_command {
@ -34,7 +34,7 @@ pub fn highlight_difference(
// let mut highlighted = suggested_command.to_string(); // let mut highlighted = suggested_command.to_string();
'next: for entry in split_suggested_command.iter_mut() { 'next: for entry in split_suggested_command.iter_mut() {
if entry == r"{{newline}}" { if entry == "\n" {
continue; continue;
} }
for old in &old_entries { for old in &old_entries {
@ -46,7 +46,6 @@ pub fn highlight_difference(
*entry = entry.red().bold().to_string(); *entry = entry.red().bold().to_string();
} }
let highlighted;
if privileged if privileged
&& (suggested_command.contains("&&") && (suggested_command.contains("&&")
|| suggested_command.contains("||") || suggested_command.contains("||")
@ -57,10 +56,8 @@ pub fn highlight_difference(
let len = split_suggested_command.len() - 1; let len = split_suggested_command.len() - 1;
split_suggested_command[len] = split_suggested_command[len] =
split_suggested_command[len].clone() + "\"".red().bold().to_string().as_str(); split_suggested_command[len].clone() + "\"".red().bold().to_string().as_str();
highlighted = split_suggested_command.join(" ");
} else {
highlighted = split_suggested_command.join(" ");
} }
let highlighted = split_suggested_command.join(" ");
Some(highlighted.replace(r" {{newline}} ", "\n")) Some(highlighted.replace(" \n ", "\n"))
} }

View file

@ -123,7 +123,7 @@ fn eval_shell_command(shell: &str, command: &str) -> Vec<String> {
pub fn split_command(command: &str) -> Vec<String> { pub fn split_command(command: &str) -> Vec<String> {
// this regex splits the command separated by spaces, except when the space // this regex splits the command separated by spaces, except when the space
// is escaped by a backslash or surrounded by quotes // is escaped by a backslash or surrounded by quotes
let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|\\\s+|'(?:\\.|[^'\\])*'|\\\s)+"#; let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\\ )+|\\|\n"#;
let regex = Regex::new(regex).unwrap(); let regex = Regex::new(regex).unwrap();
let split_command = regex let split_command = regex
.find_iter(command) .find_iter(command)