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
|
use std::path;
use crate::context::AppContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::history::DirectoryHistory;
use crate::tab::{JoshutoTab, TabHomePage};
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();
std::env::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 options = context.config_ref().display_options_ref().clone();
let tab_options = context
.tab_context_ref()
.curr_tab_ref()
.option_ref()
.clone();
let history = context.tab_context_mut().curr_tab_mut().history_mut();
if history
.create_or_soft_update(cwd.as_path(), &options, &tab_options)
.is_err()
{
history.remove(cwd.as_path());
}
if let Some(cwd_parent) = cwd.parent() {
if history
.create_or_soft_update(cwd_parent, &options, &tab_options)
.is_err()
{
history.remove(cwd_parent);
}
}
if let Some(file_path) = entry_path {
if history
.create_or_soft_update(file_path.as_path(), &options, &tab_options)
.is_err()
{
history.remove(file_path.as_path());
}
}
Ok(())
}
pub fn tab_switch(offset: i32, context: &mut AppContext) -> 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(new_index: usize, context: &mut AppContext) -> JoshutoResult {
if new_index <= context.tab_context_ref().len() {
_tab_switch(new_index - 1, context)?;
Ok(())
} else if new_index == context.tab_context_ref().len() + 1 {
new_tab(context)?;
_tab_switch(new_index - 1, context)?;
Ok(())
} else {
Err(JoshutoError::new(
JoshutoErrorKind::InvalidParameters,
format!("No tab with index {}", new_index),
))
}
}
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) -> JoshutoResult {
let new_tab_path = new_tab_home_path(context);
let tab = JoshutoTab::new(
new_tab_path,
context.ui_context_ref(),
context.config_ref().display_options_ref(),
)?;
context.tab_context_mut().push_tab(tab);
let new_index = context.tab_context_ref().len() - 1;
context.tab_context_mut().index = new_index;
_tab_switch(new_index, context)?;
Ok(())
}
pub fn close_tab(context: &mut AppContext) -> JoshutoResult {
if context.tab_context_ref().len() <= 1 {
return quit_with_action(context, QuitAction::Noop);
}
let mut tab_index = context.tab_context_ref().index;
let _ = context.tab_context_mut().pop_tab(tab_index);
let num_tabs = context.tab_context_ref().len();
if tab_index >= num_tabs {
tab_index = num_tabs - 1;
}
_tab_switch(tab_index, context)?;
Ok(())
}
|