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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
use crate::interactive::{
fit_string_graphemes_with_ellipsis,
widgets::{COLOR_BYTESIZE_SELECTED, COLOR_MARKED_LIGHT},
CursorDirection,
};
use dua::{
path_of,
traverse::{Tree, TreeIndex},
ByteFormat,
};
use itertools::Itertools;
use std::{borrow::Borrow, collections::btree_map::Entry, collections::BTreeMap, path::PathBuf};
use termion::{event::Key, event::Key::*};
use tui::{
buffer::Buffer,
layout::Rect,
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
widgets::Block,
widgets::Borders,
widgets::Text,
widgets::{Paragraph, Widget},
};
use tui_react::{List, ListProps};
use unicode_segmentation::UnicodeSegmentation;
pub enum MarkMode {
Delete,
}
pub type EntryMarkMap = BTreeMap<TreeIndex, EntryMark>;
pub struct EntryMark {
pub size: u64,
pub path: PathBuf,
pub index: usize,
pub num_errors_during_deletion: usize,
}
#[derive(Default)]
pub struct MarkPane {
selected: Option<usize>,
marked: EntryMarkMap,
list: List,
has_focus: bool,
last_sorting_index: usize,
}
pub struct MarkPaneProps {
pub border_style: Style,
pub format: ByteFormat,
}
impl MarkPane {
#[cfg(test)]
pub fn has_focus(&self) -> bool {
self.has_focus
}
pub fn set_focus(&mut self, has_focus: bool) {
self.has_focus = has_focus;
if has_focus {
self.selected = Some(self.marked.len().saturating_sub(1));
} else {
self.selected = None
}
}
pub fn toggle_index(mut self, index: TreeIndex, tree: &Tree) -> Option<Self> {
match self.marked.entry(index) {
Entry::Vacant(entry) => {
if let Some(e) = tree.node_weight(index) {
let sorting_index = self.last_sorting_index + 1;
self.last_sorting_index = sorting_index;
entry.insert(EntryMark {
size: e.size,
path: path_of(tree, index),
index: sorting_index,
num_errors_during_deletion: 0,
});
}
}
Entry::Occupied(entry) => {
entry.remove();
}
};
if self.marked.is_empty() {
None
} else {
Some(self)
}
}
pub fn marked(&self) -> &EntryMarkMap {
&self.marked
}
pub fn key(mut self, key: Key) -> Option<(Self, Option<MarkMode>)> {
let action = None;
match key {
Ctrl('R') => return self.prepare_deletion(),
Char('d') | Char(' ') => return self.remove_selected().map(|s| (s, action)),
Ctrl('u') | PageUp => self.change_selection(CursorDirection::PageUp),
Char('k') | Up => self.change_selection(CursorDirection::Up),
Char('j') | Down => self.change_selection(CursorDirection::Down),
Ctrl('d') | PageDown => self.change_selection(CursorDirection::PageDown),
_ => {}
};
Some((self, action))
}
pub fn next_entry_for_deletion(&mut self) -> Option<TreeIndex> {
match self.selected.and_then(|selected| {
self.tree_index_by_list_position(selected)
.and_then(|idx| self.marked.get(&idx).map(|d| (selected, idx, d)))
}) {
Some((position, selected_index, data)) => match data.num_errors_during_deletion {
0 => Some(selected_index),
_ => {
self.selected = match position + 1 {
p if p < self.marked.len() => Some(p),
_ => None,
};
// we assume that each entry is either followed by `delete_entry()` or `set_error_on_marked_item`.
self.tree_index_by_list_position(position + 1)
}
},
None => None,
}
}
pub fn delete_entry(self) -> Option<Self> {
self.remove_selected()
}
pub fn set_error_on_marked_item(&mut self, num_errors: usize) {
if let Some(d) = self
.selected
.and_then(|s| self.tree_index_by_list_position(s))
.and_then(|p| self.marked.get_mut(&p))
{
d.num_errors_during_deletion = num_errors;
}
}
fn prepare_deletion(self) -> Option<(Self, Option<MarkMode>)> {
Some((self, Some(MarkMode::Delete)))
}
fn remove_selected(mut self) -> Option<Self> {
if let Some(mut selected) = self.selected {
let idx = self.tree_index_by_list_position(selected);
let se_len = self.marked.len();
if let Some(idx) = idx {
self.marked.remove(&idx);
let new_len = se_len.saturating_sub(1);
if new_len == 0 {
return None;
}
if new_len == selected {
selected = selected.saturating_sub(1);
}
self.selected = Some(selected);
}
Some(self)
} else {
Some(self)
}
}
fn tree_index_by_list_position(&mut self, selected: usize) -> Option<TreeIndex> {
self.marked_sorted_by_index()
.get(selected)
.map(|(k, _)| *k.to_owned())
}
fn marked_sorted_by_index(&self) -> Vec<(&TreeIndex, &EntryMark)> {
self.marked
.iter()
.sorted_by_key(|(_, v)| &v.index)
.collect()
}
fn change_selection(&mut self, direction: CursorDirection) {
self.selected = self.selected.map(|selected| {
direction
.move_cursor(selected)
.min(self.marked.len().saturating_sub(1))
});
}
pub fn render(&mut self, props: impl Borrow<MarkPaneProps>, area: Rect, buf: &mut Buffer) {
let MarkPaneProps {
border_style,
format,
} = props.borrow();
let marked: &_ = &self.marked;
let title = format!(
"Marked {} items ({}) ",
marked.len(),
format.display(marked.iter().map(|(_k, v)| v.size).sum::<u64>())
);
let selected = self.selected;
let entries = marked.values().sorted_by_key(|v| &v.index).enumerate().map(
|(idx, v): (usize, &EntryMark)| {
let (default_style, is_selected) = match selected {
Some(selected) if idx == selected => (
Style {
bg: Color::White,
..Default::default()
},
true,
),
_ => (Style::default(), false),
};
|