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,20 +106,24 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
TreeNavResult::Continue TreeNavResult::Continue
} }
_ => { _ => {
let idx = crate::modulo(new_cur.tree_addr[0], self.data.len() as isize); if self.data.len() > 0 {
let idx = crate::modulo(new_cur.tree_addr[0], self.data.len() as isize);
self.cursor.set(ListCursor { self.cursor.set(ListCursor {
mode: ListCursorMode::Select, mode: ListCursorMode::Select,
idx: Some(idx), idx: Some(idx),
}); });
let item = self.data.get_mut(idx as usize); let item = self.data.get_mut(idx as usize);
let mut item_edit = item.write().unwrap(); let mut item_edit = item.write().unwrap();
item_edit.goto(TreeCursor { item_edit.goto(TreeCursor {
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,50 +193,62 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
depth => { depth => {
// nested // nested
let item = self.data.get_mut(cur.tree_addr[0] as usize); if cur.tree_addr[0] < self.data.len() as isize {
let mut item_edit = item.write().unwrap(); let item = self.data.get_mut(cur.tree_addr[0] as usize);
let mut item_edit = item.write().unwrap();
match item_edit.goby(direction) { match item_edit.goby(direction) {
TreeNavResult::Exit => { TreeNavResult::Exit => {
if direction.y < 0 { if direction.y < 0 {
// up // up
self.cursor.set(ListCursor { self.cursor.set(ListCursor {
mode: cur.leaf_mode, mode: cur.leaf_mode,
idx: Some(cur.tree_addr[0]) idx: Some(cur.tree_addr[0])
}); });
TreeNavResult::Continue TreeNavResult::Continue
} else if direction.y > 0 { } else if direction.y > 0 {
// dn // dn
TreeNavResult::Continue TreeNavResult::Continue
} else {
// horizontal
drop(item_edit);
if (cur.tree_addr[0]+direction.x >= 0) &&
(cur.tree_addr[0]+direction.x < self.data.len() as isize)
{
if direction.x < 0 {
cur.tree_addr[0] -= 1;
for i in 1..depth {
cur.tree_addr[i] = -1;
}
} else {
cur.tree_addr[0] += 1;
for i in 1..depth {
cur.tree_addr[i] = 0;
}
}
self.goto(cur)
} else { } else {
self.cursor.set(ListCursor::none()); // horizontal
TreeNavResult::Exit drop(item_edit);
if (cur.tree_addr[0]+direction.x >= 0) &&
(cur.tree_addr[0]+direction.x < self.data.len() as isize)
{
if direction.x < 0 {
cur.tree_addr[0] -= 1;
for i in 1..depth {
cur.tree_addr[i] = -1;
}
} else {
cur.tree_addr[0] += 1;
for i in 1..depth {
cur.tree_addr[i] = 0;
}
}
self.goto(cur)
} else {
self.cursor.set(ListCursor::none());
TreeNavResult::Exit
}
} }
} }
TreeNavResult::Continue => TreeNavResult::Continue
} }
TreeNavResult::Continue => TreeNavResult::Continue
} else {
self.cursor.set(
ListCursor {
mode: ListCursorMode::Insert,
idx: Some(0)
}
);
TreeNavResult::Continue
} }
} }
} }