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/new_directory.rs | |
parent | 3853eef2d052460982903038daac7abd2b71d12e (diff) |
refactor: project layout
Diffstat (limited to 'src/commands/new_directory.rs')
-rw-r--r-- | src/commands/new_directory.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs new file mode 100644 index 0000000..35a3b18 --- /dev/null +++ b/src/commands/new_directory.rs @@ -0,0 +1,62 @@ +extern crate ncurses; + +use std::path; + +use commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList}; +use context::JoshutoContext; +use textfield::JoshutoTextField; +use ui; + +#[derive(Clone, Debug)] +pub struct NewDirectory; + +impl NewDirectory { + pub fn new() -> Self { + NewDirectory + } + pub const fn command() -> &'static str { + "mkdir" + } +} + +impl JoshutoCommand for NewDirectory {} + +impl std::fmt::Display for NewDirectory { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(Self::command()) + } +} + +impl JoshutoRunnable for NewDirectory { + fn execute(&self, context: &mut JoshutoContext) { + let (term_rows, term_cols) = ui::getmaxyx(); + const PROMPT: &'static str = ":mkdir "; + + 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("", ""); + } + + 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); + } + Err(e) => { + ui::wprint_err(&context.views.bot_win, e.to_string().as_str()); + } + } + } + + ncurses::doupdate(); + } +} |