package commons

  1. Overview
  2. Docs

Source file Console.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(* used to be called common_extra.ml *)

(*
 * How to use it ? ex in LFS:
 *  Console.progress (w.prop_iprop#length) (fun k ->
 *  w.prop_iprop#iter (fun (p, ip) ->
 *     k ();
 *     ...
 *  ));
 *
 * todo: Unix.isatty, and the spinner trick of jason \ | / -
 *)

let execute_and_show_progress ~show len f =
  let _count = ref 0 in
  (* kind of continuation passed to f *)
  let continue_pourcentage () =
    incr _count;
    ANSITerminal.set_cursor 1 (-1);
    ANSITerminal.printf [] "%d / %d" !_count len;
    flush stdout
  in
  let nothing () = () in

  (* ANSITerminal.printf [] "0 / %d" len; flush stdout; *)
  if !Common2._batch_mode || not show then f nothing else f continue_pourcentage;
  Common.pr2 ""

let execute_and_show_progress2 ?(show = true) len f =
  let _count = ref 0 in
  (* kind of continuation passed to f *)
  let continue_pourcentage () =
    incr _count;
    ANSITerminal.set_cursor 1 (-1);
    ANSITerminal.eprintf [] "%d / %d" !_count len;
    flush stderr
  in
  let nothing () = () in

  (* ANSITerminal.printf [] "0 / %d" len; flush stdout; *)
  if !Common2._batch_mode || not show then f nothing else f continue_pourcentage

let with_progress_list_metter ?show fk xs =
  let len = List.length xs in
  execute_and_show_progress2 ?show len (fun k -> fk k xs)

let progress ?show fk xs = with_progress_list_metter ?show fk xs

(*
(* old code
let ansi_terminal = ref true *)

let (_execute_and_show_progress_func:
   (show:bool ->
    int (* length *) -> ((unit -> unit) -> 'a) -> 'a) ref)
 = ref
  (fun ~show a b ->
    failwith "no execute  yet, have you included common_extra.cmo?"
  )

let execute_and_show_progress ?(show=true) len f =
    !_execute_and_show_progress_func ~show len f

(* don't forget to call Common_extra.set_link () *)

val _execute_and_show_progress_func :
  (show:bool -> int (* length *) -> ((unit -> unit) -> unit) -> unit)
  ref
val execute_and_show_progress :
 ?show:bool -> int (* length *) -> ((unit -> unit) -> unit) -> unit

let set_link () =
  Common2._execute_and_show_progress_func := execute_and_show_progress


let _init_execute =
  set_link ()


*)

(* now in common_extra.ml:
 * let execute_and_show_progress len f = ...
 *)
OCaml

Innovation. Community. Security.