mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
port bugfix for escaped quote and add testcases
This commit is contained in:
parent
822fdb2bad
commit
54e17af01e
1 changed files with 42 additions and 6 deletions
48
core/src/external/shell.rs
vendored
48
core/src/external/shell.rs
vendored
|
|
@ -147,13 +147,17 @@ mod cmdexpand {
|
|||
Ok(expanded)
|
||||
}
|
||||
|
||||
fn escaped_char(input: &str) -> IResult<&str, &str> {
|
||||
recognize(pair(char('\\'), anychar))(input)
|
||||
}
|
||||
|
||||
fn parse_cmd(cmd: &str) -> Result<Vec<CommandPart>> {
|
||||
fn double_quote_text(input: &str) -> IResult<&str, &str> {
|
||||
recognize(delimited(char('"'), is_not("\""), char('"')))(input)
|
||||
recognize(delimited(char('"'), many0(alt((is_not("\"\\"), escaped_char))), char('"')))(input)
|
||||
}
|
||||
|
||||
fn single_quote_text(input: &str) -> IResult<&str, &str> {
|
||||
recognize(delimited(char('\''), is_not("'"), char('\'')))(input)
|
||||
recognize(delimited(char('\''), many0(alt((is_not("'"), escaped_char))), char('\'')))(input)
|
||||
}
|
||||
|
||||
fn no_quote_text(input: &str) -> IResult<&str, &str> {
|
||||
|
|
@ -183,10 +187,6 @@ mod cmdexpand {
|
|||
}
|
||||
|
||||
fn parse_text(text: &str) -> Result<Vec<TextPart>> {
|
||||
fn escaped_char(input: &str) -> IResult<&str, &str> {
|
||||
recognize(pair(char('\\'), anychar))(input)
|
||||
}
|
||||
|
||||
fn normal_text(input: &str) -> IResult<&str, TextPart> {
|
||||
let (input, output) = recognize(many1(alt((escaped_char, is_not("\\%")))))(input)?;
|
||||
Ok((input, TextPart::NormalText(output)))
|
||||
|
|
@ -219,4 +219,40 @@ mod cmdexpand {
|
|||
(Quote::DoubleQuote, _) => content.replace("\"", "\\\"").to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_expand_arguments() {
|
||||
assert_eq!(expand_cmd("echo %1", &["abc"]).unwrap(), "echo abc");
|
||||
assert_eq!(expand_cmd("echo %2", &["abc", "def"]).unwrap(), "echo def");
|
||||
assert_eq!(expand_cmd("echo %1", &["abc def"]).unwrap(), "echo \"abc def\"");
|
||||
assert_eq!(expand_cmd("echo %1", &["\"abc\""]).unwrap(), "echo \"abc\"");
|
||||
assert_eq!(
|
||||
expand_cmd(r#"echo "hello %1""#, &[r#""world""#]).unwrap(),
|
||||
r#"echo "hello \"world\"""#
|
||||
);
|
||||
assert_eq!(
|
||||
expand_cmd(r#"echo "hello %1""#, &[r#""my king""#]).unwrap(),
|
||||
r#"echo "hello \"my king\"""#
|
||||
);
|
||||
assert_eq!(
|
||||
expand_cmd(r#"echo "hello %1""#, &["my king"]).unwrap(),
|
||||
r#"echo "hello my king""#
|
||||
);
|
||||
assert_eq!(
|
||||
expand_cmd(r#"echo %1"#, &["cmd /C \"run something\""]).unwrap(),
|
||||
r#"echo "cmd /C \"run something\"""#
|
||||
);
|
||||
assert_eq!(expand_cmd("cmd %*", &["abc", "def ghk"]).unwrap(), r#"cmd abc "def ghk""#);
|
||||
assert_eq!(expand_cmd("cmd a\\%*", &["abc", "def ghk"]).unwrap(), r#"cmd a\%*"#);
|
||||
assert_eq!(
|
||||
expand_cmd(r#"cmd "Hello \"world"#, &Vec::<String>::new()).unwrap(),
|
||||
r#"cmd "Hello \"world"#
|
||||
);
|
||||
assert_eq!(expand_cmd(r#" a "b \"%1\"" "#, &["c", "d"]).unwrap(), r#" a "b \"c\"" "#);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue