diff --git a/zellij-server/src/os_input_output.rs b/zellij-server/src/os_input_output.rs index b4384165..31f1780b 100644 --- a/zellij-server/src/os_input_output.rs +++ b/zellij-server/src/os_input_output.rs @@ -162,6 +162,24 @@ fn handle_terminal( } } +// this is a utility method to separate the arguments from a pathbuf before we turn it into a +// Command. eg. "/usr/bin/vim -e" ==> "/usr/bin/vim" + "-e" (the latter will be pushed to args) +fn separate_command_arguments(command: &mut PathBuf, args: &mut Vec) { + if let Some(file_name) = command + .file_name() + .and_then(|f_n| f_n.to_str()) + .map(|f_n| f_n.to_string()) + { + let mut file_name_parts = file_name.split_ascii_whitespace(); + if let Some(first_part) = file_name_parts.next() { + command.set_file_name(first_part); + for part in file_name_parts { + args.push(String::from(part)); + } + } + } +} + /// If a [`TerminalAction::OpenFile(file)`] is given, the text editor specified by environment variable `EDITOR` /// (or `VISUAL`, if `EDITOR` is not set) will be started in the new terminal, with the given /// file open. @@ -191,11 +209,16 @@ pub fn spawn_terminal( "No Editor found, consider setting a path to one in $EDITOR or $VISUAL", ); } - let command = default_editor.unwrap_or_else(|| { + + let mut command = default_editor.unwrap_or_else(|| { PathBuf::from(env::var("EDITOR").unwrap_or_else(|_| env::var("VISUAL").unwrap())) }); let mut args = vec![]; + + if !command.is_dir() { + separate_command_arguments(&mut command, &mut args); + } let file_to_open = file_to_open .into_os_string() .into_string()