Parcourir la source

Better display after editing an entry

 - Ignore empty lines
 - Fix #10
 - Update CHANGELOG
Leo il y a 10 ans
Parent
commit
c199dfbd85
2 fichiers modifiés avec 27 ajouts et 8 suppressions
  1. 2 2
      CHANGELOG.md
  2. 25 6
      src/edit_command.ml

+ 2 - 2
CHANGELOG.md

@@ -4,9 +4,9 @@
 
 ### v0.2.2
  + Clean help
+ + Better display after editing an entry. Ignore empty lines and fix #10
  + Use lazyness to speed up and lead to less error
- + Locking tmp file to prevent launching two times the same item (**not tested
-   enough**)
+ + Locking tmp file to prevent launching two times the same item
  + Messages displayed with bold, underline and colors.
  + Add options:
     + “-v” to set verbosity.

+ 25 - 6
src/edit_command.ml

@@ -38,6 +38,11 @@ open Core.Std;;
 
 (* Module to edit command without editing the rc file directly *)
 
+(* Function to remove empty element in a list *)
+let epur =
+     List.filter ~f:(function "" -> false | _ -> true)
+;;
+
 (* Function to create a new list augmented by some commands *)
 (* TODO Factorise this *)
 let new_list current_list position new_items =
@@ -73,6 +78,7 @@ let run ~(rc:File_com.t) position =
 
     (* Edit file *)
     let edit = String.concat [ Lazy.force Const.editor ; " " ; tmp_edit ] in
+    Messages.debug edit;
     Sys.command edit
     |> (function
         0 -> ()
@@ -80,16 +86,29 @@ let run ~(rc:File_com.t) position =
         |> Messages.warning);
 
     (* Reading and applying the result *)
-    let new_commands = In_channel.read_lines tmp_edit in
+    let new_commands = In_channel.read_lines tmp_edit |> epur in
     let cmd_list = new_list shorter_list position new_commands in
     let updated_rc = { rc with Settings_t.progs = cmd_list} in
     File_com.write updated_rc;
+    (* Concat edited item. messages should start as "".
+        * If only one element, return "elt".
+        * If more than one "\nelt1\nelt2\nelt3" 
+        * TODO Test it *)
+    let rec gen_modification items =
+        let r = "\n" in
+        epur items
+        |> (function
+            | [] -> ""
+            (* Only one element *)
+            | element :: [] -> element
+            (* The list as more than two elements *)
+            | _ ->
+                    let msg = String.concat ~sep:r items in
+                    String.concat [ r ; msg ; r ])
+    in
     (* Display the result *)
-    sprintf "'%s' -> '%s'\n\n" original_command
-        (List.fold
-            ~f:(fun accum item -> String.concat [ accum ; item ; "\n" ])
-            ~init:""
-            new_commands)
+    sprintf "'%s' -> '%s'\n" original_command
+        (gen_modification new_commands)
         |> Messages.ok;
     let reread_rc = File_com.init_rc () in
     (* Display new rc file *)