diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-14 17:57:50 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-14 17:57:50 -0400 |
commit | 71e0c7faf3cc283b2b24e70631e5b18a7a7525cd (patch) | |
tree | b27794c91a930ae2a1bc5cdc6a468b9b4d553134 /src/commands/new_directory.rs | |
parent | 5a820a7a275bf5cacd2c545c0a0ac533789ec349 (diff) |
rework error handling system
rather than letting each command separately handle errors, we return a
Result<(), JoshutoError> instead and allow for run.rs to handle all errors
Diffstat (limited to 'src/commands/new_directory.rs')
-rw-r--r-- | src/commands/new_directory.rs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs index 243cf6a..b3d1c2d 100644 --- a/src/commands/new_directory.rs +++ b/src/commands/new_directory.rs @@ -2,6 +2,7 @@ use std::path; use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList}; use crate::context::JoshutoContext; +use crate::error::JoshutoError; use crate::textfield::JoshutoTextField; use crate::ui; use crate::window::JoshutoView; @@ -27,35 +28,33 @@ impl std::fmt::Display for NewDirectory { } impl JoshutoRunnable for NewDirectory { - fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) { + fn execute( + &self, + context: &mut JoshutoContext, + view: &JoshutoView, + ) -> Result<(), JoshutoError> { let (term_rows, term_cols) = ui::getmaxyx(); const PROMPT: &str = ":mkdir "; - let user_input: Option<String>; - - { + let user_input: Option<String> = { let textfield = JoshutoTextField::new( 1, term_cols, (term_rows as usize - 1, 0), PROMPT.to_string(), ); - user_input = textfield.readline_with_initial("", ""); - } + textfield.readline_with_initial("", "") + }; if let Some(user_input) = user_input { let path = path::PathBuf::from(user_input); match std::fs::create_dir_all(&path) { - Ok(_) => { - ReloadDirList::reload(context, view); - } - Err(e) => { - ui::wprint_err(&view.bot_win, e.to_string().as_str()); - } + Ok(_) => ReloadDirList::reload(context, view), + Err(e) => return Err(JoshutoError::IO(e)), } } - ncurses::doupdate(); + Ok(()) } } |