Parcourir la source

Add reediting to edit command

Leo il y a 9 ans
Parent
commit
4870e110f2
5 fichiers modifiés avec 27 ajouts et 13 suppressions
  1. 2 1
      CHANGELOG.md
  2. 2 3
      dev.json
  3. 20 7
      src/edit_command.ml
  4. 1 1
      src/messages.ml
  5. 2 1
      src/tmp_file.ml

+ 2 - 1
CHANGELOG.md

@@ -14,7 +14,8 @@
    command or the next one. The problem is that you can't call it with an
    option. To do this, use the `run` subcommand.
  + Improve list subcommand, now using Textutils library, displaying in an array
- + Improve editing command (explain how to use to add commands, improve messages…)
+ + Improve editing command (explain how to use to add commands, improve
+   messages, offer to reedit when nothing was done).
  + Code clean up
  + Add unit tests
  + Add licence warning

+ 2 - 3
dev.json

@@ -1,8 +1,7 @@
 {
   "progs": [
-    "ydump dev.json", "task", "task +next", "task +rdv +me",
-    "echo \"Finish\"", "bdump -w daemon,rc,commands /tmp/v033",
-    "ydump dev.json"
+    "ydump dev.json", "task", "task +next", "task +rdv", "echo \"Finish\"",
+    "bdump -w daemon,rc,commands /tmp/v033", "ydump dev.json"
   ],
   "settings": []
 }

+ 20 - 7
src/edit_command.ml

@@ -74,7 +74,7 @@ let rec gen_modification items =
 
 (* Function which get the nth element, put it in a file, let the user edit it,
  * and then remplace with the new result *)
-let run ~(rc:File_com.t) position =
+let rec run ~(rc:File_com.t) position =
     (* Current list of commands *)
     let current_list = rc.Settings_t.progs in
 
@@ -108,11 +108,24 @@ let run ~(rc:File_com.t) position =
     File_com.write updated_rc;
     (* Display the result, only if modified *)
     let new_cmd_mod = gen_modification new_commands in
-    if ( original_command <> new_cmd_mod )
-    then sprintf "'%s' -> '%s'\n" original_command new_cmd_mod |> Messages.ok
-    else Messages.warning "Nothing changed";
+    (* We are doing things in this order to avoid multiple listing of rc file
+     * when reediting. *)
+    if ( original_command = new_cmd_mod )
+    then (* Nothing change, try reediting *)
+      begin
+        let open Messages in
+        warning "Nothing changed.";
+        confirm "Do you want to reedit?"
+        |> function
+          | Yes -> run ~rc position
+          | No -> ()
+      end
 
-    let reread_rc = File_com.init_rc () in
-    (* Display new rc file *)
-    List_rc.run ~rc:reread_rc ()
+    else (* Display summary of changes *)
+      begin
+        sprintf "'%s' -> '%s'\n" original_command new_cmd_mod |> Messages.ok;
+        (* Display new rc file *)
+        let reread_rc = File_com.init_rc () in
+        List_rc.run ~rc:reread_rc ()
+      end;
 ;;

+ 1 - 1
src/messages.ml

@@ -154,7 +154,7 @@ let check_assume_yes ~f =
 let rec confirm info =
   check_assume_yes ~f:(fun () ->
     print ~color:Cyan ~style:Normal info;
-    print ~color:Cyan ~style:Normal "\nAre you sure ? (Yes/No): ";
+    print ~color:Cyan ~style:Normal "\n(Yes/No): ";
     (* XXX Be sure to show the message *)
     Out_channel.(flush stdout);
     let str_answer = In_channel.(input_line ~fix_win_eol:true stdin) in

+ 2 - 1
src/tmp_file.ml

@@ -222,7 +222,8 @@ let reset_all () =
     write Tmp_biniou_t.{ tmp with rc = List.Assoc.add tmp.rc name [] }
   in
   Messages.debug "Asking question";
-  Messages.confirm "You will lose number of launch for every command."
+  Messages.confirm "You will lose number of launch for every command.\
+    Are you sure?"
   |> (fun answer -> sprintf "Answer %s" (Messages.answer2str answer) |> Messages.debug; answer) (* Spy *)
     |> function
       Messages.Yes -> reset_without_ask ()