list nav: catch some out of bounds errors

This commit is contained in:
Michael Sippel 2022-09-28 13:33:51 +02:00
parent 4e02b41a77
commit 507887535f

View file

@ -106,6 +106,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
TreeNavResult::Continue TreeNavResult::Continue
} }
_ => { _ => {
if self.data.len() > 0 {
let idx = crate::modulo(new_cur.tree_addr[0], self.data.len() as isize); let idx = crate::modulo(new_cur.tree_addr[0], self.data.len() as isize);
self.cursor.set(ListCursor { self.cursor.set(ListCursor {
@ -120,6 +121,9 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
leaf_mode: new_cur.leaf_mode, leaf_mode: new_cur.leaf_mode,
tree_addr: new_cur.tree_addr[1..].iter().cloned().collect(), tree_addr: new_cur.tree_addr[1..].iter().cloned().collect(),
}); });
} else {
self.cursor.set(ListCursor::home());
}
TreeNavResult::Continue TreeNavResult::Continue
} }
@ -176,7 +180,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
+ if cur.leaf_mode == ListCursorMode::Insert { 1 } else { 0 }) + if cur.leaf_mode == ListCursorMode::Insert { 1 } else { 0 })
{ {
self.cursor.set(ListCursor { self.cursor.set(ListCursor {
mode: cur.leaf_mode, mode: if self.data.len() == 0 { ListCursorMode::Insert } else { cur.leaf_mode },
idx: Some(cur.tree_addr[0] + direction.x) idx: Some(cur.tree_addr[0] + direction.x)
}); });
TreeNavResult::Continue TreeNavResult::Continue
@ -189,6 +193,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
depth => { depth => {
// nested // nested
if cur.tree_addr[0] < self.data.len() as isize {
let item = self.data.get_mut(cur.tree_addr[0] as usize); let item = self.data.get_mut(cur.tree_addr[0] as usize);
let mut item_edit = item.write().unwrap(); let mut item_edit = item.write().unwrap();
@ -234,6 +239,17 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
} }
TreeNavResult::Continue => TreeNavResult::Continue TreeNavResult::Continue => TreeNavResult::Continue
} }
} else {
self.cursor.set(
ListCursor {
mode: ListCursorMode::Insert,
idx: Some(0)
}
);
TreeNavResult::Continue
}
} }
} }
} }