Skip to content

Commit

Permalink
add async shell command
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah committed Mar 20, 2023
1 parent 7704965 commit 9b5e430
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@
| `:pipe` | Pipe each selection to the shell command. |
| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
| `:run-shell-command`, `:sh` | Run a shell command |
| `:run-async-shell-command`, `:async-sh` | Run a shell command asynchronously |
| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
7 changes: 7 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5037,6 +5037,13 @@ fn shell_impl(shell: &[String], cmd: &str, input: Option<Rope>) -> anyhow::Resul
tokio::task::block_in_place(|| helix_lsp::block_on(shell_impl_async(shell, cmd, input)))
}

fn async_shell_impl(shell: &[String], cmd: &str, input: Option<Rope>) -> anyhow::Result<()> {
let shell = shell.to_vec();
let cmd = cmd.to_string();
tokio::task::spawn(async move { shell_impl_async(&shell, &cmd, input).await.map(|_| ()) });
Ok(())
}

async fn shell_impl_async(
shell: &[String],
cmd: &str,
Expand Down
22 changes: 21 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,19 @@ fn run_shell_command(
Ok(())
}

fn run_async_shell_command(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let shell = &cx.editor.config().shell;
async_shell_impl(shell, &args.join(" "), None)
}

fn reset_diff_change(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down Expand Up @@ -2688,7 +2701,14 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: run_shell_command,
signature: CommandSignature::all(completers::filename)
},
TypableCommand {
TypableCommand {
name: "run-async-shell-command",
aliases: &["async-sh"],
doc: "Run a shell command asynchronously",
fun: run_async_shell_command,
signature: CommandSignature::all(completers::filename)
},
TypableCommand {
name: "reset-diff-change",
aliases: &["diffget", "diffg"],
doc: "Reset the diff change at the cursor position.",
Expand Down

0 comments on commit 9b5e430

Please sign in to comment.