feat: implement TreeState select first/last
This commit is contained in:
+3
-1
@@ -91,11 +91,13 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
|||||||
if let Event::Key(key) = event::read()? {
|
if let Event::Key(key) = event::read()? {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char('q') => return Ok(()),
|
KeyCode::Char('q') => return Ok(()),
|
||||||
|
KeyCode::Char('\n' | ' ') => app.tree.toggle(),
|
||||||
KeyCode::Left => app.tree.left(),
|
KeyCode::Left => app.tree.left(),
|
||||||
KeyCode::Right => app.tree.right(),
|
KeyCode::Right => app.tree.right(),
|
||||||
KeyCode::Char('\n') => app.tree.toggle(),
|
|
||||||
KeyCode::Down => app.tree.down(),
|
KeyCode::Down => app.tree.down(),
|
||||||
KeyCode::Up => app.tree.up(),
|
KeyCode::Up => app.tree.up(),
|
||||||
|
KeyCode::Home => app.tree.first(),
|
||||||
|
KeyCode::End => app.tree.last(),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,14 @@ impl<'a> StatefulTree<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn first(&mut self) {
|
||||||
|
self.state.select_first();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last(&mut self) {
|
||||||
|
self.state.select_last(&self.items);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn down(&mut self) {
|
pub fn down(&mut self) {
|
||||||
self.state.key_down(&self.items);
|
self.state.key_down(&self.items);
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -86,6 +86,21 @@ impl TreeState {
|
|||||||
self.opened.clear();
|
self.opened.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Select the first node.
|
||||||
|
pub fn select_first(&mut self) {
|
||||||
|
self.select(vec![0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Select the last node.
|
||||||
|
pub fn select_last(&mut self, items: &[TreeItem]) {
|
||||||
|
let visible = flatten(&self.get_all_opened(), items);
|
||||||
|
let new_identifier = visible
|
||||||
|
.last()
|
||||||
|
.map(|o| o.identifier.clone())
|
||||||
|
.unwrap_or_default();
|
||||||
|
self.select(new_identifier);
|
||||||
|
}
|
||||||
|
|
||||||
/// Handles the up arrow key.
|
/// Handles the up arrow key.
|
||||||
/// Moves up in the current depth or to its parent.
|
/// Moves up in the current depth or to its parent.
|
||||||
pub fn key_up(&mut self, items: &[TreeItem]) {
|
pub fn key_up(&mut self, items: &[TreeItem]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user