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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
use crate::interactive::{
sorted_entries,
widgets::{MainWindow, MainWindowProps},
ByteVisualization, CursorDirection, DisplayOptions, EntryDataBundle, SortMode,
};
use dua::{
traverse::{Traversal, TreeIndex},
WalkOptions, WalkResult,
};
use failure::Error;
use std::{collections::BTreeMap, io, path::PathBuf};
use termion::{event::Key, input::TermRead};
use tui::backend::Backend;
use tui_react::Terminal;
#[derive(Copy, Clone)]
pub enum FocussedPane {
Main,
Help,
Mark,
}
impl Default for FocussedPane {
fn default() -> Self {
FocussedPane::Main
}
}
#[derive(Default)]
pub struct AppState {
pub root: TreeIndex,
pub selected: Option<TreeIndex>,
pub entries: Vec<EntryDataBundle>,
pub sorting: SortMode,
pub message: Option<String>,
pub focussed: FocussedPane,
pub bookmarks: BTreeMap<TreeIndex, TreeIndex>,
pub is_scanning: bool,
}
pub enum ProcessingResult {
Finished(WalkResult),
ExitRequested(WalkResult),
}
impl AppState {
pub fn draw<B>(
&mut self,
window: &mut MainWindow,
traversal: &Traversal,
display: DisplayOptions,
terminal: &mut Terminal<B>,
) -> Result<(), Error>
where
B: Backend,
{
let props = MainWindowProps {
traversal: &traversal,
display,
state: &self,
};
draw_window(window, props, terminal)
}
pub fn process_events<B>(
&mut self,
window: &mut MainWindow,
traversal: &mut Traversal,
display: &mut DisplayOptions,
terminal: &mut Terminal<B>,
keys: impl Iterator<Item = Result<Key, io::Error>>,
) -> Result<ProcessingResult, Error>
where
B: Backend,
{
use termion::event::Key::*;
use FocussedPane::*;
self.reset_message();
self.draw(window, traversal, display.clone(), terminal)?;
for key in keys.filter_map(Result::ok) {
self.reset_message();
match key {
Char('?') => self.toggle_help_pane(window),
Char('\t') => {
self.cycle_focus(window);
}
Ctrl('c') => {
return Ok(ProcessingResult::ExitRequested(WalkResult {
num_errors: traversal.io_errors,
}))
}
Char('q') | Esc => match self.focussed {
Main => {
return Ok(ProcessingResult::ExitRequested(WalkResult {
num_errors: traversal.io_errors,
}))
}
Mark => self.focussed = Main,
Help => {
self.focussed = Main;
window.help_pane = None
}
},
_ => {}
}
match self.focussed {
FocussedPane::Mark => {
self.dispatch_to_mark_pane(key, window, traversal, display.clone(), terminal)
}
FocussedPane::Help => {
window.help_pane.as_mut().expect("help pane").key(key);
}
FocussedPane::Main => match key {
Char('O') => self.open_that(traversal),
Char(' ') => self.mark_entry(false, window, traversal),
Char('d') => self.mark_entry(true, window, traversal),
Char('u') | Char('h') | Backspace | Left => {
self.exit_node_with_traversal(traversal)
}
Char('o') | Char('l') | Char('\n') | Right => {
self.enter_node_with_traversal(traversal)
}
Ctrl('u') | PageUp => self.change_entry_selection(CursorDirection::PageUp),
Char('k') | Up => self.change_entry_selection(CursorDirection::Up),
Char('j') | Down => self.change_entry_selection(CursorDirection::Down),
Ctrl('d') | PageDown => self.change_entry_selection(CursorDirection::PageDown),
Char('s') => self.cycle_sorting(traversal),
Char('g') => display.byte_vis.cycle(),
_ => {}
},
};
self.draw(window, traversal, display.clone(), terminal)?;
}
Ok(ProcessingResult::Finished(WalkResult {
num_errors: traversal.io_errors,
}))
}
}
pub fn draw_window<B>(
window: &mut MainWindow,
props: MainWindowProps,
terminal: &mut Terminal<B>,
) -> Result<(), Error>
where
B: Backend,
{
let area = terminal.pre_render()?;
window.render(props, area, terminal.current_buffer_mut());
terminal.post_render()?;
Ok(())
}
/// State and methods representing the interactive disk usage analyser for the terminal
pub struct TerminalApp {
pub traversal: Traversal,
pub display: DisplayOptions,
pub state: AppState,
pub window: MainWindow,
}
impl TerminalApp {
pub fn process_events<B>(
&mut self,
terminal: &mut Terminal<B>,
keys: impl Iterator<Item = Result<Key, io::Error>>,
) -> Result<WalkResult, Error>
where
B: Backend,
{
match self.state.process_events(
&mut self.window,
&mut self.traversal,
&mut self.display,
terminal,
keys,
)? {
ProcessingResult::Finished(res) | ProcessingResult::ExitRequested(res) => Ok(res),
}
}
pub fn initialize<B>(
terminal: &mut Terminal<B>,
options: WalkOptions,
input: Vec<PathBuf>,
mode: Interaction,
) -> Result<Option<TerminalApp>, Error>
where
B: Backend,
{
terminal.hide_cursor()?;
terminal.clear()?;
let mut display: DisplayOptions = options.clone().into();
display.byte_vis = ByteVisualization::PercentageAndBar;
let mut window = MainWindow::default();
let (keys_tx, keys_rx) = std::sync::mpsc::channel(); // unbounded
match mode {
Interaction::None => drop(keys_tx),
Interaction::Full => drop(std::thread::spawn(move || {
let keys = std::io::stdin().keys();
for key in keys {
if let Err(_) = keys_tx.send(key) {
break;
}
}
})),
}
let fetch_buffered_key_events = move || {
let mut keys = Vec::new();
while let Ok(key) = keys_rx.try_recv() {
keys.push(key);
}
keys
};
let mut state = None;
let traversal = Traversal::from_walk(options, input, |traversal| {
let s = match state.as_mut() {
Some(s) => s,
None => {
state = Some({
let sorting = Default::default();
let entries =
sorted_entries(&traversal.tree, traversal.root_index, sorting);
AppState {
root: traversal.root_index,
sorting,
selected: entries.get(0).map(|b| b.index),
entries,
is_scanning: true,
..Default::default()
}
});
state.as_mut().expect("state to be present, we just set it")
}
};
let should_exit =
|