aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyosuke Hayashi2022-05-02 14:15:02 +0000
committerGitHub2022-05-02 14:15:02 +0000
commitf85f0b72729ebbc1d93020b1c1a52a2f2fb3bc59 (patch)
tree17f4fb8a2a0ea60b8a8baea90b206f16096b3f68
parent166818359030cd1649327160c9020d943a8de244 (diff)
Add run-shell-command for Commands (#1682)
* add run_shell_command * docgen * fix command name Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> * refactored Info::new * show 'Command failed' if execution fails * TypedCommand takes care of error handling and printing the error to the statusline. * docgen * use Popup instead of autoinfo * remove to_string in format! * Revert chage in info.rs * Show "Command succeed" when success * Fix info.rs Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>
-rw-r--r--book/src/generated/typable-cmd.md1
-rw-r--r--helix-term/src/commands/typed.rs44
2 files changed, 45 insertions, 0 deletions
diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md
index ac210202..df0841b6 100644
--- a/book/src/generated/typable-cmd.md
+++ b/book/src/generated/typable-cmd.md
@@ -63,3 +63,4 @@
| `:config-reload` | Refreshes helix's config. |
| `:config-open` | Open the helix config.toml file. |
| `:pipe` | Pipe each selection to the shell command. |
+| `:run-shell-command`, `:sh` | Run a shell command |
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 394760a2..373c7018 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -1116,6 +1116,43 @@ fn pipe(
Ok(())
}
+fn run_shell_command(
+ cx: &mut compositor::Context,
+ args: &[Cow<str>],
+ _event: PromptEvent,
+) -> anyhow::Result<()> {
+ let shell = &cx.editor.config().shell;
+ let (output, success) = shell_impl(shell, &args.join(" "), None)?;
+ if success {
+ cx.editor.set_status("Command succeed");
+ } else {
+ cx.editor.set_error("Command failed");
+ }
+
+ if !output.is_empty() {
+ let callback = async move {
+ let call: job::Callback =
+ Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
+ let contents = ui::Markdown::new(
+ format!("```sh\n{}\n```", output),
+ editor.syn_loader.clone(),
+ );
+ let mut popup = Popup::new("shell", contents);
+ popup.set_position(Some(helix_core::Position::new(
+ editor.cursor().0.unwrap_or_default().row,
+ 2,
+ )));
+ compositor.replace_or_push("shell", popup);
+ });
+ Ok(call)
+ };
+
+ cx.jobs.callback(callback);
+ }
+
+ Ok(())
+}
+
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
@@ -1561,6 +1598,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: pipe,
completer: None,
},
+ TypableCommand {
+ name: "run-shell-command",
+ aliases: &["sh"],
+ doc: "Run a shell command",
+ fun: run_shell_command,
+ completer: Some(completers::directory),
+ },
];
pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =