Refactor a bit further

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

@ -4342,16 +4342,17 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
Some('|'),
|_input: &str| Vec::new(),
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
if event != PromptEvent::Validate {
return;
}
let shell = &cx.editor.config.shell;
if event == PromptEvent::Validate {
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
let mut changes = Vec::with_capacity(selection.len());
for range in selection.ranges() {
let mut process;
match Command::new(&shell[0])
let mut process = match Command::new(&shell[0])
.args(&shell[1..])
.arg(input)
.stdin(Stdio::piped())
@ -4359,39 +4360,36 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
.stderr(Stdio::piped())
.spawn()
{
Ok(p) => process = p,
Ok(process) => process,
Err(e) => {
log::error!("Failed to start shell: {}", e);
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();
}
let output = process.wait_with_output().unwrap();
if behavior != ShellBehavior::Filter {
if !output.status.success() {
let stderr = output.stderr;
if !stderr.is_empty() {
log::error!("Shell error: {}", String::from_utf8_lossy(&stderr));
if !output.stderr.is_empty() {
log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
}
cx.editor.set_error("Command failed".to_owned());
return;
}
let stdout = output.stdout;
let tendril;
match Tendril::try_from_byte_slice(&stdout) {
Ok(t) => tendril = t,
let tendril = match Tendril::try_from_byte_slice(&output.stdout) {
Ok(tendril) => tendril,
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()),
@ -4416,7 +4414,6 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}
}
},
);

Loading…
Cancel
Save