Skip to content

Commit 4591fbb

Browse files
author
Stephan Dilly
committed
tree view commands (see #714)
1 parent 0e31d57 commit 4591fbb

File tree

3 files changed

+99
-81
lines changed

3 files changed

+99
-81
lines changed

Diff for: filetree/src/filetree.rs

+49-42
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl FileTree {
4646
Ok(new_self)
4747
}
4848

49+
///
50+
pub const fn is_empty(&self) -> bool {
51+
self.items.file_count() == 0
52+
}
53+
4954
///
5055
pub fn collapse_but_root(&mut self) {
5156
self.items.collapse(0, true);
@@ -67,46 +72,23 @@ impl FileTree {
6772
)
6873
}
6974

70-
fn visual_index_to_absolute(
71-
&self,
72-
visual_index: usize,
73-
) -> Option<usize> {
74-
self.items
75-
.iterate(0, self.items.len())
76-
.enumerate()
77-
.find_map(|(i, (abs, _))| {
78-
if i == visual_index {
79-
Some(abs)
80-
} else {
81-
None
82-
}
83-
})
84-
}
85-
8675
///
8776
pub const fn visual_selection(&self) -> Option<&VisualSelection> {
8877
self.visual_selection.as_ref()
8978
}
9079

91-
fn calc_visual_selection(&self) -> Option<VisualSelection> {
92-
self.selection.map(|selection_absolute| {
93-
let mut count = 0;
94-
let mut visual_index = 0;
95-
for (index, _item) in
96-
self.items.iterate(0, self.items.len())
97-
{
98-
if selection_absolute == index {
99-
visual_index = count;
100-
}
101-
102-
count += 1;
103-
}
80+
///
81+
pub fn collapse_recursive(&mut self) {
82+
if let Some(selection) = self.selection {
83+
self.items.collapse(selection, true);
84+
}
85+
}
10486

105-
VisualSelection {
106-
index: visual_index,
107-
count,
108-
}
109-
})
87+
///
88+
pub fn expand_recursive(&mut self) {
89+
if let Some(selection) = self.selection {
90+
self.items.expand(selection, true);
91+
}
11092
}
11193

11294
///
@@ -141,16 +123,41 @@ impl FileTree {
141123
})
142124
}
143125

144-
pub fn collapse_recursive(&mut self) {
145-
if let Some(selection) = self.selection {
146-
self.items.collapse(selection, true);
147-
}
126+
fn visual_index_to_absolute(
127+
&self,
128+
visual_index: usize,
129+
) -> Option<usize> {
130+
self.items
131+
.iterate(0, self.items.len())
132+
.enumerate()
133+
.find_map(|(i, (abs, _))| {
134+
if i == visual_index {
135+
Some(abs)
136+
} else {
137+
None
138+
}
139+
})
148140
}
149141

150-
pub fn expand_recursive(&mut self) {
151-
if let Some(selection) = self.selection {
152-
self.items.expand(selection, true);
153-
}
142+
fn calc_visual_selection(&self) -> Option<VisualSelection> {
143+
self.selection.map(|selection_absolute| {
144+
let mut count = 0;
145+
let mut visual_index = 0;
146+
for (index, _item) in
147+
self.items.iterate(0, self.items.len())
148+
{
149+
if selection_absolute == index {
150+
visual_index = count;
151+
}
152+
153+
count += 1;
154+
}
155+
156+
VisualSelection {
157+
index: visual_index,
158+
count,
159+
}
160+
})
154161
}
155162

156163
const fn selection_start(current_index: usize) -> Option<usize> {

Diff for: src/components/revision_files.rs

+49-38
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{
77
use crate::{
88
keys::SharedKeyConfig,
99
queue::Queue,
10-
strings,
10+
strings::{self, order},
1111
ui::{self, style::SharedTheme},
1212
};
1313
use anyhow::Result;
@@ -109,10 +109,6 @@ impl RevisionFilesComponent {
109109
let path = format!("{}{}{}", indent_str, path_arrow, path);
110110
Span::styled(path, theme.file_tree_item(is_path, selected))
111111
}
112-
113-
fn move_selection(&mut self, dir: MoveSelection) -> bool {
114-
self.tree.move_selection(dir)
115-
}
116112
}
117113

118114
impl DrawableComponent for RevisionFilesComponent {
@@ -154,13 +150,6 @@ impl DrawableComponent for RevisionFilesComponent {
154150
f,
155151
area,
156152
&self.title,
157-
// &format!(
158-
// "{}/{} (height: {}) (top: {})",
159-
// selection.index,
160-
// selection.count,
161-
// tree_height,
162-
// self.scroll_top.get()
163-
// ),
164153
items,
165154
true,
166155
&self.theme,
@@ -188,6 +177,8 @@ impl Component for RevisionFilesComponent {
188177
)
189178
.order(1),
190179
);
180+
181+
tree_nav_cmds(&self.tree, &self.key_config, out);
191182
}
192183

193184
visibility_blocking(self)
@@ -202,33 +193,8 @@ impl Component for RevisionFilesComponent {
202193
let consumed = if key == self.key_config.exit_popup {
203194
self.hide();
204195
true
205-
} else if key == self.key_config.move_down {
206-
self.move_selection(MoveSelection::Down)
207-
} else if key == self.key_config.move_up {
208-
self.move_selection(MoveSelection::Up)
209-
} else if key == self.key_config.move_right {
210-
self.move_selection(MoveSelection::Right)
211-
} else if key == self.key_config.move_left {
212-
self.move_selection(MoveSelection::Left)
213-
} else if key == self.key_config.home
214-
|| key == self.key_config.shift_up
215-
{
216-
self.move_selection(MoveSelection::Top)
217-
} else if key == self.key_config.end
218-
|| key == self.key_config.shift_down
219-
{
220-
self.move_selection(MoveSelection::End)
221-
} else if key
222-
== self.key_config.tree_collapse_recursive
223-
{
224-
self.tree.collapse_recursive();
225-
true
226-
} else if key == self.key_config.tree_expand_recursive
227-
{
228-
self.tree.expand_recursive();
229-
true
230196
} else {
231-
false
197+
tree_nav(&mut self.tree, &self.key_config, key)
232198
};
233199

234200
return Ok(consumed.into());
@@ -252,3 +218,48 @@ impl Component for RevisionFilesComponent {
252218
Ok(())
253219
}
254220
}
221+
222+
//TODO: reuse for other tree usages
223+
fn tree_nav_cmds(
224+
tree: &FileTree,
225+
key_config: &SharedKeyConfig,
226+
out: &mut Vec<CommandInfo>,
227+
) {
228+
out.push(
229+
CommandInfo::new(
230+
strings::commands::navigate_tree(key_config),
231+
!tree.is_empty(),
232+
true,
233+
)
234+
.order(order::NAV),
235+
);
236+
}
237+
238+
//TODO: reuse for other tree usages
239+
fn tree_nav(
240+
tree: &mut FileTree,
241+
key_config: &SharedKeyConfig,
242+
key: crossterm::event::KeyEvent,
243+
) -> bool {
244+
if key == key_config.move_down {
245+
tree.move_selection(MoveSelection::Down)
246+
} else if key == key_config.move_up {
247+
tree.move_selection(MoveSelection::Up)
248+
} else if key == key_config.move_right {
249+
tree.move_selection(MoveSelection::Right)
250+
} else if key == key_config.move_left {
251+
tree.move_selection(MoveSelection::Left)
252+
} else if key == key_config.home || key == key_config.shift_up {
253+
tree.move_selection(MoveSelection::Top)
254+
} else if key == key_config.end || key == key_config.shift_down {
255+
tree.move_selection(MoveSelection::End)
256+
} else if key == key_config.tree_collapse_recursive {
257+
tree.collapse_recursive();
258+
true
259+
} else if key == key_config.tree_expand_recursive {
260+
tree.expand_recursive();
261+
true
262+
} else {
263+
false
264+
}
265+
}

Diff for: src/strings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub mod commands {
351351
key_config.get_hint(key_config.move_right),
352352
key_config.get_hint(key_config.move_left)
353353
),
354-
"navigate tree view",
354+
"navigate tree view, collapse, expand",
355355
CMD_GROUP_GENERAL,
356356
)
357357
}

0 commit comments

Comments
 (0)