summaryrefslogtreecommitdiffstats
path: root/src/commands
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-07-19 21:33:08 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-07-19 22:18:23 -0400
commit0b8747eb37d6d943d90e15ed82858d99d1800425 (patch)
tree6cb910dc37343000296c2951e0f67ff269ee3efd /src/commands
parent98e9665e59d7af0b2c002f0e6007578b3e90aa69 (diff)
changed how commands are handled
- arguments no longer go through wordexp (still working on a good alternative) other changes: - changed update_contents to reload_contents - opening files with mimetype entries are now moved from unix.rs to mimetypes.rs
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/bulk_rename.rs12
-rw-r--r--src/commands/change_directory.rs10
-rw-r--r--src/commands/command_line.rs12
-rw-r--r--src/commands/mod.rs18
-rw-r--r--src/commands/open_file.rs14
-rw-r--r--src/commands/reload_dir.rs7
-rw-r--r--src/commands/rename_file.rs42
-rw-r--r--src/commands/tab_switch.rs2
8 files changed, 48 insertions, 69 deletions
diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs
index 1c7ab46..dab20c7 100644
--- a/src/commands/bulk_rename.rs
+++ b/src/commands/bulk_rename.rs
@@ -4,10 +4,10 @@ use std::process;
use rand::Rng;
+use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::window::JoshutoView;
-use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
#[derive(Clone, Debug)]
pub struct BulkRename;
@@ -24,10 +24,12 @@ impl BulkRename {
const PREFIX: &str = "joshuto-";
let editor = match std::env::var("EDITOR") {
Ok(s) => s,
- Err(_) => return Err(JoshutoError::new(
- JoshutoErrorKind::EnvVarNotPresent,
- String::from("EDITOR environment variable not set"),
- )),
+ Err(_) => {
+ return Err(JoshutoError::new(
+ JoshutoErrorKind::EnvVarNotPresent,
+ String::from("EDITOR environment variable not set"),
+ ));
+ }
};
/* generate a random file name to write to */
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs
index c062c8f..3ec1946 100644
--- a/src/commands/change_directory.rs
+++ b/src/commands/change_directory.rs
@@ -32,13 +32,13 @@ impl ChangeDirectory {
.pop_or_create(&path, &context.config_t.sort_option)?;
std::mem::swap(&mut curr_tab.curr_list, &mut curr_list);
+
+ curr_tab.history.insert(curr_list.file_path().clone(), curr_list);
curr_tab.curr_path = path.clone();
- if let Some(s) = path.parent() {
- curr_tab
- .history
- .populate_to_root(s, &context.config_t.sort_option)?;
- }
+ curr_tab
+ .history
+ .populate_to_root(path, &context.config_t.sort_option)?;
curr_tab.refresh(view, &context.config_t);
Ok(())
diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs
index 585ae23..4f67ee8 100644
--- a/src/commands/command_line.rs
+++ b/src/commands/command_line.rs
@@ -40,14 +40,12 @@ impl CommandLine {
Some(ind) => {
let (command, xs) = trimmed.split_at(ind);
let xs = xs.trim_start();
- let wexp = wordexp::wordexp(xs, wordexp::Wordexp::new(0), 0);
- let args: Vec<&str> = match wexp.as_ref() {
- Ok(wexp) => wexp.iter().collect(),
- Err(_) => Vec::new(),
- };
- commands::from_args(command, &args)?.execute(context, view)
+ let args: Vec<String> = vec![String::from(xs)];
+ commands::from_args(String::from(command), args)?.execute(context, view)
+ }
+ None => {
+ commands::from_args(String::from(trimmed), Vec::new())?.execute(context, view)
}
- None => commands::from_args(trimmed, &Vec::new())?.execute(context, view),
}
} else {
Ok(())
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 1009c69..d94e1a0 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -70,8 +70,8 @@ impl std::fmt::Display for CommandKeybind {
}
}
-pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoCommand>> {
- match command {
+pub fn from_args(command: String, args: Vec<String>) -> JoshutoResult<Box<JoshutoCommand>> {
+ match command.as_str() {
"bulk_rename" => Ok(Box::new(self::BulkRename::new())),
"cd" => match args.len() {
0 => match HOME_DIR.as_ref() {
@@ -81,7 +81,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
format!("{}: Cannot find home directory", command),
)),
},
- 1 => match args[0] {
+ 1 => match args[0].as_str() {
".." => Ok(Box::new(self::ParentDirectory::new())),
arg => Ok(Box::new(self::ChangeDirectory::new(PathBuf::from(arg)))),
},
@@ -98,7 +98,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
String::new(),
))),
1 => Ok(Box::new(self::CommandLine::new(
- String::from(args[0]),
+ String::from(args[0].as_str()),
String::new(),
))),
i => Err(JoshutoError::new(
@@ -159,7 +159,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
let mut options = fs_extra::dir::CopyOptions::new();
options.buffer_size = 1024 * 1024 * 4;
for arg in args {
- match *arg {
+ match arg.as_str() {
"--overwrite" => options.overwrite = true,
"--skip_exist" => options.skip_exist = true,
_ => {
@@ -176,7 +176,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
"reload_dir_list" => Ok(Box::new(self::ReloadDirList::new())),
"rename" => match args.len() {
1 => {
- let path: PathBuf = PathBuf::from(args[0]);
+ let path: PathBuf = PathBuf::from(args[0].as_str());
Ok(Box::new(self::RenameFile::new(path)))
}
i => Err(JoshutoError::new(
@@ -187,7 +187,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
"rename_append" => Ok(Box::new(self::RenameFileAppend::new())),
"rename_prepend" => Ok(Box::new(self::RenameFilePrepend::new())),
"search" => match args.len() {
- 1 => Ok(Box::new(self::Search::new(args[0]))),
+ 1 => Ok(Box::new(self::Search::new(args[0].as_str()))),
i => Err(JoshutoError::new(
JoshutoErrorKind::IOInvalidData,
format!("{}: Expected 1, got {}", command, i),
@@ -199,7 +199,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
let mut toggle = false;
let mut all = false;
for arg in args {
- match *arg {
+ match arg.as_str() {
"--toggle" => toggle = true,
"--all" => all = true,
_ => {
@@ -232,7 +232,7 @@ pub fn from_args(command: &str, args: &[&str]) -> JoshutoResult<Box<JoshutoComma
"toggle_hidden" => Ok(Box::new(self::ToggleHiddenFiles::new())),
inp => Err(JoshutoError::new(
JoshutoErrorKind::UnknownCommand,
- format!("{}: {}", "Unknown command", inp),
+ format!("Unknown command: {}", inp),
)),
}
}
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs
index e698913..c5c1c90 100644
--- a/src/commands/open_file.rs
+++ b/src/commands/open_file.rs
@@ -53,7 +53,10 @@ impl OpenFile {
if curr_tab.curr_list.need_update() {
curr_tab
.curr_list
- .update_contents(&context.config_t.sort_option)?;
+ .reload_contents(&context.config_t.sort_option)?;
+ curr_tab
+ .curr_list
+ .sort(context.config_t.sort_option.compare_func());
}
curr_tab.refresh(view, &context.config_t);
} else {
@@ -69,7 +72,10 @@ impl OpenFile {
if curr_tab.curr_list.need_update() {
curr_tab
.curr_list
- .update_contents(&context.config_t.sort_option)?;
+ .reload_contents(&context.config_t.sort_option)?;
+ curr_tab
+ .curr_list
+ .sort(context.config_t.sort_option.compare_func());
}
curr_tab.refresh(view, &context.config_t);
}
@@ -102,7 +108,7 @@ impl OpenFile {
if mimetype_options.is_empty() {
open::that(&paths[0]).unwrap();
} else {
- unix::open_with_entry(paths, &mimetype_options[0]);
+ mimetype_options[0].execute_with(paths);
}
ncurses::resetty();
ncurses::refresh();
@@ -177,7 +183,7 @@ impl OpenFileWith {
if s < mimetype_options.len() {
ncurses::savetty();
ncurses::endwin();
- unix::open_with_entry(paths, &mimetype_options[s]);
+ mimetype_options[s].execute_with(paths);
ncurses::resetty();
ncurses::refresh();
}
diff --git a/src/commands/reload_dir.rs b/src/commands/reload_dir.rs
index f126f02..4524e04 100644
--- a/src/commands/reload_dir.rs
+++ b/src/commands/reload_dir.rs
@@ -20,16 +20,17 @@ impl ReloadDirList {
pub fn reload(index: usize, context: &mut JoshutoContext) -> std::io::Result<()> {
let curr_tab = &mut context.tabs[index];
let sort_option = &context.config_t.sort_option;
- curr_tab.curr_list.update_contents(sort_option)?;
+ curr_tab.curr_list.reload_contents(sort_option)?;
if let Some(parent) = curr_tab.curr_list.file_path().parent() {
match curr_tab.history.entry(parent.to_path_buf().clone()) {
Entry::Occupied(mut entry) => {
let dirlist = entry.get_mut();
- dirlist.update_contents(sort_option)?;
+ dirlist.reload_contents(sort_option)?;
}
Entry::Vacant(entry) => {
- let s = JoshutoDirList::new(parent.to_path_buf().clone(), sort_option)?;
+ let mut s = JoshutoDirList::new(parent.to_path_buf().clone(), &sort_option)?;
+ s.sort(sort_option.compare_func());
entry.insert(s);
}
}
diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs
index 02eadfb..4686b2f 100644
--- a/src/commands/rename_file.rs
+++ b/src/commands/rename_file.rs
@@ -5,16 +5,6 @@ use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::window::JoshutoView;
-use rustyline::completion::{escape, Quote};
-
-#[cfg(unix)]
-static DEFAULT_BREAK_CHARS: [u8; 18] = [
- b' ', b'\t', b'\n', b'"', b'\\', b'\'', b'`', b'@', b'$', b'>', b'<', b'=', b';', b'|', b'&',
- b'{', b'(', b'\0',
-];
-#[cfg(unix)]
-static ESCAPE_CHAR: Option<char> = Some('\\');
-
#[derive(Clone, Debug)]
pub struct RenameFile {
path: path::PathBuf,
@@ -44,7 +34,7 @@ impl RenameFile {
let curr_tab = &mut context.tabs[context.curr_tab_index];
curr_tab
.curr_list
- .update_contents(&context.config_t.sort_option)?;
+ .reload_contents(&context.config_t.sort_option)?;
curr_tab.refresh_curr(&view.mid_win, &context.config_t);
curr_tab.refresh_preview(&view.right_win, &context.config_t);
Ok(())
@@ -119,18 +109,9 @@ impl std::fmt::Display for RenameFileAppend {
impl JoshutoRunnable for RenameFileAppend {
fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) -> JoshutoResult<()> {
let curr_list = &context.tabs[context.curr_tab_index].curr_list;
- let file_name = match curr_list.get_curr_ref() {
- Some(s) => {
- let escaped = escape(
- String::from(s.file_name()),
- ESCAPE_CHAR,
- &DEFAULT_BREAK_CHARS,
- Quote::None,
- );
- Some(escaped)
- }
- None => None,
- };
+ let file_name = curr_list
+ .get_curr_ref()
+ .and_then(|s| Some(String::from(s.file_name())));
if let Some(file_name) = file_name {
self.rename_file(context, view, file_name)?;
@@ -176,18 +157,9 @@ impl std::fmt::Display for RenameFilePrepend {
impl JoshutoRunnable for RenameFilePrepend {
fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) -> JoshutoResult<()> {
let curr_list = &context.tabs[context.curr_tab_index].curr_list;
- let file_name = match curr_list.get_curr_ref() {
- Some(s) => {
- let escaped = escape(
- String::from(s.file_name()),
- ESCAPE_CHAR,
- &DEFAULT_BREAK_CHARS,
- Quote::None,
- );
- Some(escaped)
- }
- None => None,
- };
+ let file_name = curr_list
+ .get_curr_ref()
+ .and_then(|s| Some(String::from(s.file_name())));
if let Some(file_name) = file_name {
self.rename_file(context, view, file_name)?;
diff --git a/src/commands/tab_switch.rs b/src/commands/tab_switch.rs
index f1adaa3..8f4553d 100644
--- a/src/commands/tab_switch.rs
+++ b/src/commands/tab_switch.rs
@@ -32,7 +32,7 @@ impl TabSwitch {
if curr_tab.curr_list.need_update() {
curr_tab
.curr_list
- .update_contents(&context.config_t.sort_option)?;
+ .reload_contents(&context.config_t.sort_option)?;
}
curr_tab.refresh(view, &context.config_t);
}