on master help sign in
Refactor a bit further
commit9b96bb5ac802189f567b101f7c8da4b370a2fdd1 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 31 Aug 2021, 18:24 (5 years ago) parent4a76ea8
1 file changed+62−65
helix-term/src/commands.rs+62−65
helix-term/src/commands.rs+62−65
@@ -4342,80 +4342,77 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
43424342 Some('|'),
43434343 |_input: &str| Vec::new(),
43444344 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
4345+ if event != PromptEvent::Validate {
4346+ return;
4347+ }
43454348 let shell = &cx.editor.config.shell;
4346− if event == PromptEvent::Validate {
4347− let (view, doc) = current!(cx.editor);
4348− let selection = doc.selection(view.id);
4349+ let (view, doc) = current!(cx.editor);
4350+ let selection = doc.selection(view.id);
43494351
4350− let mut changes = Vec::with_capacity(selection.len());
4351−
4352− for range in selection.ranges() {
4353− let mut process;
4354− match Command::new(&shell[0])
4355− .args(&shell[1..])
4356− .arg(input)
4357− .stdin(Stdio::piped())
4358− .stdout(Stdio::piped())
4359− .stderr(Stdio::piped())
4360− .spawn()
4361− {
4362− Ok(p) => process = p,
4363− Err(e) => {
4364− log::error!("Failed to start shell: {}", e);
4365− cx.editor.set_error("Failed to start shell".to_owned());
4366− return;
4367− }
4368− }
4369− if pipe {
4370− let stdin = process.stdin.as_mut().unwrap();
4371− let fragment = range.fragment(doc.text().slice(..));
4372− stdin.write_all(fragment.as_bytes()).unwrap();
4352+ let mut changes = Vec::with_capacity(selection.len());
4353+
4354+ for range in selection.ranges() {
4355+ let mut process = match Command::new(&shell[0])
4356+ .args(&shell[1..])
4357+ .arg(input)
4358+ .stdin(Stdio::piped())
4359+ .stdout(Stdio::piped())
4360+ .stderr(Stdio::piped())
4361+ .spawn()
4362+ {
4363+ Ok(process) => process,
4364+ Err(e) => {
4365+ log::error!("Failed to start shell: {}", e);
4366+ cx.editor.set_error("Failed to start shell".to_owned());
4367+ return;
43734368 }
4369+ };
4370+ if pipe {
4371+ let stdin = process.stdin.as_mut().unwrap();
4372+ let fragment = range.fragment(doc.text().slice(..));
4373+ stdin.write_all(fragment.as_bytes()).unwrap();
4374+ }
4375+ let output = process.wait_with_output().unwrap();
43744376
4375− let output = process.wait_with_output().unwrap();
4376− if behavior != ShellBehavior::Filter {
4377− if !output.status.success() {
4378− let stderr = output.stderr;
4379− if !stderr.is_empty() {
4380− log::error!("Shell error: {}", String::from_utf8_lossy(&stderr));
4381− }
4382− cx.editor.set_error("Command failed".to_owned());
4383− return;
4377+ if behavior != ShellBehavior::Filter {
4378+ if !output.status.success() {
4379+ if !output.stderr.is_empty() {
4380+ log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
43844381 }
4385− let stdout = output.stdout;
4386− let tendril;
4387− match Tendril::try_from_byte_slice(&stdout) {
4388− Ok(t) => tendril = t,
4389− Err(_) => {
4390− cx.editor
4391− .set_error("Process did not output valid UTF-8".to_owned());
4392− return;
4393− }
4394− }
4395− let (from, to) = match behavior {
4396− ShellBehavior::Replace => (range.from(), range.to()),
4397− ShellBehavior::Insert => (range.from(), range.from()),
4398− ShellBehavior::Append => (range.to(), range.to()),
4399− _ => (range.from(), range.from()),
4400− };
4401− changes.push((from, to, Some(tendril)));
4402− } else {
4403− // if the process exits successfully, keep the selection, otherwise delete it.
4404− let keep = output.status.success();
4405− changes.push((
4406− range.from(),
4407− if keep { range.from() } else { range.to() },
4408− None,
4409− ));
4382+ cx.editor.set_error("Command failed".to_owned());
4383+ return;
44104384 }
4385+ let tendril = match Tendril::try_from_byte_slice(&output.stdout) {
4386+ Ok(tendril) => tendril,
4387+ Err(_) => {
4388+ cx.editor
4389+ .set_error("Process did not output valid UTF-8".to_owned());
4390+ return;
4391+ }
4392+ };
4393+ let (from, to) = match behavior {
4394+ ShellBehavior::Replace => (range.from(), range.to()),
4395+ ShellBehavior::Insert => (range.from(), range.from()),
4396+ ShellBehavior::Append => (range.to(), range.to()),
4397+ _ => (range.from(), range.from()),
4398+ };
4399+ changes.push((from, to, Some(tendril)));
4400+ } else {
4401+ // if the process exits successfully, keep the selection, otherwise delete it.
4402+ let keep = output.status.success();
4403+ changes.push((
4404+ range.from(),
4405+ if keep { range.from() } else { range.to() },
4406+ None,
4407+ ));
44114408 }
4409+ }
44124410
4413− let transaction = Transaction::change(doc.text(), changes.into_iter());
4411+ let transaction = Transaction::change(doc.text(), changes.into_iter());
44144412
4415− if behavior != ShellBehavior::Ignore {
4416− doc.apply(&transaction, view.id);
4417− doc.append_changes_to_history(view.id);
4418− }
4413+ if behavior != ShellBehavior::Ignore {
4414+ doc.apply(&transaction, view.id);
4415+ doc.append_changes_to_history(view.id);
44194416 }
44204417 },
44214418 );