From 8db6325777860326dde6b5571a645c64336b842e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Grythe=20St=C3=B8dle?= Date: Wed, 29 Jul 2020 20:13:57 +0200 Subject: [PATCH] Add support for spaces in file path If the file path contained a space, the editor would be launched with an incomplete file path. This also switches to using `OsStr` for the file path, which means that if the file name contains invalid UTF-8, it will not blow up because of gitui. --- src/components/externaleditor.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/components/externaleditor.rs b/src/components/externaleditor.rs index f0027035..39d7e41c 100644 --- a/src/components/externaleditor.rs +++ b/src/components/externaleditor.rs @@ -14,6 +14,7 @@ use crossterm::{ ExecutableCommand, }; use scopeguard::defer; +use std::ffi::OsStr; use std::{env, io, path::Path, process::Command}; use tui::{ backend::Backend, @@ -56,24 +57,25 @@ impl ExternalEditorComponent { io::stdout().execute(EnterAlternateScreen).expect("reset terminal"); } - let mut editor = env::var("GIT_EDITOR") + let editor = env::var("GIT_EDITOR") .ok() .or_else(|| env::var("VISUAL").ok()) .or_else(|| env::var("EDITOR").ok()) .unwrap_or_else(|| String::from("vi")); - //TODO: check the path.to_str result and return err on None because - //otherwise this will pretty likely fail in the command stage otherwise - //and https://github.com/extrawurst/gitui/issues/184 showed how weird - //'vi' handles opening not existing files - editor.push_str(&format!(" {}", path.to_string_lossy())); - + // TODO: proper handling arguments containing whitespaces + // This does not do the right thing if the input is `editor --something "with spaces"` let mut editor = editor.split_whitespace(); let command = editor.next().ok_or_else(|| { anyhow!("unable to read editor command") })?; + let mut editor: Vec<&OsStr> = + editor.map(|s| OsStr::new(s)).collect(); + + editor.push(path.as_os_str()); + Command::new(command) .current_dir(work_dir) .args(editor)