summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-06-11 18:42:52 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-06-11 18:42:52 -0400
commit362dc42aeb00c799102988e13667cfc94d37aa72 (patch)
tree1c4c0fe7f0fd910bd6bdd04578b5425420a98c5c /src
parent92bcbb950cb1f2167279ee331f924799c3498c14 (diff)
parent16efbb53efdd4d4935589425fe60e651a0e654ad (diff)
Merge branch 'main' into dev
Diffstat (limited to 'src')
-rw-r--r--src/commands/key_command.rs15
-rw-r--r--src/commands/mod.rs2
-rw-r--r--src/commands/sub_process.rs (renamed from src/commands/shell.rs)32
3 files changed, 34 insertions, 15 deletions
diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs
index 556ac87..d4f94be 100644
--- a/src/commands/key_command.rs
+++ b/src/commands/key_command.rs
@@ -61,7 +61,7 @@ pub enum KeyCommand {
SelectFiles(String, SelectOption),
SetMode,
- ShellCommand(Vec<String>),
+ SubProcess(Vec<String>, bool),
ShowWorkers,
ToggleHiddenFiles,
@@ -122,7 +122,8 @@ impl KeyCommand {
Self::SelectFiles(_, _) => "select",
Self::SetMode => "set_mode",
- Self::ShellCommand(_) => "shell",
+ Self::SubProcess(_, false) => "shell",
+ Self::SubProcess(_, true) => "spawn",
Self::ShowWorkers => "show_workers",
Self::ToggleHiddenFiles => "toggle_hidden",
@@ -313,8 +314,8 @@ impl std::str::FromStr for KeyCommand {
}
}
"set_mode" => Ok(Self::SetMode),
- "shell" => match shell_words::split(arg) {
- Ok(s) if !s.is_empty() => Ok(Self::ShellCommand(s)),
+ "shell" | "spawn" => match shell_words::split(arg) {
+ Ok(s) if !s.is_empty() => Ok(Self::SubProcess(s, command == "spawn")),
Ok(_) => Err(JoshutoError::new(
JoshutoErrorKind::InvalidParameters,
format!("{}: No commands given", command),
@@ -409,7 +410,9 @@ impl AppExecute for KeyCommand {
selection::select_files(context, pattern.as_str(), &options)
}
Self::SetMode => set_mode::set_mode(context, backend),
- Self::ShellCommand(v) => shell::shell(context, backend, v.as_slice()),
+ Self::SubProcess(v, spawn) => {
+ sub_process::sub_process(context, backend, v.as_slice(), *spawn)
+ }
Self::ShowWorkers => show_workers::show_workers(context, backend),
Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context),
@@ -444,7 +447,7 @@ impl std::fmt::Display for KeyCommand {
Self::SelectFiles(pattern, options) => {
write!(f, "{} {} {}", self.command(), pattern, options)
}
- Self::ShellCommand(c) => write!(f, "{} {:?}", self.command(), c),
+ Self::SubProcess(c, _) => write!(f, "{} {:?}", self.command(), c),
Self::Sort(t) => write!(f, "{} {}", self.command(), t),
Self::TabSwitch(i) => write!(f, "{} {}", self.command(), i),
_ => write!(f, "{}", self.command()),
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 5ef00aa..bcf4b3a 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -17,10 +17,10 @@ pub mod search_skim;
pub mod search_string;
pub mod selection;
pub mod set_mode;
-pub mod shell;
pub mod show_hidden;
pub mod show_workers;
pub mod sort;
+pub mod sub_process;
pub mod tab_ops;
pub mod touch_file;
diff --git a/src/commands/shell.rs b/src/commands/sub_process.rs
index 7823d91..e3e8f66 100644
--- a/src/commands/shell.rs
+++ b/src/commands/sub_process.rs
@@ -1,13 +1,16 @@
-use std::process;
-
use crate::context::AppContext;
use crate::error::JoshutoResult;
use crate::ui::TuiBackend;
+use std::process::{Command, Stdio};
use super::reload;
-pub fn shell_command(context: &mut AppContext, words: &[String]) -> std::io::Result<()> {
- let mut command = process::Command::new(words[0].clone());
+fn execute_sub_process(
+ context: &mut AppContext,
+ words: &[String],
+ spawn: bool,
+) -> std::io::Result<()> {
+ let mut command = Command::new(words[0].clone());
for word in words.iter().skip(1) {
match (*word).as_str() {
"%s" => {
@@ -29,19 +32,32 @@ pub fn shell_command(context: &mut AppContext, words: &[String]) -> std::io::Res
}
};
}
- command.status()?;
+ if spawn {
+ command
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()?;
+ } else {
+ command.status()?;
+ }
Ok(())
}
-pub fn shell(
+/// Handler for Joshuto's `shell` and `spawn` commands.
+pub fn sub_process(
context: &mut AppContext,
backend: &mut TuiBackend,
words: &[String],
+ spawn: bool,
) -> JoshutoResult<()> {
backend.terminal_drop();
- let res = shell_command(context, words);
+ let res = execute_sub_process(context, words, spawn);
reload::soft_reload(context.tab_context_ref().index, context)?;
- context.push_msg(format!("Finished: {}", words.join(" ")));
+ context.push_msg(format!(
+ "{}: {}",
+ if spawn { "Spawned" } else { "Finished" },
+ words.join(" ")
+ ));
backend.terminal_restore()?;
res?;
Ok(())