Refactor a bit further

imgbot
Blaž Hrastnik 3 years ago
parent 4a76ea8f88
commit 9b96bb5ac8

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

Loading…
Cancel
Save