on master help sign in
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>

commitf85f0b72729ebbc1d93020b1c1a52a2f2fb3bc59 authorRyosuke Hayashi <hayashi@hayashikun.com> dateMon 2 May 2022, 23:15 (4 years ago) parent1668183
2 files changed+45
book/src/generated/typable-cmd.md+1 helix-term/src/commands/typed.rs+44
book/src/generated/typable-cmd.md+1
@@ -63,3 +63,4 @@
6363| `:config-reload` | Refreshes helix's config. |
6464| `:config-open` | Open the helix config.toml file. |
6565| `:pipe` | Pipe each selection to the shell command. |
66+| `:run-shell-command`, `:sh` | Run a shell command |
helix-term/src/commands/typed.rs+44
@@ -1116,6 +1116,43 @@ fn pipe(
11161116 Ok(())
11171117}
11181118
1119+fn run_shell_command(
1120+ cx: &mut compositor::Context,
1121+ args: &[Cow<str>],
1122+ _event: PromptEvent,
1123+) -> anyhow::Result<()> {
1124+ let shell = &cx.editor.config().shell;
1125+ let (output, success) = shell_impl(shell, &args.join(" "), None)?;
1126+ if success {
1127+ cx.editor.set_status("Command succeed");
1128+ } else {
1129+ cx.editor.set_error("Command failed");
1130+ }
1131+
1132+ if !output.is_empty() {
1133+ let callback = async move {
1134+ let call: job::Callback =
1135+ Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
1136+ let contents = ui::Markdown::new(
1137+ format!("```sh\n{}\n```", output),
1138+ editor.syn_loader.clone(),
1139+ );
1140+ let mut popup = Popup::new("shell", contents);
1141+ popup.set_position(Some(helix_core::Position::new(
1142+ editor.cursor().0.unwrap_or_default().row,
1143+ 2,
1144+ )));
1145+ compositor.replace_or_push("shell", popup);
1146+ });
1147+ Ok(call)
1148+ };
1149+
1150+ cx.jobs.callback(callback);
1151+ }
1152+
1153+ Ok(())
1154+}
1155+
11191156pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
11201157 TypableCommand {
11211158 name: "quit",
@@ -1561,6 +1598,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
15611598 fun: pipe,
15621599 completer: None,
15631600 },
1601+ TypableCommand {
1602+ name: "run-shell-command",
1603+ aliases: &["sh"],
1604+ doc: "Run a shell command",
1605+ fun: run_shell_command,
1606+ completer: Some(completers::directory),
1607+ },
15641608 ];
15651609
15661610pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =