Browse Source

Add option to disable color

Update TODOs and CHANGELOG
Leo 10 years ago
parent
commit
8d50687b2d
4 changed files with 32 additions and 18 deletions
  1. 3 1
      CHANGELOG.md
  2. 8 1
      src/command_def.ml
  3. 2 0
      src/const.ml
  4. 19 16
      src/messages.ml

+ 3 - 1
CHANGELOG.md

@@ -6,7 +6,9 @@
  + 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.
+ + Add options:
+    + “-v” to set verbosity.
+    + “--no-color” to toggle color off
  + Correct bugs:
    + When executing ```oclaunc -r``` more than once, it tries to delete an
      unexisting file and this led to errors.

+ 8 - 1
src/command_def.ml

@@ -47,6 +47,10 @@ let args =
         ~aliases:["--verbose" ; "-verbose"]
         ~doc:"[n] Set verbosity level.\
         The higher n is, the most verbose the program is."
+    (* Flag to set colors *)
+    +> flag "--no-color" no_arg
+        ~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)
     ~aliases:["--rc" ; "-rc"]
@@ -88,10 +92,13 @@ let commands =
     ~readme:(fun () -> "See https://gitlab.com/WzukW/oclaunch for help.")
     args
 
-    (fun verbosity rc_file_name reset_tmp list_commands add delete number modify num_cmd () ->
+    (fun verbosity no_color rc_file_name reset_tmp list_commands add delete number modify num_cmd () ->
        (* Level of verbosity *)
        Const.verbosity := verbosity;
+       (* Do not use color *)
+       Const.no_color := no_color;
        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);

+ 2 - 0
src/const.ml

@@ -52,6 +52,8 @@ let editor = match (Sys.getenv "EDITOR") with
 
 (* Level of verbosity, used by Messages module *)
 let verbosity = ref 4;;
+(* Use do not use colors *)
+let no_color = ref false;;
 
 (* Default place to read settings *)
 let rc_file_default = home ^ "/" ^ ".oclaunch_rc.json";;

+ 19 - 16
src/messages.ml

@@ -39,7 +39,6 @@ open Core.Std;;
 (* Modules to manage output messages, with color *)
 
 (* TODO
-    * 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
@@ -61,21 +60,25 @@ type style =
 (* General function to print things *)
 let print ~color ~style message =
     let open Core_extended in
-    (* This code create proper escapement to display text with bold/color... *)
-    color |>
-    (function
-        | Green -> Color_print.color ~color:`Green message
-        | Red -> Color_print.color ~color:`Red message
-        | Yellow -> Color_print.color ~color:`Yellow message
-        | White -> Color_print.color ~color:`White message
-        | Plum -> Color_print.color ~color:`Plum message
-    ) |> (* Finaly print escaped string *)
-    (fun colored_msg ->
-        match style with
-        | Bold -> Color_print.boldprintf "%s" colored_msg
-        | Underline -> Color_print.underlineprintf "%s" colored_msg
-        | Normal -> printf "%s" colored_msg
-    )
+    match !Const.no_color with
+    | true -> printf "%s" message
+    | false -> begin (* Use colors *)
+        (* This code create proper escapement to display text with bold/color... *)
+        color |>
+        (function
+            | Green -> Color_print.color ~color:`Green message
+            | Red -> Color_print.color ~color:`Red message
+            | Yellow -> Color_print.color ~color:`Yellow message
+            | White -> Color_print.color ~color:`White message
+            | Plum -> Color_print.color ~color:`Plum message
+        ) |> (* Finaly print escaped string *)
+        (fun colored_msg ->
+            match style with
+            | Bold -> Color_print.boldprintf "%s" colored_msg
+            | Underline -> Color_print.underlineprintf "%s" colored_msg
+            | Normal -> printf "%s" colored_msg
+        )
+    end
 ;;
 
 (* Behave in a conform way to verbosity