From b39b506e27c11dac59aa73a2a9a2204548e9fda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sat, 2 Mar 2024 13:00:39 +0800 Subject: [PATCH] feat: support `%0` as the hovered file for Windows (#761) --- yazi-plugin/src/external/shell.rs | 41 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/yazi-plugin/src/external/shell.rs b/yazi-plugin/src/external/shell.rs index 0ba75fcd..2b85a757 100644 --- a/yazi-plugin/src/external/shell.rs +++ b/yazi-plugin/src/external/shell.rs @@ -102,9 +102,7 @@ mod parser { expanded.push(s); } else if c == '%' && it.peek().is_some_and(|&c| c == '*') { it.next(); - for arg in args { - expanded.push(arg.to_string()); - } + expanded.extend(args.iter().skip(1).map(|&s| s.to_owned())); } else { next_string(&mut it, args, &mut s, c); @@ -140,7 +138,9 @@ mod parser { } else if c == '%' { match it.peek() { Some('*') => { - s.push_str(&args.join(" ")); + if args.len() > 1 { + s.push_str(&args[1..].join(" ")); + } it.next(); } Some(n) if n.is_ascii_digit() => { @@ -155,9 +155,8 @@ mod parser { } } - let pos = pos.parse::().unwrap(); - if pos > 0 { - s.push_str(args.get(pos - 1).unwrap_or(&"")); + if let Some(arg) = args.get(pos.parse::().unwrap()) { + s.push_str(arg); } } _ => s.push('%'), @@ -173,49 +172,55 @@ mod parser { #[test] fn test_no_quote() { - let args = parse("echo abc xyz %0 %2", &["111", "222"]); - assert_eq!(args, ["echo", "abc", "xyz", "", "222"]); + let args = parse("echo abc xyz %0 %2", &["000", "111", "222"]); + assert_eq!(args, ["echo", "abc", "xyz", "000", "222"]); - let args = parse(" echo abc xyz %1 %2 ", &["111", "222"]); + let args = parse(" echo abc xyz %1 %2 ", &["", "111", "222"]); assert_eq!(args, ["echo", "abc", "xyz", "111", "222"]); } #[test] fn test_single_quote() { - let args = parse("echo 'abc xyz' '%1' %2", &["111", "222"]); + let args = parse("echo 'abc xyz' '%1' %2", &["000", "111", "222"]); assert_eq!(args, ["echo", "abc xyz", "111", "222"]); - let args = parse(r#"echo 'abc ""xyz' '%1' %2"#, &["111", "222"]); + let args = parse(r#"echo 'abc ""xyz' '%1' %2"#, &["", "111", "222"]); assert_eq!(args, ["echo", r#"abc ""xyz"#, "111", "222"]); } #[test] fn test_double_quote() { - let args = parse("echo \"abc ' 'xyz\" \"%1\" %2 %3", &["111", "222"]); + let args = parse("echo \"abc ' 'xyz\" \"%1\" %2 %3", &["", "111", "222"]); assert_eq!(args, ["echo", "abc ' 'xyz", "111", "222", ""]); } #[test] fn test_escaped() { - let args = parse("echo \"a\tbc ' 'x\nyz\" \"\\%1\" %2 %3", &["111", "22 2"]); + let args = parse("echo \"a\tbc ' 'x\nyz\" \"\\%1\" %2 %3", &["", "111", "22 2"]); assert_eq!(args, ["echo", "a\tbc ' 'x\nyz", "%1", "22 2", ""]); } #[test] fn test_percent_star() { - let args = parse("echo %* xyz", &["111", "222"]); + let args = parse("echo %* xyz", &[]); + assert_eq!(args, ["echo", "xyz"]); + + let args = parse("echo %* xyz", &["000", "111", "222"]); assert_eq!(args, ["echo", "111", "222", "xyz"]); - let args = parse("echo '%*' xyz", &["111", "222"]); + let args = parse("echo '%*' xyz", &["000", "111", "222"]); assert_eq!(args, ["echo", "111 222", "xyz"]); - let args = parse("echo -C%* xyz", &["111", "222"]); + let args = parse("echo -C%* xyz", &[]); + assert_eq!(args, ["echo", "-C", "xyz"]); + + let args = parse("echo -C%* xyz", &["000", "111", "222"]); assert_eq!(args, ["echo", "-C111 222", "xyz"]); } #[test] fn test_env_var() { - let args = parse(" %EDITOR% %* xyz", &["111", "222"]); + let args = parse(" %EDITOR% %* xyz", &["000", "111", "222"]); assert_eq!(args, ["%EDITOR%", "111", "222", "xyz"]); } }