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 22ab25d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
10 changes: 10 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5037,6 +5037,16 @@ 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>,
) -> impl std::future::Future<Output = anyhow::Result<(Tendril, bool)>> {
let shell = shell.to_vec();
let cmd = cmd.to_string();
async move { shell_impl_async(&shell, &cmd, input).await }
}

async fn shell_impl_async(
shell: &[String],
cmd: &str,
Expand Down
35 changes: 17 additions & 18 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2061,17 +2061,12 @@ fn run_shell_command(
}

let shell = &cx.editor.config().shell;
let (output, success) = shell_impl(shell, &args.join(" "), None)?;
if success {
cx.editor.set_status("Command succeeded");
} else {
cx.editor.set_error("Command failed");
}

if !output.is_empty() {
let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| {
let job = async_shell_impl(shell, &args.join(" "), None);
let callback = async move {
let (output, success) = job.await?;
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| {
if !output.is_empty() {
let contents = ui::Markdown::new(
format!("```sh\n{}\n```", output),
editor.syn_loader.clone(),
Expand All @@ -2080,13 +2075,17 @@ fn run_shell_command(
helix_core::Position::new(editor.cursor().0.unwrap_or_default().row, 2),
));
compositor.replace_or_push("shell", popup);
},
));
Ok(call)
};

cx.jobs.callback(callback);
}
}
if success {
editor.set_status("Command succeeded");
} else {
editor.set_error("Command failed");
}
},
));
Ok(call)
};
cx.jobs.callback(callback);

Ok(())
}
Expand Down

0 comments on commit 22ab25d

Please sign in to comment.