Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run-shell-command for Commands #1682

Merged
merged 13 commits into from
May 2, 2022
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@
| `:sort` | Sort ranges in selection. |
| `:rsort` | Sort ranges in selection in reverse order. |
| `:tree-sitter-subtree`, `:ts-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. |
| `:run_shell_command`, `:sh` | Run a shell command |
28 changes: 28 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,27 @@ pub mod cmd {
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) = match shell_impl(shell, &args.join(" "), None) {
Ok(result) => result,
Err(err) => {
cx.editor.set_error(err.to_string());
return Ok(());
}
};
archseer marked this conversation as resolved.
Show resolved Hide resolved

if !output.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't show anything if there is no output? How does user know if it finished running?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the shell command is executed synchronously, the user will know it's finished when it releases from the freeze.
I think it's one way to display a message like "Command successes" in the status area if success.

cx.editor.autoinfo = Some(Info::from_string("Run shell command", output.to_string()));
}

Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -3278,6 +3299,13 @@ pub mod cmd {
fun: tree_sitter_subtree,
completer: None,
},
TypableCommand {
name: "run_shell_command",
hayashikun marked this conversation as resolved.
Show resolved Hide resolved
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>> =
Expand Down
18 changes: 18 additions & 0 deletions helix-view/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ impl Info {
}
}

pub fn from_string(title: &str, body: String) -> Self {
if body.is_empty() {
return Self {
title: title.to_string(),
height: 1,
width: title.len() as u16,
text: "".to_string(),
};
}

Self {
title: title.to_string(),
width: body.lines().map(|l| l.width()).max().unwrap() as u16,
height: body.lines().count() as u16,
text: body,
Copy link
Contributor

@pickfire pickfire Feb 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
text: body,
text,

Using text here is clearer I think? But probably need to change the above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
But the information is now displayed in Popup, should I revert info.rs change?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey is this going into the main branch any time soon?

}
}
archseer marked this conversation as resolved.
Show resolved Hide resolved

pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
let body = body
.into_iter()
Expand Down