summaryrefslogtreecommitdiffstats
path: root/src/commands/parent_directory.rs
blob: 59a61220b894f47ad0483680340eadecbd55b7fd (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
80
81
82
83
84
85
86
87
use crate::commands::{JoshutoCommand, JoshutoRunnable};
use crate::context::JoshutoContext;
use crate::error::JoshutoError;
use crate::preview;
use crate::ui;
use crate::window::JoshutoView;

#[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,
        view: &JoshutoView,
    ) -> Result<(), JoshutoError> {
        if !context.curr_tab_mut().curr_path.pop() {
            return Ok(());
        }

        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_option)
                        {
                            Ok(s) => Some(s),
                            Err(e) => {
                                ui::wprint_err(&view.left_win, e.to_string().as_str());
                                None
                            }
                        };
                    }
                    None => {
                        ncurses::werase(view.left_win.win);
                        view.left_win.queue_for_refresh();
                    }
                }
                curr_tab.refresh(
                    view,
                    &context.config_t,
                    &context.username,
                    &context.hostname,
                );
                preview::preview_file(curr_tab, view, &context.config_t);
                ncurses::doupdate();
                return Ok(());
            }
            Err(e) => {
                return Err(JoshutoError::IO(e));
            }
        };
    }
}

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,
        view: &JoshutoView,
    ) -> Result<(), JoshutoError> {
        Self::parent_directory(context, view)
    }
}