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
use std::path;
use std::rc::Rc;
use users::UsersCache;
use crate::args::Args;
use crate::completion::Completion;
use crate::content_window::ContentWindow;
use crate::fileinfo::PathContent;
use crate::filter::FilterKind;
use crate::fm_error::{FmError, FmResult};
use crate::input::Input;
use crate::mode::Mode;
use crate::preview::Preview;
use crate::selectable_content::SelectableContent;
use crate::shortcut::Shortcut;
use crate::visited::History;
#[derive(Clone)]
pub struct Tab {
pub mode: Mode,
pub window: ContentWindow,
pub input: Input,
pub path_content: PathContent,
pub height: usize,
pub show_hidden: bool,
pub nvim_server: String,
pub completion: Completion,
pub must_quit: bool,
pub preview: Preview,
pub history: History,
pub shortcut: Shortcut,
pub searched: Option<String>,
}
impl Tab {
pub fn new(args: Args, height: usize, users_cache: Rc<UsersCache>) -> FmResult<Self> {
let path = std::fs::canonicalize(path::Path::new(&args.path))?;
let path_content = PathContent::new(&path, false, users_cache)?;
let show_hidden = false;
let nvim_server = args.server;
let mode = Mode::Normal;
let window = ContentWindow::new(path_content.content.len(), height);
let input = Input::default();
let completion = Completion::default();
let must_quit = false;
let preview = Preview::Empty;
let mut history = History::default();
history.push(&path);
let shortcut = Shortcut::new();
let searched = None;
Ok(Self {
mode,
window,
input,
path_content,
height,
show_hidden,
nvim_server,
completion,
must_quit,
preview,
history,
shortcut,
searched,
})
}
pub fn fill_completion(&mut self) -> FmResult<()> {
self.completion.set_kind(&self.mode);
let current_path = self.path_str().unwrap_or_default().to_owned();
self.completion
.complete(&self.input.string(), &self.path_content, ¤t_path)
}
pub fn refresh_view(&mut self) -> FmResult<()> {
self.path_content.filter = FilterKind::All;
self.input.reset();
self.path_content.reset_files()?;
self.window.reset(self.path_content.content.len());
Ok(())
}
pub fn go_to_child(&mut self) -> FmResult<()> {
let childpath = &self
.path_content
.selected()
.ok_or_else(|| FmError::custom("go_to_child", "Empty directory"))?
.path
.clone();
self.history.push(childpath);
self.set_pathcontent(childpath)?;
self.window.reset(self.path_content.content.len());
self.input.cursor_start();
Ok(())
}
pub fn set_height(&mut self, height: usize) {
self.window.set_height(height);
self.height = height;
}
pub fn must_quit(&self) -> bool {
self.must_quit
}
pub fn path_str(&self) -> Option<&str> {
self.path_content.path.to_str()
}
pub fn set_pathcontent(&mut self, path: &path::Path) -> FmResult<()> {
self.history.push(path);
self.path_content.change_directory(path)?;
self.window.reset(self.path_content.content.len());
Ok(())
}
pub fn set_window(&mut self) {
let len = self.path_content.content.len();
self.window.reset(len);
}
pub fn scroll_to(&mut self, index: usize) {
self.window.scroll_to(index);
}
pub fn find_jump_index(&self, jump_target: &path::Path) -> Option<usize> {
self.path_content
.content
.iter()
.position(|file| file.path == jump_target)
}
pub fn refresh_shortcuts(&mut self, mount_points: &[&path::Path]) {
self.shortcut.refresh(mount_points)
}
pub fn go_to_index(&mut self, index: usize) {
self.path_content.select_index(index);
self.window.scroll_to(index);
}
pub fn refresh_users(&mut self, users_cache: Rc<UsersCache>) -> FmResult<()> {
self.path_content.refresh_users(users_cache)
}
}