diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-05 11:29:36 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-05 11:29:36 -0500 |
commit | c6c4c5c2b2ceecfc2beeb15502c78e0358fafc02 (patch) | |
tree | b566753837eac13c8fd6661d1d1a62855ed3f721 | |
parent | cd1eaaeb2c52abf455a886d0115ed856288d2422 (diff) |
add support for creating new directories
- make destroy() not take in &mut self
-rw-r--r-- | src/joshuto.rs | 2 | ||||
-rw-r--r-- | src/joshuto/command.rs | 4 | ||||
-rw-r--r-- | src/joshuto/command/file_operation.rs | 2 | ||||
-rw-r--r-- | src/joshuto/command/new_directory.rs | 70 | ||||
-rw-r--r-- | src/joshuto/command/open_file.rs | 2 | ||||
-rw-r--r-- | src/joshuto/keymap.rs | 12 | ||||
-rw-r--r-- | src/joshuto/window.rs | 2 |
7 files changed, 89 insertions, 5 deletions
diff --git a/src/joshuto.rs b/src/joshuto.rs index d7fb9a8..62ebb06 100644 --- a/src/joshuto.rs +++ b/src/joshuto.rs @@ -181,7 +181,7 @@ fn recurse_get_keycommand<'a>(keymap: &'a HashMap<i32, CommandKeybind>) let keymap_len = keymap.len(); - let mut win = window::JoshutoPanel::new(keymap_len as i32 + 1, term_cols, + let win = window::JoshutoPanel::new(keymap_len as i32 + 1, term_cols, ((term_rows - keymap_len as i32 - 2) as usize, 0)); let mut display_vec: Vec<String> = Vec::with_capacity(keymap_len); diff --git a/src/joshuto/command.rs b/src/joshuto/command.rs index 60f0417..35a199d 100644 --- a/src/joshuto/command.rs +++ b/src/joshuto/command.rs @@ -37,6 +37,9 @@ pub use self::file_operation::DeleteFiles; pub use self::file_operation::RenameFile; pub use self::file_operation::RenameFileMethod; +mod new_directory; +pub use self::new_directory::NewDirectory; + mod show_hidden; pub use self::show_hidden::ToggleHiddenFiles; @@ -189,6 +192,7 @@ pub fn from_args(args: &[&str]) -> Option<Box<dyn JoshutoCommand>> } Some(Box::new(self::RenameFile::new(method))) } + "new_directory" => Some(Box::new(self::NewDirectory::new())), "select_files" => { let mut toggle = false; let mut all = false; diff --git a/src/joshuto/command/file_operation.rs b/src/joshuto/command/file_operation.rs index ba36b76..3a9bc85 100644 --- a/src/joshuto/command/file_operation.rs +++ b/src/joshuto/command/file_operation.rs @@ -380,7 +380,7 @@ impl RenameFile { let mut term_cols: i32 = 0; ncurses::getmaxyx(ncurses::stdscr(), &mut term_rows, &mut term_cols); - let mut win = window::JoshutoPanel::new(1, term_cols, (term_rows as usize - 1, 0)); + let win = window::JoshutoPanel::new(1, term_cols, (term_rows as usize - 1, 0)); ncurses::keypad(win.win, true); const PROMPT: &str = ":rename_file "; diff --git a/src/joshuto/command/new_directory.rs b/src/joshuto/command/new_directory.rs new file mode 100644 index 0000000..da9ec59 --- /dev/null +++ b/src/joshuto/command/new_directory.rs @@ -0,0 +1,70 @@ +extern crate ncurses; + +use std; +use std::fmt; +use std::path; + +use joshuto; +use joshuto::ui; +use joshuto::window; + +use joshuto::command; + +#[derive(Debug)] +pub struct NewDirectory; + +impl NewDirectory { + pub fn new() -> Self { NewDirectory } + pub fn command() -> &'static str { "new_directory" } +} + +impl command::JoshutoCommand for NewDirectory {} + +impl std::fmt::Display for NewDirectory { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result + { + write!(f, "{}", Self::command()) + } +} + +impl command::Runnable for NewDirectory { + fn execute(&self, context: &mut joshuto::JoshutoContext) + { + let mut term_rows: i32 = 0; + let mut term_cols: i32 = 0; + ncurses::getmaxyx(ncurses::stdscr(), &mut term_rows, &mut term_cols); + + let win = window::JoshutoPanel::new(1, term_cols, (term_rows as usize - 1, 0)); + ncurses::keypad(win.win, true); + + const PROMPT: &str = ":mkdir "; + ncurses::waddstr(win.win, PROMPT); + + win.move_to_top(); + ncurses::doupdate(); + + if let Some(user_input) = ui::get_str(&win, (0, PROMPT.len() as i32)) { + let path = path::PathBuf::from(user_input); + match std::fs::create_dir_all(&path) { + Ok(_) => { + context.reload_dirlists(); + + ui::redraw_view(&context.views.left_win, context.parent_list.as_ref()); + ui::redraw_view(&context.views.mid_win, context.curr_list.as_ref()); + ui::redraw_view(&context.views.right_win, context.preview_list.as_ref()); + + ui::redraw_status(&context.views, context.curr_list.as_ref(), + &context.curr_path, + &context.config_t.username, &context.config_t.hostname); + }, + Err(e) => { + ui::wprint_err(&context.views.bot_win, e.to_string().as_str()); + }, + } + } + + win.destroy(); + ncurses::update_panels(); + ncurses::doupdate(); + } +} diff --git a/src/joshuto/command/open_file.rs b/src/joshuto/command/open_file.rs index 261eb5b..586defe 100644 --- a/src/joshuto/command/open_file.rs +++ b/src/joshuto/command/open_file.rs @@ -185,7 +185,7 @@ impl OpenFileWith { }; let option_size = mimetype_options.len(); - let mut win = window::JoshutoPanel::new(option_size as i32 + 2, term_cols, + let win = window::JoshutoPanel::new(option_size as i32 + 2, term_cols, (term_rows as usize - option_size - 2, 0)); ncurses::keypad(win.win, true); diff --git a/src/joshuto/keymap.rs b/src/joshuto/keymap.rs index e11418e..1b90f24 100644 --- a/src/joshuto/keymap.rs +++ b/src/joshuto/keymap.rs @@ -144,12 +144,22 @@ impl JoshutoKeymap { options.overwrite = true; let command = CommandKeybind::SimpleKeybind( Box::new(command::PasteFiles::new(options))); - subkeymap.insert(Keycode::UPPER_P as i32, command); + subkeymap.insert(Keycode::LOWER_O as i32, command); let command = CommandKeybind::CompositeKeybind(subkeymap); keymaps.insert(Keycode::LOWER_P as i32, command); } + { + let mut subkeymap: HashMap<i32, CommandKeybind> = HashMap::new(); + let command = CommandKeybind::SimpleKeybind( + Box::new(command::NewDirectory::new())); + subkeymap.insert(Keycode::LOWER_K as i32, command); + + let command = CommandKeybind::CompositeKeybind(subkeymap); + keymaps.insert(Keycode::LOWER_M as i32, command); + } + JoshutoKeymap { keymaps, } diff --git a/src/joshuto/window.rs b/src/joshuto/window.rs index ce9b5af..74d1e95 100644 --- a/src/joshuto/window.rs +++ b/src/joshuto/window.rs @@ -43,7 +43,7 @@ impl JoshutoPanel { self.create(rows, cols, coords); } - pub fn destroy(&mut self) + pub fn destroy(&self) { ncurses::del_panel(self.panel); ncurses::delwin(self.win); |