summaryrefslogtreecommitdiffstats
path: root/src/commands/shell.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/shell.rs')
-rw-r--r--src/commands/shell.rs44
1 files changed, 35 insertions, 9 deletions
diff --git a/src/commands/shell.rs b/src/commands/shell.rs
index 19e901d..b9cbd1c 100644
--- a/src/commands/shell.rs
+++ b/src/commands/shell.rs
@@ -1,26 +1,48 @@
use std::process;
-use crate::commands::{self, JoshutoCommand, JoshutoRunnable};
+use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
-use crate::ui::widgets::TuiTextField;
use crate::ui::TuiBackend;
#[derive(Clone, Debug)]
pub struct ShellCommand {
- pub command: String,
+ pub words: Vec<String>,
}
impl ShellCommand {
- pub fn new(command: String) -> Self {
- Self { command }
+ pub fn new(words: Vec<String>) -> Self {
+ Self { words }
}
pub const fn command() -> &'static str {
"console"
}
- pub fn shell_command(command: &str) -> std::io::Result<()> {
- let mut command = process::Command::new("sh").arg("-c").arg(command).spawn()?;
+ pub fn shell_command(&self, context: &mut JoshutoContext) -> std::io::Result<()> {
+ let mut command = process::Command::new(self.words[0].clone());
+ for word in self.words.iter().skip(1) {
+ match word.as_str() {
+ "%s" => {
+ let curr_tab = context.curr_tab_ref();
+ if let Some(curr_list) = curr_tab.curr_list_ref() {
+ let mut i = 0;
+ for entry in curr_list.selected_entries().map(|e| e.file_name()) {
+ command.arg(entry);
+ i += 1;
+ }
+ if i == 0 {
+ if let Some(entry) = curr_list.get_curr_ref() {
+ command.arg(entry.file_name());
+ }
+ }
+ }
+ }
+ s => {
+ command.arg(s);
+ }
+ };
+ }
+ command.status()?;
Ok(())
}
}
@@ -29,14 +51,18 @@ impl JoshutoCommand for ShellCommand {}
impl std::fmt::Display for ShellCommand {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "{}: sh -c '{}'", Self::command(), self.command)
+ write!(f, "{}: {:?}", Self::command(), self.words.join(" "))
}
}
impl JoshutoRunnable for ShellCommand {
fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> {
backend.terminal_drop();
- let res = Self::shell_command(self.command.as_str());
+ let res = self.shell_command(context);
+ ReloadDirList::soft_reload(context.curr_tab_index, context)?;
+ context
+ .message_queue
+ .push_back(format!("Finished: {}", self.words.join(" ")));
backend.terminal_restore()?;
res?;
Ok(())