From cfe80acb6f8893e20dc04ff9a99c4551c3129624 Mon Sep 17 00:00:00 2001 From: RoloEdits Date: Wed, 31 Jul 2024 16:29:14 -0700 Subject: [PATCH] output `stderr` in `:sh` popup if shell commands fail (#11239) * refactor(commands): output `stderr` in `:sh` popup * refactor(commands): switch to `from_utf8_lossy` This way something is always displayed. * refactor: no longer log stderr output on failure --- helix-term/src/commands.rs | 33 +++++++++++++++----------------- helix-term/src/commands/typed.rs | 2 +- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 2c5d2783b..e9d3a28ff 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5716,27 +5716,24 @@ async fn shell_impl_async( process.wait_with_output().await? }; - if !output.status.success() { - if !output.stderr.is_empty() { - let err = String::from_utf8_lossy(&output.stderr).to_string(); - log::error!("Shell error: {}", err); - bail!("Shell error: {}", err); - } - match output.status.code() { - Some(exit_code) => bail!("Shell command failed: status {}", exit_code), - None => bail!("Shell command failed"), + let output = if !output.status.success() { + if output.stderr.is_empty() { + match output.status.code() { + Some(exit_code) => bail!("Shell command failed: status {}", exit_code), + None => bail!("Shell command failed"), + } } + String::from_utf8_lossy(&output.stderr) + // Prioritize `stderr` output over `stdout` } else if !output.stderr.is_empty() { - log::debug!( - "Command printed to stderr: {}", - String::from_utf8_lossy(&output.stderr).to_string() - ); - } + let stderr = String::from_utf8_lossy(&output.stderr); + log::debug!("Command printed to stderr: {stderr}"); + stderr + } else { + String::from_utf8_lossy(&output.stdout) + }; - let str = std::str::from_utf8(&output.stdout) - .map_err(|_| anyhow!("Process did not output valid UTF-8"))?; - let tendril = Tendril::from(str); - Ok(tendril) + Ok(Tendril::from(output)) } fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index d20bdc17d..cd40e0532 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -2308,7 +2308,7 @@ fn run_shell_command( )); compositor.replace_or_push("shell", popup); } - editor.set_status("Command succeeded"); + editor.set_status("Command run"); }, )); Ok(call)