From 3515551baf1f87f2dff7538940f3056052bad346 Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Wed, 3 Dec 2025 14:23:44 +0000 Subject: [PATCH] fix: handle cancelled menu without crashing When pressing Ctrl+C in a menu, the render_menu function would crash with IndexError because element.selected was invalid after cancellation. Now we check if the element was cancelled or has an invalid selection before trying to access the selected option, and display 'Cancelled.' instead. --- src/rich_toolkit/styles/base.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/rich_toolkit/styles/base.py b/src/rich_toolkit/styles/base.py index 8670016..6c24cb0 100644 --- a/src/rich_toolkit/styles/base.py +++ b/src/rich_toolkit/styles/base.py @@ -309,10 +309,17 @@ def render_menu( ) result_content.append(" ") - result_content.append( - element.options[element.selected]["name"], - style=self.console.get_style("result"), - ) + # Check if cancelled or invalid selection + if element._cancelled or not (0 <= element.selected < len(element.options)): + result_content.append( + "Cancelled.", + style=self.console.get_style("cancelled"), + ) + else: + result_content.append( + element.options[element.selected]["name"], + style=self.console.get_style("result"), + ) return result_content