Parcourir la source

Add confirmation when deleting commands

 + Remove corresponding line in TODO
 + Factor code and improve comments
Leo il y a 9 ans
Parent
commit
048c40d638
2 fichiers modifiés avec 21 ajouts et 14 suppressions
  1. 0 1
      TODO.md
  2. 21 13
      src/remove_command.ml

+ 0 - 1
TODO.md

@@ -2,7 +2,6 @@
 
 ## Users idees
 
- + Add confirmation on delete.
  + Add undo command ?
  + Show help in context
  + Allow to delete a command after a certain number of launch

+ 21 - 13
src/remove_command.ml

@@ -58,22 +58,30 @@ let remove current_list n =
   ( !removed, new_list )
 ;;
 
-(* Function which add the commands (one per line) ridden on stdin to the rc
- * file, and then display th new configuration *)
+(* Perform removal *)
+let perform ~rc new_list =
+  let updated_rc = { rc with Settings_t.progs = new_list } in
+  File_com.write updated_rc;
+  (* Display the result, after rereading rc *)
+  let reread_rc = File_com.init_rc () in
+  List_rc.run ~rc:reread_rc ()
+;;
+
+(* Function which removes a command, after getting confirmation, and then
+  * display the new configuration *)
 let run ~(rc:File_com.t) n_to_remove =
-  (* Get actual list of commands *)
   let actual_list = rc.Settings_t.progs in
-  (* Get nth *)
-  let nth = Option.value n_to_remove
-              ~default:((List.length actual_list) - 1) in
-  (* Remove the nth command, after display it *)
+  (* Get nth command, default last *)
+  let nth =
+    Messages.debug "Will remove command number:";
+    Option.value n_to_remove ~default:((List.length actual_list) - 1)
+    |> Tools.spy1_int
+  in
+  (* Remove the nth command, after asking *)
   let removed,new_list = remove actual_list nth in
   sprintf "Removing: %s\n" removed
   |> Messages.warning;
-  (* Write new list to rc file *)
-  let updated_rc = { rc with Settings_t.progs = new_list } in
-  File_com.write updated_rc;
-  (* Display the result *)
-  let reread_rc = File_com.init_rc () in
-  List_rc.run ~rc:reread_rc ()
+  match Messages.confirm "Are you sure?" with
+  | Messages.Yes -> perform ~rc new_list
+  | Messages.No -> Messages.ok "Exiting, nothing done"; exit 0;
 ;;