Parcourir la source

Use more laziness in constants to limit errors.

Related to #3

It should limit error displayed when it is not necessary.
For example, when using command to list items, we don't need editor and that
shouldn't print an error message. However, when using a command to modify an entry, we
need an editor and we display the error message.
Leo il y a 10 ans
Parent
commit
c86f127281
6 fichiers modifiés avec 67 ajouts et 13 suppressions
  1. 3 0
      CHANGELOG.md
  2. 3 3
      src/command_def.ml
  3. 4 3
      src/const.ml
  4. 49 0
      src/const.mli
  5. 2 2
      src/edit_command.ml
  6. 6 5
      src/file_com.ml

+ 3 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## 0.2.x
 
 ### v0.2.2
+ + Use lazyness to speed up and lead to less error
  + Locking tmp file to prevent launching two times the same item (**not tested
    enough**)
  + Messages displayed with bold, underline and colors.
@@ -12,6 +13,8 @@
  + Correct bugs:
    + When executing ```oclaunc -r``` more than once, it tries to delete an
      unexisting file and this led to errors.
+   + When some variable like $EDITOR was not set, it was crashing everytime.
+     Now, it crashes only if there is no other solution.
 
 ### v0.2.1
  + Add new command line option:

+ 3 - 3
src/command_def.ml

@@ -52,7 +52,7 @@ let args =
         ~aliases:["-no-color"]
         ~doc:"Use this flag to disable color usage."
     (* Flag to use different rc file *)
-    +> flag "-c" (optional_with_default !Const.rc_file file)
+    +> flag "-c" (optional_with_default (Lazy.force !Const.rc_file) file)
     ~aliases:["--rc" ; "-rc"]
     ~doc:"file Read configuration from the given file and continue parsing."
     (* Flag to reset tmp file *)
@@ -100,8 +100,8 @@ let commands =
        Messages.debug (sprintf "Verbosity set to %i" !Const.verbosity);
        Messages.debug (sprintf "Color %s" (match !Const.no_color with true -> "off" | false -> "on"));
        (* Use given rc file, should run the nth argument if present *)
-       Const.rc_file := rc_file_name;
-       Messages.debug (sprintf "Configuration file is %s" !Const.rc_file);
+       Const.rc_file := (Lazy.return rc_file_name);
+       Messages.debug (sprintf "Configuration file is %s" (Lazy.force !Const.rc_file));
        (* Obtain data from rc_file *)
        let rc_content = File_com.init_rc () in
        (* A default number, corresponding to first item *)

+ 4 - 3
src/const.ml

@@ -54,12 +54,12 @@ let get_var ?(exp=false) name =
 
 (* Get current home *)
 let home =
-    get_var ~exp:true "HOME"
+    lazy (get_var ~exp:true "HOME")
 ;;
 
 (* Get default editor *)
 let editor = (* If editor is not set, it gets "" *)
-    get_var "EDITOR"
+    lazy (get_var "EDITOR")
 ;;
 
 (* Level of verbosity, used by Messages module *)
@@ -68,7 +68,8 @@ let verbosity = ref 4;;
 let no_color = ref false;;
 
 (* Default place to read settings *)
-let rc_file_default = home ^ "/" ^ ".oclaunch_rc.json";;
+let rc_file_default = Lazy.(home >>| (fun home -> home ^ "/" ^
+    ".oclaunch_rc.json"));;
 (* Current place to read settings, maybe modified from command line argument *)
 let rc_file = ref rc_file_default;;
 (* Set tmp file, in witch stock launches, in biniou format *)

+ 49 - 0
src/const.mli

@@ -0,0 +1,49 @@
+(******************************************************************************)
+(* Copyright © Joly Clément, 2014-2015                                        *)
+(*                                                                            *)
+(*  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.                                                                   *)
+(******************************************************************************)
+
+(* Env variables *)
+val home : string lazy_t
+val editor : string lazy_t
+
+(* Settings *)
+val verbosity : int ref
+val no_color : bool ref
+
+(* Files *)
+val rc_file_default : string Core.Std.Lazy.t
+val rc_file : string Core.Std.Lazy.t ref
+val tmp_file : string
+

+ 2 - 2
src/edit_command.ml

@@ -52,7 +52,7 @@ let new_list current_list position new_items =
 
 
 
-(* Function which get the nth element, put it in afile, let the user edit it,
+(* 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 =
     (* Current list of commands *)
@@ -72,7 +72,7 @@ let run ~(rc:File_com.t) position =
 
 
     (* Edit file *)
-    let edit = String.concat [ Const.editor ; " " ; tmp_edit ] in
+    let edit = String.concat [ Lazy.force Const.editor ; " " ; tmp_edit ] in
     Sys.command edit
     |> (function
         0 -> ()

+ 6 - 5
src/file_com.ml

@@ -48,7 +48,7 @@ let write (tmp_file:t) =
         (* Create string to be written *)
         let data = (Settings_j.string_of_rc_file tmp_file
         |> Yojson.Basic.prettify ~std:true) in
-        Out_channel.write_all name ~data
+        Out_channel.write_all (Lazy.force name) ~data
 ;;
 
 (* Return the configuration file template *)
@@ -71,14 +71,15 @@ let create_rc_file ~name =
 
 (* Function to read the rc file *)
 let rec init_rc ?(rc=(!Const.rc_file)) () =
+   let rc' = Lazy.force rc in
   (* Verify that file exist *)
-  match (Sys.file_exists rc) with
-    | `No -> create_rc_file ~name:rc; init_rc ~rc ();
+  match (Sys.file_exists rc') with
+    | `No -> create_rc_file ~name:rc'; init_rc ~rc ();
     | `Unknown -> failwith "Error reading configuration file";
     | `Yes -> (* Try to read, if there is an error, reset file *)
             try
-                In_channel.read_all rc |> Settings_j.rc_file_of_string
+                In_channel.read_all rc' |> Settings_j.rc_file_of_string
             with
             | Yojson.Json_error _ -> (* Invalid file, delete, so that it will be reseted
-            on next call *) Sys.remove rc; init_rc ~rc ()
+            on next call *) Sys.remove rc'; init_rc ~rc ()
 ;;