summaryrefslogtreecommitdiffstats
path: root/src/commands/new_directory.rs
blob: 788d576eb4bb394b76cc2b917aff52a80bdd4ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::path;

use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
use crate::textfield::JoshutoTextField;
use crate::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: &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();
    }
}