summaryrefslogtreecommitdiffstats
path: root/src/joshuto/command/change_directory.rs
blob: 14bd19295e99571b14f40892c292458fca1eb815 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
extern crate fs_extra;
extern crate ncurses;

use std;
use std::fmt;
use std::path;
use std::process;

use joshuto;
use joshuto::command;
use joshuto::ui;

#[derive(Clone, Debug)]
pub struct ChangeDirectory {
    path: path::PathBuf,
}

impl ChangeDirectory {
    pub fn new(path: path::PathBuf) -> Self
    {
        ChangeDirectory {
            path,
        }
    }
    pub const fn command() -> &'static str { "cd" }
}

impl command::JoshutoCommand for ChangeDirectory {}

impl std::fmt::Display for ChangeDirectory {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "{} {}", Self::command(), self.path.to_str().unwrap())
    }
}

impl command::Runnable for ChangeDirectory {
    fn execute(&self, context: &mut joshuto::JoshutoContext)
    {
        if !self.path.exists() {
            ui::wprint_err(&context.views.bot_win, "Error: No such file or directory");
            ncurses::doupdate();
            return;
        }
        let curr_tab = &mut context.tabs[context.tab_index];

        match std::env::set_current_dir(self.path.as_path()) {
            Ok(_) => {
                curr_tab.curr_path = self.path.clone();
            },
            Err(e) => {
                ui::wprint_err(&context.views.bot_win, e.to_string().as_str());
                return;
            }
        }

        {
            curr_tab.history.populate_to_root(&curr_tab.curr_path, &context.config_t.sort_type);

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

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

            let preview_list = curr_tab.preview_list.take();
            curr_tab.history.put_back(preview_list);
        }

        curr_tab.curr_list = match curr_tab.history.pop_or_create(&curr_tab.curr_path,
                    &context.config_t.sort_type) {
            Ok(s) => {
                if let Some(dirent) = s.get_curr_entry() {
                    if dirent.path.is_dir() {
                        curr_tab.preview_list = match curr_tab.history.pop_or_create(
                                    &dirent.path, &context.config_t.sort_type) {
                            Ok(s) => {
                                Some(s)
                            },
                            Err(e) => {
                                eprintln!("{}", e);
                                None
                            },
                        };
                    }
                }
                Some(s)
            },
            Err(e) => {
                eprintln!("{}", e);
                process::exit(1);
            },
        };

        if let Some(parent) = curr_tab.curr_path.parent() {
            curr_tab.parent_list = match curr_tab.history.pop_or_create(&parent, &context.config_t.sort_type) {
                Ok(s) => { Some(s) },
                Err(e) => {
                    eprintln!("{}", e);
                    process::exit(1);
                },
            };
        }

        ui::redraw_view(&context.theme_t, &context.views.left_win,
                curr_tab.parent_list.as_ref());
        ui::redraw_view_detailed(&context.theme_t, &context.views.mid_win,
                curr_tab.curr_list.as_ref());
        ui::redraw_view(&context.theme_t, &context.views.right_win,
                curr_tab.preview_list.as_ref());

        ui::redraw_status(&context.theme_t, &context.views,
                curr_tab.curr_list.as_ref(),
                &curr_tab.curr_path,
                &context.username, &context.hostname);

        ncurses::doupdate();
    }
}