diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-19 21:21:33 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-19 21:21:33 -0400 |
commit | 7b4c611ed4d804bd52aeda0a619a54bea9b1e13d (patch) | |
tree | 7ba31fff7d5de171aadfc32c67f81dd64428b3ba /src/commands/change_directory.rs | |
parent | 7741d4d2c8798ad0bb609e97fb3bda86c5318a36 (diff) |
Change command to use an enum instead of polymorphism
Diffstat (limited to 'src/commands/change_directory.rs')
-rw-r--r-- | src/commands/change_directory.rs | 65 |
1 files changed, 17 insertions, 48 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index 7559c52..1094111 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -1,61 +1,30 @@ use std::path; -use crate::commands::{JoshutoCommand, JoshutoRunnable}; use crate::context::JoshutoContext; use crate::error::JoshutoResult; use crate::history::DirectoryHistory; -use crate::ui::TuiBackend; use crate::util::load_child::LoadChild; -#[derive(Clone, Debug)] -pub struct ChangeDirectory { - path: path::PathBuf, +pub fn cd(path: &path::Path, context: &mut JoshutoContext) -> std::io::Result<()> { + std::env::set_current_dir(path)?; + context.tab_context_mut().curr_tab_mut().set_pwd(path); + Ok(()) } -impl ChangeDirectory { - pub fn new(path: path::PathBuf) -> Self { - ChangeDirectory { path } - } - pub const fn command() -> &'static str { - "cd" - } +pub fn change_directories(path: &path::Path, context: &mut JoshutoContext) -> std::io::Result<()> { + cd(path, context)?; + let sort_options = context.config_t.sort_option.clone(); + context + .tab_context_mut() + .curr_tab_mut() + .history_mut() + .populate_to_root(&path, &sort_options)?; - pub fn cd(path: &path::Path, context: &mut JoshutoContext) -> std::io::Result<()> { - std::env::set_current_dir(path)?; - context.tab_context_mut().curr_tab_mut().set_pwd(path); - Ok(()) - } - - pub fn change_directories( - path: &path::Path, - context: &mut JoshutoContext, - ) -> std::io::Result<()> { - Self::cd(path, context)?; - - let sort_options = context.config_t.sort_option.clone(); - context - .tab_context_mut() - .curr_tab_mut() - .history_mut() - .populate_to_root(&path, &sort_options)?; - - Ok(()) - } + Ok(()) } -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, _: &mut TuiBackend) -> JoshutoResult<()> { - Self::change_directories(&self.path, context)?; - LoadChild::load_child(context)?; - - Ok(()) - } +pub fn change_directory(context: &mut JoshutoContext, path: &path::Path) -> JoshutoResult<()> { + change_directories(path, context)?; + LoadChild::load_child(context)?; + Ok(()) } |