shell: Move changes outside so we can properly handle errors

pull/689/head
Blaž Hrastnik 3 years ago
parent e772808a5b
commit 4a76ea8f88

@ -4346,74 +4346,73 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
if event == PromptEvent::Validate { if event == PromptEvent::Validate {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let mut error: Option<&str> = None;
let transaction = let mut changes = Vec::with_capacity(selection.len());
Transaction::change_by_selection(doc.text(), selection, |range| {
let mut process; for range in selection.ranges() {
match Command::new(&shell[0]) let mut process;
.args(&shell[1..]) match Command::new(&shell[0])
.arg(input) .args(&shell[1..])
.stdin(Stdio::piped()) .arg(input)
.stdout(Stdio::piped()) .stdin(Stdio::piped())
.stderr(Stdio::piped()) .stdout(Stdio::piped())
.spawn() .stderr(Stdio::piped())
{ .spawn()
Ok(p) => process = p, {
Err(e) => { Ok(p) => process = p,
log::error!("Failed to start shell: {}", e); Err(e) => {
error = Some("Failed to start shell"); log::error!("Failed to start shell: {}", e);
return (0, 0, None); cx.editor.set_error("Failed to start shell".to_owned());
} return;
}
if pipe {
let stdin = process.stdin.as_mut().unwrap();
let fragment = range.fragment(doc.text().slice(..));
stdin.write_all(fragment.as_bytes()).unwrap();
} }
}
if pipe {
let stdin = process.stdin.as_mut().unwrap();
let fragment = range.fragment(doc.text().slice(..));
stdin.write_all(fragment.as_bytes()).unwrap();
}
let output = process.wait_with_output().unwrap(); let output = process.wait_with_output().unwrap();
if behavior != ShellBehavior::Filter { if behavior != ShellBehavior::Filter {
if !output.status.success() { if !output.status.success() {
let stderr = output.stderr; let stderr = output.stderr;
if !stderr.is_empty() { if !stderr.is_empty() {
log::error!( log::error!("Shell error: {}", String::from_utf8_lossy(&stderr));
"Shell error: {}",
String::from_utf8_lossy(&stderr)
);
}
error = Some("Command failed");
return (0, 0, None);
} }
let stdout = output.stdout; cx.editor.set_error("Command failed".to_owned());
let tendril; return;
match Tendril::try_from_byte_slice(&stdout) { }
Ok(t) => tendril = t, let stdout = output.stdout;
Err(_) => { let tendril;
error = Some("Process did not output valid UTF-8"); match Tendril::try_from_byte_slice(&stdout) {
return (0, 0, None); Ok(t) => tendril = t,
} Err(_) => {
cx.editor
.set_error("Process did not output valid UTF-8".to_owned());
return;
} }
let (from, to) = match behavior {
ShellBehavior::Replace => (range.from(), range.to()),
ShellBehavior::Insert => (range.from(), range.from()),
ShellBehavior::Append => (range.to(), range.to()),
_ => (range.from(), range.from()),
};
(from, to, Some(tendril))
} else {
// if the process exits successfully, keep the selection, otherwise delete it.
let keep = output.status.success();
(
range.from(),
if keep { range.from() } else { range.to() },
None,
)
} }
}); let (from, to) = match behavior {
ShellBehavior::Replace => (range.from(), range.to()),
ShellBehavior::Insert => (range.from(), range.from()),
ShellBehavior::Append => (range.to(), range.to()),
_ => (range.from(), range.from()),
};
changes.push((from, to, Some(tendril)));
} else {
// if the process exits successfully, keep the selection, otherwise delete it.
let keep = output.status.success();
changes.push((
range.from(),
if keep { range.from() } else { range.to() },
None,
));
}
}
let transaction = Transaction::change(doc.text(), changes.into_iter());
if let Some(error) = error { if behavior != ShellBehavior::Ignore {
cx.editor.set_error(error.to_owned());
} else if behavior != ShellBehavior::Ignore {
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id); doc.append_changes_to_history(view.id);
} }

Loading…
Cancel
Save