Skip to content

Commit 8db6325

Browse files
jonstodleStephan Dilly
authored andcommitted
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.
1 parent 6b4d088 commit 8db6325

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/components/externaleditor.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crossterm::{
1414
ExecutableCommand,
1515
};
1616
use scopeguard::defer;
17+
use std::ffi::OsStr;
1718
use std::{env, io, path::Path, process::Command};
1819
use tui::{
1920
backend::Backend,
@@ -56,24 +57,25 @@ impl ExternalEditorComponent {
5657
io::stdout().execute(EnterAlternateScreen).expect("reset terminal");
5758
}
5859

59-
let mut editor = env::var("GIT_EDITOR")
60+
let editor = env::var("GIT_EDITOR")
6061
.ok()
6162
.or_else(|| env::var("VISUAL").ok())
6263
.or_else(|| env::var("EDITOR").ok())
6364
.unwrap_or_else(|| String::from("vi"));
6465

65-
//TODO: check the path.to_str result and return err on None because
66-
//otherwise this will pretty likely fail in the command stage otherwise
67-
//and https://github.com/extrawurst/gitui/issues/184 showed how weird
68-
//'vi' handles opening not existing files
69-
editor.push_str(&format!(" {}", path.to_string_lossy()));
70-
66+
// TODO: proper handling arguments containing whitespaces
67+
// This does not do the right thing if the input is `editor --something "with spaces"`
7168
let mut editor = editor.split_whitespace();
7269

7370
let command = editor.next().ok_or_else(|| {
7471
anyhow!("unable to read editor command")
7572
})?;
7673

74+
let mut editor: Vec<&OsStr> =
75+
editor.map(|s| OsStr::new(s)).collect();
76+
77+
editor.push(path.as_os_str());
78+
7779
Command::new(command)
7880
.current_dir(work_dir)
7981
.args(editor)

0 commit comments

Comments
 (0)