@@ -10,6 +10,15 @@ use std::path::PathBuf;
1010use std:: process;
1111use textwrap:: fill;
1212
13+ // Utility function to parse command index from formatted string like "1. Command Name"
14+ fn parse_command_index ( index_str : & str ) -> Option < usize > {
15+ index_str
16+ . split ( '.' )
17+ . next ( )
18+ . and_then ( |num| num. trim ( ) . parse :: < usize > ( ) . ok ( ) )
19+ . and_then ( |num| num. checked_sub ( 1 ) )
20+ }
21+
1322pub fn edit_menu ( config_path : & PathBuf ) {
1423 let mut config = crate :: config:: load_config ( config_path) . unwrap_or_else ( |e| {
1524 eprintln ! ( "Error: {e}" ) ;
@@ -62,11 +71,18 @@ pub fn edit_menu(config_path: &PathBuf) {
6271 match save_prompt {
6372 Some ( "Yes" ) => {
6473 if validate_json ( & config) {
65- save_config ( config_path, & config) ;
66- println ! (
67- "✅ Changes Saved. Press any key to return to Main Menu..."
68- ) ;
69- pause ( ) ;
74+ match save_config ( config_path, & config) {
75+ Ok ( _) => {
76+ println ! (
77+ "✅ Changes Saved. Press any key to return to Main Menu..."
78+ ) ;
79+ pause ( ) ;
80+ }
81+ Err ( e) => {
82+ eprintln ! ( "❌ Error saving config: {e}" ) ;
83+ pause ( ) ;
84+ }
85+ }
7086 } else {
7187 println ! ( "❌ Error: Invalid JSON format. Changes not saved." ) ;
7288 }
@@ -133,13 +149,13 @@ pub fn edit_command(config: &mut Config, changes_made: &mut bool) {
133149 None => return ,
134150 } ;
135151
136- let command_number = command_index
137- . split ( '.' )
138- . next ( )
139- . unwrap ( )
140- . parse :: < usize > ( )
141- . unwrap ( )
142- - 1 ;
152+ let command_number = match parse_command_index ( & command_index) {
153+ Some ( num ) => num ,
154+ None => {
155+ println ! ( "❌ Invalid command selection." ) ;
156+ return ;
157+ }
158+ } ;
143159
144160 let display_name = match prompt_or_return ( || {
145161 inquire:: Text :: new ( "Enter the new display name for the command:" )
@@ -186,13 +202,13 @@ pub fn reorder_command(config: &mut Config, changes_made: &mut bool) {
186202 None => return ,
187203 } ;
188204
189- let command_number = command_index
190- . split ( '.' )
191- . next ( )
192- . unwrap ( )
193- . parse :: < usize > ( )
194- . unwrap ( )
195- - 1 ;
205+ let command_number = match parse_command_index ( & command_index) {
206+ Some ( num ) => num ,
207+ None => {
208+ println ! ( "❌ Invalid command selection." ) ;
209+ return ;
210+ }
211+ } ;
196212
197213 let new_position: usize = match prompt_or_return ( || {
198214 inquire:: Text :: new ( "Enter the new position for this command:" )
@@ -236,13 +252,13 @@ pub fn delete_command(config: &mut Config, changes_made: &mut bool) {
236252 None => return ,
237253 } ;
238254
239- let command_number = command_index
240- . split ( '.' )
241- . next ( )
242- . unwrap ( )
243- . parse :: < usize > ( )
244- . unwrap ( )
245- - 1 ;
255+ let command_number = match parse_command_index ( & command_index) {
256+ Some ( num ) => num ,
257+ None => {
258+ println ! ( "❌ Invalid command selection." ) ;
259+ return ;
260+ }
261+ } ;
246262
247263 let deleted = config. commands . remove ( command_number) ;
248264 println ! (
0 commit comments