summaryrefslogtreecommitdiffstats
path: root/src/commands/parent_directory.rs
blob: 15d35560c5461c2c05de6e033c005718fec904ec (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::commands::{JoshutoCommand, JoshutoRunnable};
use crate::context::JoshutoContext;
use crate::preview;
use crate::ui;

#[derive(Clone, Debug)]
pub struct ParentDirectory;

impl ParentDirectory {
    pub fn new() -> Self {
        ParentDirectory
    }
    pub const fn command() -> &'static str {
        "parent_directory"
    }

    pub fn parent_directory(context: &mut JoshutoContext) {
        if !context.curr_tab_mut().curr_path.pop() {
            return;
        }

        match std::env::set_current_dir(&context.curr_tab_ref().curr_path) {
            Ok(_) => {
                {
                    let curr_tab = &mut context.tabs[context.curr_tab_index];

                    let curr_list = curr_tab.curr_list.take();
                    curr_tab.history.put_back(curr_list);
                    let parent_list = curr_tab.parent_list.take();
                    curr_tab.curr_list = parent_list;

                    match curr_tab.curr_path.parent() {
                        Some(parent) => {
                            curr_tab.parent_list = match curr_tab
                                .history
                                .pop_or_create(&parent, &context.config_t.sort_type)
                            {
                                Ok(s) => Some(s),
                                Err(e) => {
                                    ui::wprint_err(&context.views.left_win, e.to_string().as_str());
                                    None
                                }
                            };
                        }
                        None => {
                            ncurses::werase(context.views.left_win.win);
                            ncurses::wnoutrefresh(context.views.left_win.win);
                        }
                    }
                    curr_tab.refresh(
                        &context.views,
                        &context.config_t,
                        &context.username,
                        &context.hostname,
                    );
                }
                preview::preview_file(context);
            }
            Err(e) => {
                ui::wprint_err(&context.views.bot_win, e.to_string().as_str());
            }
        };
        ncurses::doupdate();
    }
}

impl JoshutoCommand for ParentDirectory {}

impl std::fmt::Display for ParentDirectory {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(Self::command())
    }
}

impl JoshutoRunnable for ParentDirectory {
    fn execute(&self, context: &mut JoshutoContext) {
        Self::parent_directory(context);
    }
}