diff --git a/cove-tui/src/chat/tree/cursor.rs b/cove-tui/src/chat/tree/cursor.rs index dc3949b..bf0eb8f 100644 --- a/cove-tui/src/chat/tree/cursor.rs +++ b/cove-tui/src/chat/tree/cursor.rs @@ -103,19 +103,10 @@ impl TreeView { tree: &mut Tree, id: &mut M::Id, ) -> bool { - if let Some(siblings) = tree.siblings(id) { - let prev_sibling = siblings - .iter() - .zip(siblings.iter().skip(1)) - .find(|(_, s)| *s == id) - .map(|(s, _)| s); - if let Some(prev_sibling) = prev_sibling { - *id = prev_sibling.clone(); - true - } else { - false - } - } else { + if let Some(prev_sibling) = tree.prev_sibling(id) { + *id = prev_sibling; + true + } else if tree.parent(id).is_none() { // We're at the root of our tree, so we need to move to the root of // the previous tree. if let Some(prev_tree_id) = store.prev_tree(tree.root()).await { @@ -125,6 +116,8 @@ impl TreeView { } else { false } + } else { + false } } @@ -137,19 +130,10 @@ impl TreeView { tree: &mut Tree, id: &mut M::Id, ) -> bool { - if let Some(siblings) = tree.siblings(id) { - let next_sibling = siblings - .iter() - .zip(siblings.iter().skip(1)) - .find(|(s, _)| *s == id) - .map(|(_, s)| s); - if let Some(next_sibling) = next_sibling { - *id = next_sibling.clone(); - true - } else { - false - } - } else { + if let Some(next_sibling) = tree.next_sibling(id) { + *id = next_sibling; + true + } else if tree.parent(id).is_none() { // We're at the root of our tree, so we need to move to the root of // the next tree. if let Some(next_tree_id) = store.next_tree(tree.root()).await { @@ -159,6 +143,8 @@ impl TreeView { } else { false } + } else { + false } } diff --git a/cove-tui/src/store.rs b/cove-tui/src/store.rs index b5e93e9..14c5867 100644 --- a/cove-tui/src/store.rs +++ b/cove-tui/src/store.rs @@ -99,6 +99,24 @@ impl Tree { None } } + + pub fn prev_sibling(&self, id: &M::Id) -> Option { + let siblings = self.siblings(id)?; + siblings + .iter() + .zip(siblings.iter().skip(1)) + .find(|(_, s)| *s == id) + .map(|(s, _)| s.clone()) + } + + pub fn next_sibling(&self, id: &M::Id) -> Option { + let siblings = self.siblings(id)?; + siblings + .iter() + .zip(siblings.iter().skip(1)) + .find(|(s, _)| *s == id) + .map(|(_, s)| s.clone()) + } } #[async_trait]