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
|
use std::process;
use std::time;
use crate::commands::{CommandKeybind, FileOperationThread, JoshutoCommand, ReloadDirList};
use crate::config::{self, JoshutoCommandMapping, JoshutoConfig};
use crate::context::JoshutoContext;
use crate::error::JoshutoError;
use crate::tab::JoshutoTab;
use crate::ui;
use crate::window::JoshutoPanel;
use crate::window::JoshutoView;
fn recurse_get_keycommand(keymap: &JoshutoCommandMapping) -> Option<&Box<JoshutoCommand>> {
let (term_rows, term_cols) = ui::getmaxyx();
ncurses::timeout(-1);
let ch: i32 = {
let keymap_len = keymap.len();
let win = JoshutoPanel::new(
keymap_len as i32 + 1,
term_cols,
((term_rows - keymap_len as i32 - 2) as usize, 0),
);
let mut display_vec: Vec<String> = keymap
.iter()
.map(|(k, v)| format!(" {}\t{}", *k as u8 as char, v))
.collect();
display_vec.sort();
win.move_to_top();
ui::display_menu(&win, &display_vec);
ncurses::doupdate();
ncurses::wgetch(win.win)
};
ncurses::doupdate();
if ch == config::keymap::ESCAPE {
None
} else {
match keymap.get(&ch) {
Some(CommandKeybind::CompositeKeybind(m)) => recurse_get_keycommand(&m),
Some(CommandKeybind::SimpleKeybind(s)) => Some(s),
_ => None,
}
}
}
fn reload_tab(
index: usize,
context: &mut JoshutoContext,
view: &JoshutoView,
) -> Result<(), std::io::Error> {
ReloadDirList::reload(index, context)?;
if index == context.curr_tab_index {
let dirty_tab = &mut context.tabs[index];
dirty_tab.refresh(view, &context.config_t);
}
Ok(())
}
fn join_thread(
context: &mut JoshutoContext,
thread: FileOperationThread,
view: &JoshutoView,
) -> Result<(), std::io::Error> {
ncurses::werase(view.bot_win.win);
ncurses::doupdate();
let (tab_src, tab_dest) = (thread.tab_src, thread.tab_dest);
match thread.handle.join() {
Err(e) => {
ui::wprint_err(&view.bot_win, format!("{:?}", e).as_str());
view.bot_win.queue_for_refresh();
}
Ok(_) => {
if tab_src < context.tabs.len() {
reload_tab(tab_src, context, view)?;
}
if tab_dest != tab_src && tab_dest < context.tabs.len() {
reload_tab(tab_dest, context, view)?;
}
}
}
Ok(())
}
fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) -> Result<(), std::io::Error> {
let thread_wait_duration: time::Duration = time::Duration::from_millis(100);
for i in 0..context.threads.len() {
match &context.threads[i].recv_timeout(&thread_wait_duration) {
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
let thread = context.threads.swap_remove(i);
join_thread(context, thread, view)?;
ncurses::doupdate();
break;
}
Ok(progress_info) => {
ui::draw_progress_bar(
&view.bot_win,
progress_info.bytes_finished as f32 / progress_info.total_bytes as f32,
);
ncurses::doupdate();
}
_ => {}
}
}
Ok(())
}
#[inline]
fn resize_handler(context: &mut JoshutoContext, view: &JoshutoView) {
ui::redraw_tab_view(&view.tab_win, &context);
let curr_tab = &mut context.tabs[context.curr_tab_index];
curr_tab.refresh(view, &context.config_t);
ncurses::doupdate();
}
fn init_context(context: &mut JoshutoContext, view: &JoshutoView) {
match std::env::current_dir() {
Ok(curr_path) => match JoshutoTab::new(curr_path, &context.config_t.sort_option) {
Ok(tab) => {
context.tabs.push(tab);
context.curr_tab_index = context.tabs.len() - 1;
ui::redraw_tab_view(&view.tab_win, &context);
let curr_tab = &mut context.tabs[context.curr_tab_index];
curr_tab.refresh(view, &context.config_t);
ncurses::doupdate();
}
Err(e) => {
ui::end_ncurses();
eprintln!("{}", e);
process::exit(1);
}
},
Err(e) => {
ui::end_ncurses();
eprintln!("{}", e);
process::exit(1);
}
}
}
pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) {
ui::init_ncurses();
let mut context = JoshutoContext::new(config_t);
let mut view = JoshutoView::new(context.config_t.column_ratio);
init_context(&mut context, &view);
while !context.exit {
if !context.threads.is_empty() {
ncurses::timeout(0);
match process_threads(&mut context, &view) {
Ok(_) => {}
Err(e) => ui::wprint_err(&view.bot_win, e.to_string().as_str()),
}
ncurses::doupdate();
} else {
ncurses::timeout(-1);
}
if let Some(ch) = ncurses::get_wch() {
let ch = match ch {
ncurses::WchResult::Char(s) => s as i32,
ncurses::WchResult::KeyCode(s) => s,
};
if ch == ncurses::KEY_RESIZE {
view.resize_views();
resize_handler(&mut context, &view);
continue;
}
let keycommand;
match keymap_t.get(&ch) {
Some(CommandKeybind::CompositeKeybind(m)) => match recurse_get_keycommand(&m) {
Some(s) => keycommand = s,
None => continue,
},
Some(CommandKeybind::SimpleKeybind(s)) => {
keycommand = s;
}
None => {
ui::wprint_err(&view.bot_win, &format!("Unknown keycode: {}", ch));
ncurses::doupdate();
continue;
}
}
match keycommand.execute(&mut context, &view) {
Ok(()) => {}
Err(JoshutoError::IO(e)) => {
ui::wprint_err(&view.bot_win, e.to_string().as_str());
ncurses::doupdate();
}
Err(JoshutoError::Keymap(e)) => {
ui::wprint_err(&view.bot_win, e.to_string().as_str());
ncurses::doupdate();
}
}
}
}
ui::end_ncurses();
}
|