Enter to expand database tree (#85)

* enter to expand database tree

* always check if the key is enter

* return current_selection

* fix clippy warnings
pull/86/head
Takayuki Maeda 3 years ago committed by GitHub
parent 6f8b13714a
commit f42ebafe68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,6 +16,7 @@ pub enum MoveSelection {
Right,
Top,
End,
Enter,
}
#[derive(Debug, Clone, Copy)]
@ -112,6 +113,7 @@ impl DatabaseTree {
MoveSelection::Right => self.selection_right(selection),
MoveSelection::Top => Self::selection_start(selection),
MoveSelection::End => self.selection_end(selection),
MoveSelection::Enter => self.expand(selection),
};
let changed_index = new_index.map(|i| i != selection).unwrap_or_default();
@ -315,6 +317,22 @@ impl DatabaseTree {
self.select_parent(current_index)
}
fn expand(&mut self, current_selection: usize) -> Option<usize> {
let item = &mut self.items.tree_items.get(current_selection)?;
if item.kind().is_database() && item.kind().is_database_collapsed() {
self.items.expand(current_selection, false);
return Some(current_selection);
}
if item.kind().is_schema() && item.kind().is_schema_collapsed() {
self.items.expand(current_selection, false);
return Some(current_selection);
}
None
}
fn selection_right(&mut self, current_selection: usize) -> Option<usize> {
let item = &mut self.items.tree_items.get(current_selection)?;
@ -415,6 +433,62 @@ mod test {
assert_eq!(tree.selection, Some(2));
}
#[test]
fn test_expand() {
let items = vec![Database::new(
"a".to_string(),
vec![Table::new("b".to_string()).into()],
)];
// a
// b
let mut tree = DatabaseTree::new(&items, &BTreeSet::new()).unwrap();
assert!(tree.move_selection(MoveSelection::Enter));
assert!(!tree.items.tree_items[0].kind().is_database_collapsed());
assert_eq!(tree.selection, Some(0));
assert!(tree.move_selection(MoveSelection::Down));
assert_eq!(tree.selection, Some(1));
assert!(!tree.move_selection(MoveSelection::Enter));
assert_eq!(tree.selection, Some(1));
let items = vec![Database::new(
"a".to_string(),
vec![Schema {
name: "b".to_string(),
tables: vec![Table::new("c".to_string()).into()],
}
.into()],
)];
// a
// b
// c
let mut tree = DatabaseTree::new(&items, &BTreeSet::new()).unwrap();
assert!(tree.move_selection(MoveSelection::Enter));
assert!(!tree.items.tree_items[0].kind().is_database_collapsed());
assert!(tree.items.tree_items[1].kind().is_schema_collapsed());
assert_eq!(tree.selection, Some(0));
assert!(tree.move_selection(MoveSelection::Down));
assert_eq!(tree.selection, Some(1));
assert!(tree.move_selection(MoveSelection::Enter));
assert!(!tree.items.tree_items[1].kind().is_database_collapsed());
assert_eq!(tree.selection, Some(1));
assert!(tree.move_selection(MoveSelection::Down));
assert_eq!(tree.selection, Some(2));
assert!(!tree.move_selection(MoveSelection::Enter));
assert_eq!(tree.selection, Some(2));
}
#[test]
fn test_selection_multiple_up_down() {
let items = vec![Database::new(

@ -322,14 +322,14 @@ impl App {
}
}
Focus::DabataseList => {
if self.databases.event(key)?.is_consumed() {
return Ok(EventState::Consumed);
}
let state = self.databases.event(key)?;
if key == self.config.key_config.enter && self.databases.tree_focused() {
self.update_table().await?;
return Ok(EventState::Consumed);
}
return Ok(state);
}
Focus::Table => {
match self.tab.selected_tab {

@ -22,6 +22,8 @@ pub fn common_nav(key: Key, key_config: &KeyConfig) -> Option<MoveSelection> {
Some(MoveSelection::Top)
} else if key == key_config.scroll_to_bottom {
Some(MoveSelection::End)
} else if key == key_config.enter {
Some(MoveSelection::Enter)
} else {
None
}

Loading…
Cancel
Save