Parcourir la source

Add feature : -a option allow to add command to rc file without editing it

 - Shorter call of rc file reader
 - Update TODO, CHANGELOG
Leo il y a 10 ans
Parent
commit
2daa9db57f
6 fichiers modifiés avec 92 ajouts et 7 suppressions
  1. 1 0
      CHANGELOG.md
  2. 1 0
      TODO.md
  3. 68 0
      src/add_command.ml
  4. 13 4
      src/file_com.ml
  5. 8 3
      src/oclaunch.ml
  6. 1 0
      src/tmp_file.ml

+ 1 - 0
CHANGELOG.md

@@ -4,6 +4,7 @@
  + Add new command line option.
    + “-r” can now take a number to start from.
    + “-l” list commands of the configuration file with its number.
+   + “-a” add the command given on stdin to configuration file.
  + Improve some messages.
  + Display run commands in title bar of the windows terminal.
  + New tmp file

+ 1 - 0
TODO.md

@@ -32,6 +32,7 @@
  + Documentation ;-)
  + Handle errors in reading rc file
  + Add build info in binary for 0install
+ + Use color in messages
 
 ## Long term
  + Translate displayed messages.

+ 68 - 0
src/add_command.ml

@@ -0,0 +1,68 @@
+(******************************************************************************)
+(* Copyright © Joly Clément, 2014                                             *)
+(*                                                                            *)
+(*  leowzukw@vmail.me                                                         *)
+(*                                                                            *)
+(*  Ce logiciel est un programme informatique servant à exécuter              *)
+(*  automatiquement des programmes à l'ouverture du terminal.                 *)
+(*                                                                            *)
+(*  Ce logiciel est régi par la licence CeCILL soumise au droit français et   *)
+(*  respectant les principes de diffusion des logiciels libres. Vous pouvez   *)
+(*  utiliser, modifier et/ou redistribuer ce programme sous les conditions    *)
+(*  de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA    *)
+(*  sur le site "http://www.cecill.info".                                     *)
+(*                                                                            *)
+(*  En contrepartie de l'accessibilité au code source et des droits de copie, *)
+(*  de modification et de redistribution accordés par cette licence, il n'est *)
+(*  offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons, *)
+(*  seule une responsabilité restreinte pèse sur l'auteur du programme,  le   *)
+(*  titulaire des droits patrimoniaux et les concédants successifs.           *)
+(*                                                                            *)
+(*  A cet égard  l'attention de l'utilisateur est attirée sur les risques     *)
+(*  associés au chargement,  à l'utilisation,  à la modification et/ou au     *)
+(*  développement et à la reproduction du logiciel par l'utilisateur étant    *)
+(*  donné sa spécificité de logiciel libre, qui peut le rendre complexe à     *)
+(*  manipuler et qui le réserve donc à des développeurs et des professionnels *)
+(*  avertis possédant  des  connaissances  informatiques approfondies.  Les   *)
+(*  utilisateurs sont donc invités à charger  et  tester  l'adéquation  du    *)
+(*  logiciel à leurs besoins dans des conditions permettant d'assurer la      *)
+(*  sécurité de leurs systèmes et ou de leurs données et, plus généralement,  *)
+(*  à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.        *)
+(*                                                                            *)
+(*  Le fait que vous puissiez accéder à cet en-tête signifie que vous avez    *)
+(*  pris connaissance de la licence CeCILL, et que vous en avez accepté les   *)
+(*  termes.                                                                   *)
+(******************************************************************************)
+
+open Core.Std;;
+
+(* Module to add command without editing the rc file directly *)
+
+(* Function to create a new list augmented by some commands *)
+let new_list current_list position new_items =
+    match position with
+    | None -> List.append current_list new_items
+    | Some n -> (* If a number is given, add commands after position n by
+    splitting the list and concatenating all. List.split_n works like this :
+        * #let l1 = [1;2;3;4;5;6] in
+        * # List.split_n l1 2;;
+        * - : int list * int list = ([1; 2], [3; 4; 5; 6]) *)
+    let l_begin,l_end = List.split_n current_list n in
+    List.concat [ l_begin ; new_items ; l_end ]
+;;
+
+
+
+(* 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 =
+    (* 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
+    (* Create an updated rc file *)
+    let updated_rc = { rc with progs = (new_list rc.progs position cmd_list)} in
+    File_com.write updated_rc;
+    (* Display the result *)
+    let reread_rc = File_com.init_rc () in
+    List_rc.run ~rc:reread_rc
+;;
+

+ 13 - 4
src/file_com.ml

@@ -41,6 +41,15 @@ open Core.Std;;
 (* Type of the values *)
 type t = Settings_v.rc_file;;
 
+(* Function to write the rc file *)
+let write (tmp_file:t) =
+        (* Short name *)
+        let name = Const.rc_file in
+        (* Create string to be written *)
+        let data = Settings_j.string_of_rc_file tmp_file in
+        Out_channel.write_all name ~data
+;;
+
 (* Return the configuration file template *)
 let rc_template () =
   Settings_v.create_rc_file ~progs:[] ~settings:[]
@@ -59,10 +68,10 @@ let create_rc_file ~name =
 ;;
 
 (* Function to read the rc file *)
-let rec init_rc ~rc:rc_file =
+let rec init_rc ?(rc=Const.rc_file) () =
   (* Verify that file exist *)
-  match (Sys.file_exists rc_file) with
-    | `No -> create_rc_file ~name:rc_file; init_rc ~rc:rc_file;
+  match (Sys.file_exists rc) with
+    | `No -> create_rc_file ~name:rc; init_rc ~rc ();
     | `Unknown -> failwith "Error reading configuration file";
-    | `Yes -> In_channel.read_all rc_file |> Settings_j.rc_file_of_string
+    | `Yes -> In_channel.read_all rc |> Settings_j.rc_file_of_string
 ;;

+ 8 - 3
src/oclaunch.ml

@@ -46,7 +46,7 @@ let version_number = "0.2.0-dev";;
 let build_info = ( "Build with OCaml version " ^ (Sys.ocaml_version) ^ " on " ^ (Sys.os_type) );;
 
 (* Obtain data from rc file *)
-let rc_content = File_com.init_rc ~rc:Const.rc_file;;
+let rc_content = File_com.init_rc ();;
 
 (* Define commands *)
 let commands =
@@ -65,10 +65,15 @@ let commands =
     +> flag "-l" no_arg
     ~aliases:["-list" ; "--list"]
     ~doc:"Print a list of all command with its number. Useful to launch with number"
+    (* Flag to add a command to rc file, from stdin or directly *)
+    +> flag "-a" no_arg
+    ~aliases:["-add" ; "--add"]
+    ~doc:"Add the command given on stdin to configuration file at a given position. If nothing is given, append it."
     +> anon (maybe ("Command number" %: int)))
-    (fun reset_tmp list_commands num_cmd () ->
-       (* First try to list *)
+    (fun reset_tmp list_commands add num_cmd () ->
+       (* First try to list or add *)
        if list_commands then List_rc.run ~rc:rc_content
+       else if add then Add_command.run ~rc:rc_content num_cmd
        else
        match reset_tmp with
          | true -> (* Reset temp file, if nothing is given, put 0 value *)

+ 1 - 0
src/tmp_file.ml

@@ -45,6 +45,7 @@ let write (tmp_file:t) =
     let name = Const.tmp_file in
     let biniou_tmp = Tmp_biniou_b.string_of_tmp_file tmp_file in
     Out_channel.write_all name ~data:biniou_tmp
+;;
 
 (* XXX Using and keyword because each function can call each other *)
 (* Function to read the tmp file *)