summaryrefslogtreecommitdiffstats
path: root/src/commands/tab_ops.rs
blob: e519267ec1846d8406a9c03fd8ab99a7c95bf48e (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::collections::HashMap;
use std::path::Path;
use std::{io, path};

use uuid::Uuid;

use crate::config::clean::app::display::new_tab::NewTabMode;
use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
use crate::history::{
    create_dirlist_with_history, generate_entries_to_root, DirectoryHistory, JoshutoHistory,
};
use crate::tab::{JoshutoTab, TabHomePage};
use crate::util::{cwd, unix};

use crate::HOME_DIR;

use super::quit::{quit_with_action, QuitAction};

fn _tab_switch(new_index: usize, context: &mut AppContext) -> std::io::Result<()> {
    context.tab_context_mut().index = new_index;
    let cwd = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
    cwd::set_current_dir(cwd.as_path())?;

    let entry_path = match context
        .tab_context_ref()
        .curr_tab_ref()
        .curr_list_ref()
        .and_then(|l| l.curr_entry_ref())
    {
        Some(entry) => {
            let file_path = entry.file_path();
            if file_path.is_dir() {
                Some(file_path.to_path_buf())
            } else {
                None
            }
        }
        None => None,
    };

    let display_options = context.config_ref().display_options_ref();
    let tab_options = context.tab_context_ref().curr_tab_ref().option_ref();

    let history = context.tab_context_ref().curr_tab_ref().history_ref();

    let mut dirlists = Vec::with_capacity(3);
    for curr_path in [
        Some(cwd.as_path().to_path_buf()),
        cwd.parent().map(|p| p.to_path_buf()),
        entry_path,
    ]
    .into_iter()
    .flatten()
    {
        match history.get(&curr_path) {
            Some(list) => {
                if list.need_update() {
                    let dirlist = create_dirlist_with_history(
                        history,
                        cwd.as_path(),
                        display_options,
                        tab_options,
                    )?;
                    dirlists.push(dirlist);
                }
            }
            None => {
                let dirlist = create_dirlist_with_history(
                    history,
                    cwd.as_path(),
                    display_options,
                    tab_options,
                )?;
                dirlists.push(dirlist);
            }
        }
    }

    let history = context.tab_context_mut().curr_tab_mut().history_mut();
    history.insert_entries(dirlists);

    Ok(())
}

pub fn tab_switch(context: &mut AppContext, offset: i32) -> std::io::Result<()> {
    let index = context.tab_context_ref().index;
    let num_tabs = context.tab_context_ref().len();
    let new_index = (index as i32 + num_tabs as i32 + offset) as usize % num_tabs;

    _tab_switch(new_index, context)
}

pub fn tab_switch_index(context: &mut AppContext, new_index: usize) -> AppResult {
    let num_tabs = context.tab_context_ref().len();
    if new_index <= num_tabs {
        _tab_switch(new_index - 1, context)?;
    } else if new_index > num_tabs {
        for _ in 0..(new_index - num_tabs) {
            new_tab(context, &NewTabMode::Default)?;
        }
        _tab_switch(new_index - 1, context)?;
    }
    Ok(())
}

pub fn new_tab_home_path(context: &AppContext) -> path::PathBuf {
    match context.config_ref().tab_options_ref().home_page() {
        TabHomePage::Home => match HOME_DIR.as_ref() {
            Some(s) => s.clone(),
            None => path::PathBuf::from("/"),
        },
        TabHomePage::Inherit => context.tab_context_ref().curr_tab_ref().cwd().to_path_buf(),
        TabHomePage::Root => path::PathBuf::from("/"),
    }
}

pub fn new_tab(context: &mut AppContext, mode: &NewTabMode) -> AppResult {
    let new_tab_path = match mode {
        NewTabMode::Default => Ok(new_tab_home_path(context)),
        NewTabMode::CurrentTabDir => {
            Ok(context.tab_context_ref().curr_tab_ref().cwd().to_path_buf())
        }
        NewTabMode::CursorDir => context
            .tab_context_ref()
            .curr_tab_ref()
            .curr_list_ref()
            .and_then(|list| {
                list.curr_entry_ref().and_then(|entry| {
                    if entry.metadata.is_dir() {
                        Some(entry.file_path_buf())
                    } else {
                        None
                    }
                })
            })
            .ok_or(AppError::new(
                AppErrorKind::InvalidParameters,
                "No directory at cursor.".to_string(),
            )),
        NewTabMode::Directory(directory) => {
            let directory_path = unix::expand_shell_string(directory);
            Ok(if directory_path.is_absolute() {
                directory_path
            } else {
                let mut tab_dir = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
                tab_dir.push(directory_path);
                tab_dir
            })
        }
    }?;
    if new_tab_path.exists() && new_tab_path.is_dir() {
        let id = Uuid::new_v4();
        let mut new_tab_history = JoshutoHistory::new();
        let tab_display_options = context
            .config_ref()
            .display_options_ref()
            .default_tab_display_option
            .clone();
        let dirlists = generate_entries_to_root(
            new_tab_path.as_path(),
            &new_tab_history,
            context.ui_context_ref(),
            context.config_ref().display_options_ref(),
            &tab_display_options,
        )?;
        new_tab_history.insert_entries(dirlists);

        let tab_display_options = context
            .config_ref()
            .display_options_ref()
            .default_tab_display_option
            .clone();
        let tab = JoshutoTab::new(new_tab_path, new_tab_history, tab_display_options)?;
        context.tab_context_mut().insert_tab(id, tab);
        let new_index = context.tab_context_ref().len() - 1;
        context.tab_context_mut().index = new_index;
        _tab_switch(new_index, context)?;
        Ok(())
    } else {
        AppResult::Err(AppError::new(
            AppErrorKind::InvalidParameters,
            "Directory does not exist.".to_string(),
        ))
    }
}

pub fn close_tab(context: &mut AppContext) -> AppResult {
    if context.tab_context_ref().len() <= 1 {
        let action = if context.args.change_directory {
            QuitAction::OutputCurrentDirectory
        } else {
            QuitAction::Noop
        };
        return quit_with_action(context, action);
    }
    let curr_tab_id = context.tab_context_ref().curr_tab_id();
    let mut tab_index = context.tab_context_ref().index;

    let _ = context.tab_context_mut().remove_tab(&curr_tab_id);
    let num_tabs = context.tab_context_ref().len();
    if tab_index >= num_tabs {
        tab_index = num_tabs - 1;
    }
    _tab_switch(tab_index, context)?;
    Ok(())
}

pub fn reload_all_tabs(context: &mut AppContext, curr_path: &Path) -> io::Result<()> {
    let mut map = HashMap::new();
    {
        let display_options = context.config_ref().display_options_ref();

        for (id, tab) in context.tab_context_ref().iter() {
            let tab_options = tab.option_ref();
            let history = tab.history_ref();
            let dirlist =
                create_dirlist_with_history(history, curr_path, display_options, tab_options)?;
            map.insert(*id, dirlist);
        }
    }

    for (id, dirlist) in map {
        if let Some(tab) = context.tab_context_mut().tab_mut(&id) {
            tab.history_mut().insert(curr_path.to_path_buf(), dirlist);
        }
    }
    Ok(())
}

pub fn remove_entry_from_all_tabs(context: &mut AppContext, curr_path: &Path) {
    for (_, tab) in context.tab_context_mut().iter_mut() {
        tab.history_mut().remove(curr_path);
    }
}