From ee907e75e565a10f353d22c017af3366c521ca47 Mon Sep 17 00:00:00 2001
From: Hernawan Fa'iz Abdillah <hernawan.faiz.abdillah@gmail.com>
Date: Thu, 20 Jul 2023 14:03:31 +0700
Subject: [PATCH] handle menu navigation inapplicable on single item

---
 src/engine.rs | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/engine.rs b/src/engine.rs
index 3ece6d4c..56b9e8b2 100644
--- a/src/engine.rs
+++ b/src/engine.rs
@@ -892,6 +892,9 @@ impl Reedline {
             ReedlineEvent::MenuNext => {
                 self.active_menu()
                     .map_or(Ok(EventStatus::Inapplicable), |menu| {
+                        if menu.get_values().len() < 2 {
+                            return Ok(EventStatus::Inapplicable);
+                        }
                         menu.menu_event(MenuEvent::NextElement);
                         Ok(EventStatus::Handled)
                     })
@@ -899,6 +902,9 @@ impl Reedline {
             ReedlineEvent::MenuPrevious => {
                 self.active_menu()
                     .map_or(Ok(EventStatus::Inapplicable), |menu| {
+                        if menu.get_values().len() < 2 {
+                            return Ok(EventStatus::Inapplicable);
+                        }
                         menu.menu_event(MenuEvent::PreviousElement);
                         Ok(EventStatus::Handled)
                     })
@@ -906,6 +912,9 @@ impl Reedline {
             ReedlineEvent::MenuUp => {
                 self.active_menu()
                     .map_or(Ok(EventStatus::Inapplicable), |menu| {
+                        if menu.get_values().len() < 2 {
+                            return Ok(EventStatus::Inapplicable);
+                        }
                         menu.menu_event(MenuEvent::MoveUp);
                         Ok(EventStatus::Handled)
                     })
@@ -913,6 +922,9 @@ impl Reedline {
             ReedlineEvent::MenuDown => {
                 self.active_menu()
                     .map_or(Ok(EventStatus::Inapplicable), |menu| {
+                        if menu.get_values().len() < 2 {
+                            return Ok(EventStatus::Inapplicable);
+                        }
                         menu.menu_event(MenuEvent::MoveDown);
                         Ok(EventStatus::Handled)
                     })
@@ -920,6 +932,9 @@ impl Reedline {
             ReedlineEvent::MenuLeft => {
                 self.active_menu()
                     .map_or(Ok(EventStatus::Inapplicable), |menu| {
+                        if menu.get_values().len() < 2 {
+                            return Ok(EventStatus::Inapplicable);
+                        }
                         menu.menu_event(MenuEvent::MoveLeft);
                         Ok(EventStatus::Handled)
                     })
@@ -927,6 +942,9 @@ impl Reedline {
             ReedlineEvent::MenuRight => {
                 self.active_menu()
                     .map_or(Ok(EventStatus::Inapplicable), |menu| {
+                        if menu.get_values().len() < 2 {
+                            return Ok(EventStatus::Inapplicable);
+                        }
                         menu.menu_event(MenuEvent::MoveRight);
                         Ok(EventStatus::Handled)
                     })