Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): handle editor/visual/configured editor with arguments #1587

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion zellij-server/src/os_input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
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.
Expand Down Expand Up @@ -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()
Expand Down