From ea38f2ffbfb6ec8e20343c89d98a690ad344caff Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 23 Mar 2018 22:24:49 +0100 Subject: [PATCH 1/2] Fix: Pass /dev/tty as stdin for editor This fixes the issue that spawning the editor trashes the terminal. The signature of the Runtime::editor() function changed, which has to be fixed in using code. --- lib/core/libimagrt/src/error.rs | 1 + lib/core/libimagrt/src/runtime.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/core/libimagrt/src/error.rs b/lib/core/libimagrt/src/error.rs index cfba9a80..35331c67 100644 --- a/lib/core/libimagrt/src/error.rs +++ b/lib/core/libimagrt/src/error.rs @@ -23,6 +23,7 @@ error_chain! { } foreign_links { + IO(::std::io::Error); TomlDeError(::toml::de::Error); TomlQueryError(::toml_query::error::Error); HandlebarsTemplateError(::handlebars::TemplateError); diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 1e29f96d..8cb4521c 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -39,6 +39,7 @@ use io::OutputProxy; use libimagerror::trace::*; use libimagstore::store::Store; use libimagstore::file_abstraction::InMemoryFileAbstraction; +use libimagutil::debug_result::DebugResult; use spec::CliSpec; /// The Runtime object @@ -454,7 +455,7 @@ impl<'a> Runtime<'a> { } /// Get a editor command object which can be called to open the $EDITOR - pub fn editor(&self) -> Option { + pub fn editor(&self) -> Result, RuntimeError> { self.cli() .value_of("editor") .map(String::from) @@ -466,18 +467,19 @@ impl<'a> Runtime<'a> { }) }) .or(env::var("EDITOR").ok()) - .map(|s| {debug!("Editing with '{}'", s); s}) + .ok_or_else(|| RuntimeErrorKind::IOError.into()) + .map_dbg(|s| format!("Editing with '{}'", s)) .and_then(|s| { let mut split = s.split(" "); let command = split.next(); if command.is_none() { - return None + return Ok(None) } let mut c = Command::new(command.unwrap()); // secured above c.args(split); - c.stdin(::std::process::Stdio::null()); + c.stdin(::std::fs::File::open("/dev/tty")?); c.stderr(::std::process::Stdio::inherit()); - Some(c) + Ok(Some(c)) }) } From 742975466562fd7a76ddf1c16db53cfc510dbf0c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 23 Mar 2018 22:57:07 +0100 Subject: [PATCH 2/2] Rewrite edit_in_tmpfile() for new Runtime::editor() signature --- lib/entry/libimagentryedit/src/edit.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/entry/libimagentryedit/src/edit.rs b/lib/entry/libimagentryedit/src/edit.rs index 6802eaa3..b2d45383 100644 --- a/lib/entry/libimagentryedit/src/edit.rs +++ b/lib/entry/libimagentryedit/src/edit.rs @@ -72,18 +72,19 @@ impl EditHeader for Entry { pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> { use libimagutil::edit::edit_in_tmpfile_with_command; - rt.editor() - .ok_or(EE::from_kind(EditErrorKind::NoEditor)) - .and_then(|editor| { - edit_in_tmpfile_with_command(editor, s) - .chain_err(|| EditErrorKind::IOError) - .and_then(|worked| { - if !worked { - Err(EditErrorKind::ProcessExitFailure.into()) - } else { - Ok(()) - } - }) + let editor = rt + .editor() + .chain_err(|| EditErrorKind::NoEditor)? + .ok_or_else(|| EE::from_kind(EditErrorKind::NoEditor))?; + + edit_in_tmpfile_with_command(editor, s) + .chain_err(|| EditErrorKind::IOError) + .and_then(|worked| { + if !worked { + Err(EditErrorKind::ProcessExitFailure.into()) + } else { + Ok(()) + } }) }