blob: 23ebfc6592cecda5df1ac4a2016562e67e62ccf5 (
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
|
use std::path;
use crate::commands::reload;
use crate::context::AppContext;
use crate::error::JoshutoResult;
use crate::history::DirectoryHistory;
// ChangeDirectory command
pub fn cd(path: &path::Path, context: &mut AppContext) -> std::io::Result<()> {
std::env::set_current_dir(path)?;
context.tab_context_mut().curr_tab_mut().set_cwd(path);
Ok(())
}
pub fn change_directory(context: &mut AppContext, mut path: &path::Path) -> JoshutoResult {
let new_cwd = if path.is_absolute() {
path.to_path_buf()
} else {
while let Ok(p) = path.strip_prefix("../") {
parent_directory(context)?;
path = p;
}
let mut new_cwd = std::env::current_dir()?;
new_cwd.push(path);
new_cwd
};
cd(new_cwd.as_path(), context)?;
let options = context.config_ref().display_options_ref().clone();
let ui_context = context.ui_context_ref().clone();
let tab_options = context
.tab_context_ref()
.curr_tab_ref()
.option_ref()
.clone();
context
.tab_context_mut()
.curr_tab_mut()
.history_mut()
.populate_to_root(new_cwd.as_path(), &ui_context, &options, &tab_options)?;
Ok(())
}
// ParentDirectory command
pub fn parent_directory(context: &mut AppContext) -> JoshutoResult {
if let Some(parent) = context
.tab_context_ref()
.curr_tab_ref()
.cwd()
.parent()
.map(|p| p.to_path_buf())
{
std::env::set_current_dir(&parent)?;
context
.tab_context_mut()
.curr_tab_mut()
.set_cwd(parent.as_path());
reload::soft_reload_curr_tab(context)?;
}
Ok(())
}
// PreviousDirectory command
pub fn previous_directory(context: &mut AppContext) -> JoshutoResult {
if let Some(path) = context.tab_context_ref().curr_tab_ref().previous_dir() {
let path = path.to_path_buf();
std::env::set_current_dir(&path)?;
context
.tab_context_mut()
.curr_tab_mut()
.set_cwd(path.as_path());
reload::soft_reload_curr_tab(context)?;
}
Ok(())
}
|