Browse Source

Adapt the rest of the program to use the new rc format

 + Fixes #5
 + Semantic of some code needs improvement, for instance some variable
 are named command (or cmd) while they are instances of the entry class
 actually. The use of the command method confuse a bit further then.
 + We do not always took profit from the autowrite of rc file on
 instantiation, this may be improved in the future.
 + Keep file_comm to allow to import form the old file format.
Leo 8 years ago
parent
commit
c421a206d7
14 changed files with 81 additions and 56 deletions
  1. 8 7
      src/add_command.ml
  2. 2 2
      src/clean_command.ml
  3. 5 5
      src/command_def.ml
  4. 5 3
      src/default.ml
  5. 14 7
      src/edit_command.ml
  6. 4 7
      src/list_rc.ml
  7. 7 7
      src/remove_command.ml
  8. 3 3
      src/state.ml
  9. 13 4
      src/test/ec_t.ml
  10. 7 2
      src/test/edit_t.ml
  11. 4 0
      src/tools.ml
  12. 1 0
      src/tools.mli
  13. 6 7
      src/unify.ml
  14. 2 2
      src/unify.mli

+ 8 - 7
src/add_command.ml

@@ -54,18 +54,19 @@ let new_list current_list position new_items =
 
 (* Function which add the commands (one per line) ridden on stdin to the rc
  * file, and then display th new configuration *)
-let run ~(rc:File_com.t) position =
+let run ~(rc:Rc.t) position =
   (* Read command from stdin, as a list. fix_win_eol removes \r\n *)
-  let cmd_list = In_channel.input_lines ~fix_win_eol:true In_channel.stdin in
+  let cmd_list =
+    In_channel.input_lines ~fix_win_eol:true In_channel.stdin
+    |> List.map ~f:(fun command -> new Rc.entry command)
+  in
   (* Create an updated rc file *)
   let updated_rc =
-    { rc with
-      Settings_t.progs = (new_list rc.Settings_t.progs position cmd_list)
-    }
+    rc#change_entries (new_list rc#entries position cmd_list)
   in
-  File_com.write updated_rc;
+  updated_rc#write;
   (* Display the result *)
-  let reread_rc = File_com.init_rc () in
+  let reread_rc = Rc.init () in
   List_rc.run ~rc:reread_rc ()
 ;;
 

+ 2 - 2
src/clean_command.ml

@@ -37,7 +37,7 @@
 open Core.Std;;
 
 let run ~rc () =
-  (* Everything is done in background by File_com.write *)
-  File_com.write rc;
+  let rc = Unify.prettify rc in
   Messages.debug "Configuration file cleaned up";
+  rc
 ;;

+ 5 - 5
src/command_def.ml

@@ -42,7 +42,7 @@ open Command;;
 
 (* Type to return result of the work with common arguments *)
 type return_arg = {
-  rc : Settings_t.rc_file Lazy.t;
+  rc : Rc.t Lazy.t;
 }
 
 (* Shorthand *)
@@ -92,7 +92,7 @@ let shared_params =
 
          (* Obtain data from rc_file *)
          d "Reading rc_file...";
-         let rc_content = lazy (File_com.init_rc ()) in
+         let rc_content = lazy (Rc.init ()) in
          d "Read rc_file";
          { rc = rc_content } (* We use type for futur use *)
        )
@@ -199,7 +199,7 @@ let clean =
     )
     (fun { rc } () ->
        let rc = Lazy.force rc in
-       Clean_command.run ~rc ()
+       Clean_command.run ~rc () |> ignore;
     )
 ;;
 
@@ -273,7 +273,7 @@ let edit =
        iter_seq cmd_seq ~f:(fun n ->
               let position =
                 Option.value n
-                  ~default:(List.length (rc.Settings_t.progs) - 1)
+                  ~default:(List.length (rc#entries) - 1)
               in
               Edit_command.run ~rc position)
     )
@@ -339,7 +339,7 @@ let run ~version ~build_info () =
                 http://cecill.info/licences/Licence_CeCILL_V2.1-en.html \
                 (https://s.oclaunch.eu.org/cecill) for details. More here: \
                 https://oclaunch.eu.org/floss-under-cecill (https://s.oclaunch.eu.org/l)."
-      ~readme:(fun () -> File_com.welcome_msg)
+      ~readme:(fun () -> Rc.welcome_msg)
       ~preserve_subcommand_order:()
       [ ("run", default) ; ("licence", licence) ; ("add", add) ; ("edit", edit)
       ; ("list", list) ; ("cleanup", clean) ; ("delete", delete)

+ 5 - 3
src/default.ml

@@ -41,7 +41,7 @@ open Core.Std;;
 
 (* cmd_number is the number of the command the user wants
  * to execute *)
-let run ~rc cmd_number =
+let run ~(rc:Rc.t) cmd_number =
 
   (* Wait for another oclaunch instance which could launch the same program at
    * the same time and then lock *)
@@ -68,9 +68,11 @@ let run ~rc cmd_number =
     end
   | Some num -> begin
       (* Run given (num) item *)
-      File_com.num_cmd2cmd ~rc num
+      rc#entry ~n:num
       |> function
       | None -> Messages.warning "Your number is out of bound"
-      | Some cmd_to_exec -> Exec_cmd.execute cmd_to_exec;
+      | Some entry ->
+          let cmd_to_exec = entry#command in
+          Exec_cmd.execute cmd_to_exec;
     end
 ;;

+ 14 - 7
src/edit_command.ml

@@ -62,6 +62,7 @@ let new_list current_list position new_items =
 let gen_modification items =
   let r = "\n" in
   epur items
+  |> List.map ~f:(fun entry -> entry#command)
   |> (function
        | [] -> ""
        (* Only one element *)
@@ -74,9 +75,9 @@ let gen_modification items =
 
 (* Function which get the nth element, put it in a file, let the user edit it,
  * and then replace with the result *)
-let rec run ~(rc:File_com.t) position =
+let rec run ~(rc:Rc.t) position =
   (* Current list of commands *)
-  let current_list = rc.Settings_t.progs in
+  let current_list = rc#entries in
 
   (* Creating tmp file, for editing *)
   let tmp_filename = [
@@ -104,10 +105,16 @@ let rec run ~(rc:File_com.t) position =
          |> Messages.warning);
 
   (* Reading and applying the result *)
-  let new_commands = In_channel.read_lines tmp_filename |> 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;
+  let new_commands =
+    In_channel.read_lines tmp_filename
+    |> List.map ~f:(fun command -> new Rc.entry command)
+    |> epur
+  in
+  let cmd_list =
+    new_list shorter_list position new_commands
+  in
+  let updated_rc = rc#change_entries cmd_list in
+  updated_rc#write;
   (* Display the result, only if modified *)
   let new_cmd_mod = gen_modification new_commands in
   (* We are doing things in this order to avoid multiple listing of rc file
@@ -127,7 +134,7 @@ let rec run ~(rc:File_com.t) position =
     begin
       sprintf "'%s' -> '%s'\n" original_command new_cmd_mod |> Messages.ok;
       (* Display new rc file *)
-      let reread_rc = File_com.init_rc () in
+      let reread_rc = Rc.init () in
       List_rc.run ~rc:reread_rc ()
     end;
 ;;

+ 4 - 7
src/list_rc.ml

@@ -72,12 +72,9 @@ let truncate ?elength str =
 
 (* Generate list to feed the table, returning list of tuples
  * (number of a command in rc file, command, number of launch). *)
-(* FIXME Remove ?rc or use it *)
-let generate_list ?rc ?elength log =
+let generate_list ~rc ?elength log =
   let rc_numbered =
-    File_com.init_rc ()
-    |> fun rc -> rc.Settings_t.progs
-                 |> List.mapi ~f:(fun i item -> ( item, i ))
+    rc#entries |> List.mapi ~f:(fun i entry -> ( entry#command, i ))
   in
   List.map log ~f:(function ( cmd, number ) ->
          (* We are using list instead of tuple since it is what Text_utils want
@@ -106,10 +103,10 @@ let generate_list ?rc ?elength log =
  * - Test it, esp. ordering
  * - Allow to set form of the table, multiple rc file, display next to be
  * launched… *)
-let run ?rc ?elength () =
+let run ~rc ?elength () =
   let tmp : Tmp_file.t = Tmp_file.init () in
   Tmp_file.get_accurate_log ~tmp ()
-  |> generate_list ?rc ?elength
+  |> generate_list ~rc ?elength
   |> Textutils.Ascii_table.simple_list_table
        ~display:Textutils.Ascii_table.Display.column_titles
        [ "Id" ; "Command" ; "Number of launch" ]

+ 7 - 7
src/remove_command.ml

@@ -41,7 +41,7 @@ open Core.Std;;
 (* Function remove nth command in the rc_file, returning the removed one and the
  * new list *)
 let remove current_list n =
-  let removed = ref "" in
+  let removed = ref (Rc.empty_entry ()) in
   (* The list without the nth item *)
   let new_list = List.filteri current_list ~f:(fun i _ ->
          if i <> n then
@@ -55,22 +55,22 @@ let remove current_list n =
              false
            end
        ) in
-  ( !removed, new_list )
+  ( !removed#command, new_list )
 ;;
 
 (* Perform removal *)
 let perform ~rc new_list =
-  let updated_rc = { rc with Settings_t.progs = new_list } in
-  File_com.write updated_rc;
+  let updated_rc = rc#change_entries new_list in
+  updated_rc#write;
   (* Display the result, after rereading rc *)
-  let reread_rc = File_com.init_rc () in
+  let reread_rc = Rc.init () 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 =
-  let actual_list = rc.Settings_t.progs in
+let run ~(rc:Rc.t) n_to_remove =
+  let actual_list = rc#entries in
   (* Get nth command, default last *)
   let nth =
     Messages.debug "Will remove command number:";

+ 3 - 3
src/state.ml

@@ -39,7 +39,7 @@ open Core.Std;;
 (* Module to display the current state of the program *)
 
 (* Display current number *)
-let print_current ~rc () =
+let print_current ~(rc:Rc.t) () =
   Tmp_file.(init ()
             |> (fun tmp -> get_accurate_log ~tmp ())
             |> Exec_cmd.less_launched_num
@@ -51,9 +51,9 @@ let print_current ~rc () =
             (* XXX Debug *)
             sprintf "Num: %i" num |> Messages.debug;
 
-            File_com.num_cmd2cmd ~rc num
+            rc#entry ~n:num
             |> (function
-                 | Some cmd -> cmd
+                 | Some entry -> entry#command
                  | None -> Messages.warning "Error, should not append, this is a bug";
                    assert false)
             |> (fun ( cmd : string ) ->

+ 13 - 4
src/test/ec_t.ml

@@ -40,20 +40,29 @@ open Core.Std;;
 
 (* Function epur *)
 let epur () =
-  let current = Edit_command.epur [ "qw" ; "" ; "erty" ; "a" ; "" ; "zerty"] in
-  let expected = [ "qw" ; "erty" ; "a" ; "zerty" ] in
+  let current =
+    List.map ~f:Tools.to_entry [ "qw" ; "" ; "erty" ; "a" ; "" ; "zerty"]
+    |> Edit_command.epur
+  in
+  let expected = List.map ~f:Tools.to_entry [ "qw" ; "erty" ; "a" ; "zerty" ] in
   OUnit.assert_equal current expected
 ;;
 
 (* Function gen_modification *)
 let gm1 () =
-  let current = Edit_command.gen_modification [ "qw" ] in
+  let current =
+    List.map ~f:Tools.to_entry  [ "qw" ]
+    |> Edit_command.gen_modification
+  in
   let expected = "qw" in
   OUnit.assert_equal current expected
 ;;
 
 let gm2 () =
-  let current = Edit_command.gen_modification [ "qw" ; "erty" ; "a" ; "zerty"] in
+  let current =
+    List.map ~f:Tools.to_entry [ "qw" ; "erty" ; "a" ; "zerty"]
+    |> Edit_command.gen_modification
+  in
   let expected = "\nqw\nerty\na\nzerty\n" in
   OUnit.assert_equal current expected
 ;;

+ 7 - 2
src/test/edit_t.ml

@@ -56,7 +56,10 @@ let ll_data = [
 ]
 
 let llt_l =
-  List.map ll_data ~f:(fun (t, s, name) -> ( (epur t s), name))
+  List.map ll_data ~f:(fun (t, s, name) -> (
+    let t = List.map ~f:Tools.to_entry t in
+    let s = List.map ~f:Tools.to_entry s in
+    (epur t s), name))
   |> List.map ~f:(fun ( f,name ) -> (name, `Quick, f))
 ;;
 (* =========================================== *)
@@ -124,7 +127,9 @@ let gm_data = [
 ]
 
 let gmt_l =
-  List.map gm_data ~f:(fun (t, s, name) -> ( (gen_mod t s), name))
+  List.map gm_data ~f:(fun (t, s, name) ->
+    let t = List.map ~f:Tools.to_entry t in
+    ( (gen_mod t s), name))
   |> List.map ~f:(fun ( f,name ) -> (name, `Quick, f))
 ;;
 (* =========================================== *)

+ 4 - 0
src/tools.ml

@@ -86,3 +86,7 @@ let spy1_log (log : (string * int) list) =
 let spy1_rc rc =
   failwith "Not implemented"
 ;;
+
+(* It's simpler to define strings (for instance in the test) and convert it to
+ * entry then *)
+let to_entry command = new Rc.entry command;;

+ 1 - 0
src/tools.mli

@@ -44,3 +44,4 @@ val spy1_float : float -> float
 val spy1_list : f:('a -> string)-> 'a list -> 'a list
 val spy1_log : (string * int) list -> (string * int) list
 val spy1_rc : 'a -> 'a
+val to_entry : string -> Rc.entry

+ 6 - 7
src/unify.ml

@@ -62,12 +62,12 @@ let prettify_cmd cmds =
   let without_lr =
     (* Removing line return, and trailing spaces, at the end or
        at the start of a command *)
-    List.filter_map cmds ~f:(fun str ->
-           trim str |> function
+    List.filter_map cmds ~f:(fun entry ->
+           trim entry#command |> function
            | "" ->
              Messages.debug "Trimmed command";
              None
-           | s -> Some s)
+           | s -> Some ((entry#change_command s)))
   in
 
   (* Remove doubled entries *)
@@ -85,10 +85,9 @@ let prettify_cmd cmds =
 (* 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 prettify rc_file =
-  let cmds = rc_file.Settings_v.progs in
+let prettify (rc:Rc.t) =
+  let cmds = rc#entries in
   let unique = prettify_cmd cmds in
-  (* Store the deduplicated list in new rc_file *)
-  {rc_file with Settings_v.progs = unique}
+  rc#change_entries unique
 ;;
 

+ 2 - 2
src/unify.mli

@@ -37,5 +37,5 @@
 open Core.Std;;
 
 val make_uniq : 'a list -> 'a list
-val prettify_cmd : string list -> string list
-val prettify : Settings_t.rc_file -> Settings_t.rc_file
+val prettify_cmd : Rc.entry list -> Rc.entry list
+val prettify : Rc.t -> Rc.t