diff options
author | Caleb Bassi <calebjbassi@gmail.com> | 2019-02-15 04:43:09 -0800 |
---|---|---|
committer | Caleb Bassi <calebjbassi@gmail.com> | 2019-02-15 05:56:09 -0800 |
commit | 29b3922f9efb9f277641a63f8f1719a15cbb8d16 (patch) | |
tree | d919ba11fa5c754565c64c43cb1614f7ae4d2546 /src/commands/change_directory.rs | |
parent | 3853eef2d052460982903038daac7abd2b71d12e (diff) |
refactor: project layout
Diffstat (limited to 'src/commands/change_directory.rs')
-rw-r--r-- | src/commands/change_directory.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs new file mode 100644 index 0000000..40ea6d8 --- /dev/null +++ b/src/commands/change_directory.rs @@ -0,0 +1,97 @@ +extern crate ncurses; + +use std::path; +use std::process; + +use commands::{JoshutoCommand, JoshutoRunnable}; +use context::JoshutoContext; +use preview; +use ui; + +#[derive(Clone, Debug)] +pub struct ChangeDirectory { + path: path::PathBuf, +} + +impl ChangeDirectory { + pub fn new(path: path::PathBuf) -> Self { + ChangeDirectory { path } + } + pub const fn command() -> &'static str { + "cd" + } + + pub fn change_directory(path: &path::PathBuf, context: &mut JoshutoContext) { + if !path.exists() { + ui::wprint_err(&context.views.bot_win, "Error: No such file or directory"); + ncurses::doupdate(); + return; + } + let curr_tab = &mut context.tabs[context.curr_tab_index]; + + let parent_list = curr_tab.parent_list.take(); + curr_tab.history.put_back(parent_list); + let curr_list = curr_tab.curr_list.take(); + curr_tab.history.put_back(curr_list); + + match std::env::set_current_dir(path.as_path()) { + Ok(_) => { + curr_tab.curr_path = path.clone(); + } + Err(e) => { + ui::wprint_err(&context.views.bot_win, e.to_string().as_str()); + return; + } + } + curr_tab + .history + .populate_to_root(&curr_tab.curr_path, &context.config_t.sort_type); + + curr_tab.curr_list = match curr_tab + .history + .pop_or_create(&curr_tab.curr_path, &context.config_t.sort_type) + { + Ok(s) => Some(s), + Err(e) => { + eprintln!("{}", e); + process::exit(1); + } + }; + + if let Some(parent) = curr_tab.curr_path.parent() { + curr_tab.parent_list = match curr_tab + .history + .pop_or_create(&parent, &context.config_t.sort_type) + { + Ok(s) => Some(s), + Err(e) => { + eprintln!("{}", e); + process::exit(1); + } + }; + } + + curr_tab.refresh( + &context.views, + &context.config_t, + &context.username, + &context.hostname, + ); + } +} + +impl JoshutoCommand for ChangeDirectory {} + +impl std::fmt::Display for ChangeDirectory { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{} {}", Self::command(), self.path.to_str().unwrap()) + } +} + +impl JoshutoRunnable for ChangeDirectory { + fn execute(&self, context: &mut JoshutoContext) { + Self::change_directory(&self.path, context); + preview::preview_file(context); + ncurses::doupdate(); + } +} |