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
|
use crate::interactive::{
app::{FocussedPane, TerminalApp},
sorted_entries,
widgets::{HelpPane, MarkPane},
};
use dua::path_of;
use itertools::Itertools;
use petgraph::Direction;
pub enum CursorDirection {
PageDown,
Down,
Up,
PageUp,
}
impl CursorDirection {
pub fn move_cursor(&self, n: usize) -> usize {
use CursorDirection::*;
match self {
Down => n.saturating_add(1),
Up => n.saturating_sub(1),
PageDown => n.saturating_add(10),
PageUp => n.saturating_sub(10),
}
}
}
impl TerminalApp {
pub fn cycle_focus(&mut self) {
use FocussedPane::*;
self.state.focussed = match (
self.state.focussed,
&self.window.help_pane,
&self.window.mark_pane,
) {
(Main, Some(_), _) => Help,
(Help, _, Some(_)) => Mark,
(Help, _, None) => Main,
(Mark, _, _) => Main,
(Main, None, None) => Main,
(Main, None, Some(_)) => Mark,
};
}
pub fn toggle_help_pane(&mut self) {
use FocussedPane::*;
self.state.focussed = match self.state.focussed {
Main | Mark => {
self.window.help_pane = Some(HelpPane::default());
Help
}
Help => {
self.window.help_pane = None;
Main
}
}
}
pub fn update_message(&mut self) {
self.state.message = None;
}
pub fn open_that(&mut self) {
match self.state.selected {
Some(ref idx) => {
open::that(path_of(&self.traversal.tree, *idx)).ok();
}
None => {}
}
}
pub fn exit_node(&mut self) {
match self
.traversal
.tree
.neighbors_directed(self.state.root, Direction::Incoming)
.next()
{
Some(parent_idx) => {
self.state.root = parent_idx;
self.state.entries =
sorted_entries(&self.traversal.tree, parent_idx, self.state.sorting);
self.state.selected = self.state.entries.get(0).map(|b| b.index);
}
None => self.state.message = Some("Top level reached".into()),
}
}
pub fn enter_node(&mut self) {
if let Some(new_root) = self.state.selected {
let new_entries = sorted_entries(&self.traversal.tree, new_root, self.state.sorting);
match new_entries.get(0) {
Some(b) => {
self.state.root = new_root;
self.state.selected = Some(b.index);
self.state.entries = new_entries;
}
None => self.state.message = Some("Entry is a file or an empty directory".into()),
}
}
}
pub fn change_entry_selection(&mut self, direction: CursorDirection) {
let entries = sorted_entries(&self.traversal.tree, self.state.root, self.state.sorting);
let next_selected_pos = match self.state.selected {
Some(ref selected) => entries
.iter()
.find_position(|b| b.index == *selected)
.map(|(idx, _)| direction.move_cursor(idx))
.unwrap_or(0),
None => 0,
};
self.state.selected = entries
.get(next_selected_pos)
.or(entries.last())
.map(|b| b.index)
.or(self.state.selected)
}
pub fn cycle_sorting(&mut self) {
self.state.sorting.toggle_size();
self.state.entries =
sorted_entries(&self.traversal.tree, self.state.root, self.state.sorting);
}
pub fn mark_entry(&mut self, advance_cursor: bool) {
match (self.state.selected, self.window.mark_pane.take()) {
(Some(index), Some(pane)) => {
self.window.mark_pane = pane.toggle_index(index, &self.traversal.tree);
}
(Some(index), None) => {
self.window.mark_pane =
MarkPane::default().toggle_index(index, &self.traversal.tree)
}
_ => {}
};
if advance_cursor {
self.change_entry_selection(CursorDirection::Down)
}
}
}
|