Browse Source

Add function to ask for confirmation

 + Some TODO remains
 + Didn't change licence confirmation, since it's mandatory not to be able to
 avoid it.
Leo 9 years ago
parent
commit
87134a7ea4
2 changed files with 37 additions and 0 deletions
  1. 35 0
      src/messages.ml
  2. 2 0
      src/messages.mli

+ 35 - 0
src/messages.ml

@@ -60,6 +60,7 @@ type color =
     | Yellow
     | Yellow
     | White
     | White
     | Plum
     | Plum
+    | Cyan
 ;;
 ;;
 
 
 type style =
 type style =
@@ -84,6 +85,7 @@ let print ~color ~style message =
             | Yellow -> Color_print.color ~color:`Yellow message
             | Yellow -> Color_print.color ~color:`Yellow message
             | White -> Color_print.color ~color:`White message
             | White -> Color_print.color ~color:`White message
             | Plum -> Color_print.color ~color:`Plum message
             | Plum -> Color_print.color ~color:`Plum message
+            | Cyan -> Color_print.color ~color:`Cyan message
         ) |> (* Finaly print escaped string *)
         ) |> (* Finaly print escaped string *)
         (fun colored_msg ->
         (fun colored_msg ->
             match style with
             match style with
@@ -104,6 +106,7 @@ let check_verbosity ~f function_number =
     | false -> ()
     | false -> ()
 ;;
 ;;
 
 
+
 (* Print debugging, information, important... messages *)
 (* Print debugging, information, important... messages *)
 let debug message =
 let debug message =
     check_verbosity ~f:(fun () ->
     check_verbosity ~f:(fun () ->
@@ -126,6 +129,38 @@ let warning message =
     ) 1
     ) 1
 ;;
 ;;
 
 
+(* Type for the answers *)
+type answer = Yes | No;;
+(* State of the program, if you should always answer yes, no or ask to the user
+ * (default)*)
+(* TODO Put it in Const *)
+let assume_yes = None;;
+(* Allow to assume yes or no like with a --yes option *)
+let check_assume_yes ~f =
+  match assume_yes with
+  | Some true -> Yes (* --yes *)
+  | Some false -> No (* --no *)
+  | None -> f ()
+;;
+
+(* Get confirmation
+ * TODO:
+   * allow option like -y
+   * test it *)
+let rec confirm info =
+  check_assume_yes ~f:(fun () ->
+    print ~color:Cyan ~style:Normal info;
+    print ~color:Cyan ~style:Normal "Are you sure ? (Yes/No): ";
+    In_channel.(input_line ~fix_win_eol:true stdin)
+    |> (function
+      | Some "Y" | Some "y" | Some "Yes" | Some "YES" | Some "yes" -> Yes
+      | Some "N" | Some "n" | Some "No" | Some "NO" | Some "no" -> No
+      | Some _ | None ->
+        warning "Please enter 'yes' or 'no' or 'y' or 'n'.";
+        confirm info)
+    )
+;;
+
 let ok message =
 let ok message =
     check_verbosity ~f:(fun () ->
     check_verbosity ~f:(fun () ->
         let mess = message ^ "\n" in
         let mess = message ^ "\n" in

+ 2 - 0
src/messages.mli

@@ -39,5 +39,7 @@ val info : string -> unit
 val warning : string -> unit
 val warning : string -> unit
 val ok : string -> unit
 val ok : string -> unit
 val tips : string -> unit
 val tips : string -> unit
+type answer = Yes | No
+val confirm : string -> answer
 
 
 val reset : unit -> unit
 val reset : unit -> unit