Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 748dfc3

Browse files
author
Sebastian Thiel
committed
add 'k' navigation key
1 parent a76ad50 commit 748dfc3

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

src/interactive/app.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ pub struct TerminalApp {
2626
pub state: DisplayState,
2727
}
2828

29+
enum CursorDirection {
30+
Down,
31+
Up,
32+
}
33+
2934
impl TerminalApp {
3035
fn draw<B>(&self, terminal: &mut Terminal<B>) -> Result<(), Error>
3136
where
@@ -63,24 +68,10 @@ impl TerminalApp {
6368
self.draw(terminal)?;
6469
for key in keys.filter_map(Result::ok) {
6570
match key {
66-
Char('j') => {
67-
let entries =
68-
sorted_entries(&self.traversal.tree, self.state.root, self.state.sorting);
69-
let next_selected_pos = match self.state.selected {
70-
Some(ref selected) => entries
71-
.iter()
72-
.find_position(|(idx, _)| *idx == *selected)
73-
.map(|(idx, _)| idx + 1)
74-
.unwrap_or(0),
75-
None => 0,
76-
};
77-
self.state.selected = match entries.get(next_selected_pos) {
78-
Some((idx, _)) => Some(*idx),
79-
None => self.state.selected,
80-
};
81-
}
71+
Char('k') => self.change_vertical_index(CursorDirection::Up),
72+
Char('j') => self.change_vertical_index(CursorDirection::Down),
8273
Char('s') => self.state.sorting.toggle_size(),
83-
Ctrl('c') | Char('\n') | Char('q') => break,
74+
Ctrl('c') | Char('q') => break,
8475
_ => {}
8576
};
8677
self.draw(terminal)?;
@@ -90,6 +81,25 @@ impl TerminalApp {
9081
})
9182
}
9283

84+
fn change_vertical_index(&mut self, direction: CursorDirection) -> () {
85+
let entries = sorted_entries(&self.traversal.tree, self.state.root, self.state.sorting);
86+
let next_selected_pos = match self.state.selected {
87+
Some(ref selected) => entries
88+
.iter()
89+
.find_position(|(idx, _)| *idx == *selected)
90+
.map(|(idx, _)| match direction {
91+
CursorDirection::Down => idx.saturating_add(1),
92+
CursorDirection::Up => idx.saturating_sub(1),
93+
})
94+
.unwrap_or(0),
95+
None => 0,
96+
};
97+
self.state.selected = match entries.get(next_selected_pos) {
98+
Some((idx, _)) => Some(*idx),
99+
None => self.state.selected,
100+
};
101+
}
102+
93103
pub fn initialize<B>(
94104
terminal: &mut Terminal<B>,
95105
options: WalkOptions,

tests/interactive.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,35 @@ mod app {
118118
}
119119

120120
// Entry-Navigation
121-
// when hitting the j key
122121
{
122+
// when hitting the j key
123123
app.process_events(&mut terminal, b"j".keys())?;
124124
assert_eq!(
125125
node_by_name(&app, fixture_str(long_root)),
126126
node_by_index(&app, *app.state.selected.as_ref().unwrap()),
127127
"it moves the cursor down and selects the next entry based on the current sort mode"
128128
);
129+
// when hitting it while there is nowhere to go
130+
app.process_events(&mut terminal, b"j".keys())?;
131+
assert_eq!(
132+
node_by_name(&app, fixture_str(long_root)),
133+
node_by_index(&app, *app.state.selected.as_ref().unwrap()),
134+
"it stays at the previous position"
135+
);
136+
// when hitting the k key
137+
app.process_events(&mut terminal, b"k".keys())?;
138+
assert_eq!(
139+
node_by_name(&app, fixture_str(short_root)),
140+
node_by_index(&app, *app.state.selected.as_ref().unwrap()),
141+
"it moves the cursor up and selects the next entry based on the current sort mode"
142+
);
143+
// when hitting the k key again
144+
app.process_events(&mut terminal, b"k".keys())?;
145+
assert_eq!(
146+
node_by_name(&app, fixture_str(short_root)),
147+
node_by_index(&app, *app.state.selected.as_ref().unwrap()),
148+
"it stays at the current cursor position as there is nowhere to go"
149+
);
129150
}
130151

131152
Ok(())

0 commit comments

Comments
 (0)