Browse Source

Add option to set verbosity level

 - The higher it is, the most is displayed
 - Remove TODO saying we have to allow to set the way messages are displayed
 - Updated CHANGELOG, TODO list
 - Place useful debugging messages after verbosity set
Leo 10 years ago
parent
commit
43e2a21553
9 changed files with 48 additions and 24 deletions
  1. 1 0
      CHANGELOG.md
  2. 0 3
      TODO.md
  3. 10 2
      src/command_def.ml
  4. 3 0
      src/const.ml
  5. 2 3
      src/default.ml
  6. 1 3
      src/exec_cmd.ml
  7. 29 10
      src/messages.ml
  8. 0 1
      src/oclaunch.ml
  9. 2 2
      src/tmp_file.ml

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@
  + Locking tmp file to prevent launching two times the same item (**not tested
    enough**)
  + Messages displayed with bold, underline and colors.
+ + Add option “-v” to set verbosity.
  + Correct bugs:
    + When executing ```oclaunc -r``` more than once, it tries to delete an
      unexisting file and this led to errors.

+ 0 - 3
TODO.md

@@ -14,11 +14,8 @@
  + Make multiple tmp file really working by using checksum for rc file.
 
 ## Short term
- + Use a dedicated module, for user messages. Allow to print in color and to set
-   verbosity level (maybe also script output, in JSON format)
 
 ### Configuration value
- + Make displaying command before launching configurable
  + Allow to run infinitely or say when it is finish
  + Make tmp file emplacement configurable
  + Use two modes, one for easy launch and another more

+ 10 - 2
src/command_def.ml

@@ -42,6 +42,11 @@ open Core.Std;;
 let args =
     let open Command.Spec in
     (empty
+    (* Flag to set verbosity level *)
+    +> flag "-v" (optional_with_default !Const.verbosity int)
+        ~aliases:["--verbose" ; "-verbose"]
+        ~doc:"[n] Set verbosity level.\
+        The higher n is, the most verbose the program is."
     (* Flag to use different rc file *)
     +> flag "-c" (optional_with_default !Const.rc_file file)
     ~aliases:["--rc" ; "-rc"]
@@ -83,10 +88,13 @@ let commands =
     ~readme:(fun () -> "See https://gitlab.com/WzukW/oclaunch for help.")
     args
 
-    (fun rc_file_name reset_tmp list_commands add delete number modify num_cmd () ->
-        Messages.debug "Parsed command line arguments";
+    (fun verbosity rc_file_name reset_tmp list_commands add delete number modify num_cmd () ->
+       (* Level of verbosity *)
+       Const.verbosity := verbosity;
+       Messages.debug (sprintf "Verbosity set to %i" !Const.verbosity);
        (* 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);
        (* Obtain data from rc_file *)
        let rc_content = File_com.init_rc () in
        (* A default number, corresponding to first item *)

+ 3 - 0
src/const.ml

@@ -50,6 +50,9 @@ let editor = match (Sys.getenv "EDITOR") with
   | None -> failwith "Wrong value for $EDITOR\n"
 ;;
 
+(* Level of verbosity, used by Messages module *)
+let verbosity = ref 4;;
+
 (* Default place to read settings *)
 let rc_file_default = home ^ "/" ^ ".oclaunch_rc.json";;
 (* Current place to read settings, maybe modified from command line argument *)

+ 2 - 3
src/default.ml

@@ -48,9 +48,8 @@ let run ~rc:rc_content cmd_number =
   match cmd_number with
     | None -> begin
         (* Execute each item (one by one) in config file *)
-          let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.Settings_t.progs in
-            (* TODO Use display option in rc file *)
-            Exec_cmd.execute cmd_to_exec;
+        let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.Settings_t.progs in
+        Exec_cmd.execute cmd_to_exec;
       end
     | Some num -> begin
         let cmd_to_exec = Exec_cmd.num_cmd_to_cmd ~cmd_list:rc_content.Settings_t.progs num in

+ 1 - 3
src/exec_cmd.ml

@@ -38,7 +38,6 @@ open Core.Std;;
 
 (* Function allowing to set the title of the current terminal windows
  * XXX Maybe better in some lib *)
-(* TODO Allow to set it in configuration file *)
 let set_title new_title =
     (* Use echo command to set term  title *)
     Sys.command (sprintf "echo -en \"\\033]0;%s\\a\"" new_title)
@@ -54,9 +53,8 @@ let num_cmd_to_cmd ~cmd_list number =
       (* If in range of the list, return the corresponding command else return
        * an empty string after displaying error. *)
       | Some x -> set_title x; x
-      (* TODO Make this printing configurable *)
       | None ->
-          Messages.ok "All has been launched!\n";
+          Messages.ok "All has been launched!";
           Messages.tips "You can reset with '-r'";
           (* Return empty string *)
           ""

+ 29 - 10
src/messages.ml

@@ -39,8 +39,7 @@ open Core.Std;;
 (* Modules to manage output messages, with color *)
 
 (* TODO
-    * allow to set verbosity
-    * allow to toggle colors
+    * allow to toggle colors on or off
     * allow to display bold & underlined messages *)
 
 (* Types corresponding to some colors & style of the Core_extended.Color_print
@@ -79,28 +78,48 @@ let print ~color ~style message =
     )
 ;;
 
+(* Behave in a conform way to verbosity
+ * The higher is the number, the more important the message is, the lower
+ * verbosity value display it *)
+let check_verbosity ~f function_number =
+    match function_number <= !Const.verbosity with
+    true -> (* Display the message *)
+        f ()
+    | false -> ()
+;;
+
 (* Print debugging, information, important... messages *)
 let debug message =
-    let mess = (Time.now()|> Time.to_string) ^ " " ^ message ^ "\n" in
-    print ~color:Plum ~style:Underline mess
+    check_verbosity ~f:(fun () ->
+        let mess = (Time.now()|> Time.to_string) ^ " " ^ message ^ "\n" in
+        print ~color:Plum ~style:Underline mess
+    ) 5
 ;;
 
 let info message =
-    let mess = message ^ "\n" in
-    print ~color:White ~style:Normal mess
+    check_verbosity ~f:(fun () ->
+        let mess = message ^ "\n" in
+        print ~color:White ~style:Normal mess
+    ) 4
 ;;
 
 let warning message =
-    let mess = message ^ "\n" in
-    print ~color:Red ~style:Bold mess
+    check_verbosity ~f:(fun () ->
+        let mess = message ^ "\n" in
+        print ~color:Red ~style:Bold mess
+    ) 1
 ;;
 
 let ok message =
-    let mess = message ^ "\n" in
-    print ~color:Green ~style:Bold mess
+    check_verbosity ~f:(fun () ->
+        let mess = message ^ "\n" in
+        print ~color:Green ~style:Bold mess
+    ) 2
 ;;
 
 let tips message =
+    check_verbosity ~f:(fun () ->
     let mess = message ^ "\n" in
     print ~color:Yellow ~style:Normal mess
+    ) 3
 ;;

+ 0 - 1
src/oclaunch.ml

@@ -46,6 +46,5 @@ let version_number = "0.2.2";;
 let build_info = ( "Build with OCaml version " ^ (Sys.ocaml_version) ^ " on " ^ (Sys.os_type) );;
 
 let () =
-  Messages.debug "Started";
   Command.run ~version:version_number ~build_info:build_info Command_def.commands
 ;;

+ 2 - 2
src/tmp_file.ml

@@ -62,7 +62,7 @@ let rec read () =
     with _ ->
         (* If file is not in the right format, delete it and create a new one.
          * Then, read it *)
-        Messages.ok "Reinitialises tmp file\n"; (* TODO Make it settable *)
+        Messages.ok "Reinitialises tmp file\n";
         Sys.remove name;
         create_tmp_file ();
         read ()
@@ -130,5 +130,5 @@ let reset cmd_num =
             (* Set the number *)
             log ~func:((fun a b -> a) n) ();
             sprintf "Tmp file reseted to %i" n |> Messages.ok
-    | _ -> Messages.warning "Invalid number" (* TODO Make it settable *)
+    | _ -> Messages.warning "Invalid number"
 ;;