From 1fcd0dccc84ce80ad82c660d3dfd3b2fc9abc617 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 29 Apr 2026 21:52:11 +0200 Subject: [PATCH] fix: %F and %U field code handling Fixes %F path arguments being ignored --- src/mime_app.rs | 76 ++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/src/mime_app.rs b/src/mime_app.rs index 4bec78c..48624fe 100644 --- a/src/mime_app.rs +++ b/src/mime_app.rs @@ -49,42 +49,19 @@ pub fn exec_to_command( } } - Some('f') => { - if !field_code_used { - // TODO: files on remote file systems should be copied to a temporary local file. - batch_process = true; - field_code_used = true; - new_argument.push_str(path.as_bytes()); - } + // %f and %u behave the same in a file manager. + Some('f' | 'u') if !field_code_used => { + // TODO: files on remote file systems should be copied to a temporary local file. + batch_process = true; + field_code_used = true; + new_argument.push_str(path.as_bytes()); } - Some('F') => { - if !field_code_used && new_argument.is_empty() { - field_code_used = true; - for path in path_opt - .iter() - .map(AsRef::as_ref) - .filter(|&path| from_file_or_dir(path).is_none()) - { - args.push(BString::new(path.as_bytes().to_owned())); - } - } - } - - Some('u') => { - if !field_code_used { - batch_process = true; - field_code_used = true; - new_argument.push_str(path.as_bytes()); - } - } - - Some('U') => { - if !field_code_used && new_argument.is_empty() { - field_code_used = true; - for path in path_opt.iter().map(AsRef::as_ref) { - args.push(BString::new(path.as_bytes().to_owned())); - } + // %F and %U behave the same in a file manager. + Some('F') | Some('U') if !field_code_used && new_argument.is_empty() => { + field_code_used = true; + for path in path_opt.iter().map(AsRef::as_ref) { + args.push(BString::new(path.as_bytes().to_owned())); } } @@ -133,12 +110,6 @@ pub fn exec_to_command( Some(commands) } -fn from_file_or_dir(path: impl AsRef) -> Option { - url::Url::from_file_path(&path) - .ok() - .or_else(|| url::Url::from_directory_path(&path).ok()) -} - #[derive(Clone, Debug)] pub struct MimeApp { pub id: String, @@ -490,33 +461,40 @@ mod tests { #[test] #[allow(non_snake_case)] fn one_path_F_field_code() { - let exec = "/usr/bin/bar %F"; - let paths = ["cat"]; + let exec = "/usr/bin/cosmic-term -w %F"; + let paths = ["/home/user"]; let commands = exec_to_command(exec, "one_path_F_field_code", None, &paths) .expect("Should parse valid exec"); assert_eq!(1, commands.len()); let command = commands.first().unwrap(); + let mut args = command.get_args(); - assert_eq!("/usr/bin/bar", command.get_program().to_str().unwrap()); - assert_eq!("cat", command.get_args().next().unwrap().to_str().unwrap()); + assert_eq!( + "/usr/bin/cosmic-term", + command.get_program().to_str().unwrap() + ); + assert_eq!("-w", args.next().unwrap().to_str().unwrap()); + assert_eq!(paths[0], args.next().unwrap().to_str().unwrap()); } #[test] fn one_path_u_field_code() { - let exec = "/usr/bin/foobar %u"; - let paths = ["/home/josh/krumpli"]; + let exec = "/usr/bin/cosmic-term -w %u"; + let paths = ["/home/user"]; let commands = exec_to_command(exec, "one_path_u_field_code", None, &paths) .expect("Should parse valid exec"); assert_eq!(1, commands.len()); let command = commands.first().unwrap(); + let mut args = command.get_args(); - assert_eq!("/usr/bin/foobar", command.get_program().to_str().unwrap()); assert_eq!( - *paths.first().unwrap(), - command.get_args().next().unwrap().to_str().unwrap() + "/usr/bin/cosmic-term", + command.get_program().to_str().unwrap() ); + assert_eq!("-w", args.next().unwrap().to_str().unwrap()); + assert_eq!(paths[0], args.next().unwrap().to_str().unwrap()); } #[test]