Skip to content

Commit 6dfc949

Browse files
committed
Add support for variables in shell commands
1 parent a6dd02c commit 6dfc949

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/app.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl Tile {
315315
}
316316

317317
Message::RunFunction(command) => {
318-
command.execute(&self.config);
318+
command.execute(&self.config, &self.query);
319319

320320
if self.config.buffer_rules.clear_on_enter {
321321
window::latest()
@@ -430,13 +430,21 @@ impl Tile {
430430

431431
let mut exact: Vec<App> = filter_vec
432432
.par_iter()
433-
.filter(|x| x.name_lc == query)
433+
.filter(|x| match &x.open_command {
434+
Function::RunShellCommand(_) => x
435+
.name_lc
436+
.starts_with(query.split_once(" ").unwrap_or((&query, "")).0),
437+
_ => x.name_lc == query,
438+
})
434439
.cloned()
435440
.collect();
436441

437442
let mut prefix: Vec<App> = filter_vec
438443
.par_iter()
439-
.filter(|x| x.name_lc != query && x.name_lc.starts_with(&query))
444+
.filter(|x| match x.open_command {
445+
Function::RunShellCommand(_) => false,
446+
_ => x.name_lc != query && x.name_lc.starts_with(&query),
447+
})
440448
.cloned()
441449
.collect();
442450

src/commands.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,31 @@ use crate::config::Config;
99
#[derive(Debug, Clone)]
1010
pub enum Function {
1111
OpenApp(String),
12-
RunShellCommand(Vec<String>),
12+
RunShellCommand(String),
1313
RandomVar(i32),
1414
GoogleSearch(String),
1515
OpenPrefPane,
1616
Quit,
1717
}
1818

1919
impl Function {
20-
pub fn execute(&self, config: &Config) {
20+
pub fn execute(&self, config: &Config, query: &str) {
2121
match self {
2222
Function::OpenApp(path) => {
2323
NSWorkspace::new().openURL(&NSURL::fileURLWithPath(
2424
&objc2_foundation::NSString::from_str(path),
2525
));
2626
}
2727
Function::RunShellCommand(shell_command) => {
28+
let mut final_command = shell_command.to_owned();
29+
30+
for (argument_num, argument) in query.split_whitespace().enumerate() {
31+
final_command =
32+
final_command.replace(&format!("$var{}", argument_num), argument);
33+
}
2834
Command::new("sh")
2935
.arg("-c")
30-
.arg(shell_command.join(" "))
36+
.arg(final_command)
3137
.status()
3238
.ok();
3339
}

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Default for Buffer {
122122
/// Alias is the text that is used to call this command / search for it
123123
#[derive(Debug, Clone, Deserialize, Serialize)]
124124
pub struct Shelly {
125-
command: Vec<String>,
125+
command: String,
126126
icon_path: Option<String>,
127127
alias: String,
128128
alias_lc: String,

0 commit comments

Comments
 (0)