From 2fd209c502f1101d86fa2a4d88e2349aa1c86c0a Mon Sep 17 00:00:00 2001
From: Michael Sippel <micha@fragmental.art>
Date: Sun, 7 Nov 2021 08:50:36 +0100
Subject: [PATCH] list editor: catch out of bounds in ListEditor::get_item()

---
 nested/src/list/editor.rs | 6 +++++-
 shell/src/main.rs         | 4 +++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/nested/src/list/editor.rs b/nested/src/list/editor.rs
index e80d43a..261efe9 100644
--- a/nested/src/list/editor.rs
+++ b/nested/src/list/editor.rs
@@ -698,7 +698,11 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
 
     pub fn get_item(&self) -> Option<Arc<RwLock<ItemEditor>>> {
         if let Some(idx) = self.cursor.get().idx {
-            Some(self.data.get(idx))
+            if idx < self.data.len() {
+                Some(self.data.get(idx))
+            } else {
+                None
+            }
         } else {
             None
         }
diff --git a/shell/src/main.rs b/shell/src/main.rs
index c53f5ab..004e164 100644
--- a/shell/src/main.rs
+++ b/shell/src/main.rs
@@ -162,7 +162,9 @@ async fn main() {
                         process_list_editor.goto_end();
                     }
                     TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
-                        process_list_editor.get_item().unwrap().write().unwrap().launch_pty2();
+                        if let Some(launcher) = process_list_editor.get_item() {
+                            launcher.write().unwrap().launch_pty2();
+                        }
                     }
 
                     ev => {