summaryrefslogtreecommitdiffstats
path: root/src/commands/shell.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-09-19 21:21:33 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-09-19 21:21:33 -0400
commit7b4c611ed4d804bd52aeda0a619a54bea9b1e13d (patch)
tree7ba31fff7d5de171aadfc32c67f81dd64428b3ba /src/commands/shell.rs
parent7741d4d2c8798ad0bb609e97fb3bda86c5318a36 (diff)
Change command to use an enum instead of polymorphism
Diffstat (limited to 'src/commands/shell.rs')
-rw-r--r--src/commands/shell.rs89
1 files changed, 34 insertions, 55 deletions
diff --git a/src/commands/shell.rs b/src/commands/shell.rs
index 69406b9..d813ed6 100644
--- a/src/commands/shell.rs
+++ b/src/commands/shell.rs
@@ -1,69 +1,48 @@
use std::process;
-use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::ui::TuiBackend;
-#[derive(Clone, Debug)]
-pub struct ShellCommand {
- pub words: Vec<String>,
-}
-
-impl ShellCommand {
- pub fn new(words: Vec<String>) -> Self {
- Self { words }
- }
- pub const fn command() -> &'static str {
- "console"
- }
+use super::reload;
- 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" => {
- if let Some(curr_list) =
- context.tab_context_ref().curr_tab_ref().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());
- }
+pub fn shell_command(context: &mut JoshutoContext, words: &[String]) -> std::io::Result<()> {
+ let mut command = process::Command::new(words[0].clone());
+ for word in words.iter().skip(1) {
+ match (*word).as_str() {
+ "%s" => {
+ if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().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.curr_entry_ref() {
+ command.arg(entry.file_name());
}
}
}
- s => {
- command.arg(s);
- }
- };
- }
- command.status()?;
- Ok(())
- }
-}
-
-impl JoshutoCommand for ShellCommand {}
-
-impl std::fmt::Display for ShellCommand {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "{}: {:?}", Self::command(), self.words.join(" "))
+ }
+ s => {
+ command.arg(s);
+ }
+ };
}
+ command.status()?;
+ Ok(())
}
-impl JoshutoRunnable for ShellCommand {
- fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> {
- backend.terminal_drop();
- let res = self.shell_command(context);
- ReloadDirList::soft_reload(context.tab_context_ref().get_index(), context)?;
- context.push_msg(format!("Finished: {}", self.words.join(" ")));
- backend.terminal_restore()?;
- res?;
- Ok(())
- }
+pub fn shell(
+ context: &mut JoshutoContext,
+ backend: &mut TuiBackend,
+ words: &[String],
+) -> JoshutoResult<()> {
+ backend.terminal_drop();
+ let res = shell_command(context, words);
+ reload::soft_reload(context.tab_context_ref().get_index(), context)?;
+ context.push_msg(format!("Finished: {}", words.join(" ")));
+ backend.terminal_restore()?;
+ res?;
+ Ok(())
}