Parcourir la source

Doubled entries are removed before writing rc file

 + We are calling the prettify function before each write of the rc file.
 + Prettify function is returning an rc file.
 + This fix #12, because now every modification of the rc file are filtered to
 remove these doubled entries.
Leo il y a 9 ans
Parent
commit
980e5e7f7b
2 fichiers modifiés avec 8 ajouts et 5 suppressions
  1. 3 2
      src/file_com.ml
  2. 5 3
      src/unify.ml

+ 3 - 2
src/file_com.ml

@@ -45,8 +45,9 @@ type t = Settings_v.rc_file;;
 let write (rc_file:t) =
         (* Short name *)
         let name = !Const.rc_file in
-        (* Create string to be written *)
-        let data = (Settings_j.string_of_rc_file rc_file
+        (* Create string to be written, after removing duplicated commands (and
+         * newlines) *)
+        let data = (Unify.prettify rc_file |> Settings_j.string_of_rc_file
         |> Yojson.Basic.prettify ~std:true) in
         Out_channel.write_all (Lazy.force name) ~data
 ;;

+ 5 - 3
src/unify.ml

@@ -58,7 +58,7 @@ let make_uniq dubbled_entries =
 (* Removing doubled entries (cmds). We need to remove carriage return before
  * deduplicating, since they don't need to be in rc file, and the first one
  * would be kept during deduplication. *)
-let pretty rc_file =
+let prettify rc_file =
   let cmds = rc_file.Settings_v.progs in
   let without_lr = (* Removing line return, and trailing spaces *)
     List.filter_map cmds ~f:(fun str ->
@@ -69,16 +69,18 @@ let pretty rc_file =
         | s -> Some s)
   in
   let unique = make_uniq without_lr in
+  (* Store the deduplicated list in new rc_file *)
+  let unified_rc = {rc_file with Settings_v.progs = unique} in
   (* If there is the same number of element, each one is present only once. *)
   if List.(length unique = length without_lr) then
     begin
       Messages.debug "No duplicate found";
-      unique
+      unified_rc
     end
   else
     begin
       Messages.debug "Duplicate found, removed";
-      unique
+      unified_rc
     end
 ;;