File: progress.ml

package info (click to toggle)
ocaml-luv 0.5.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,504 kB
  • sloc: ml: 11,130; makefile: 6,223; sh: 4,592; ansic: 1,517; python: 38
file content (27 lines) | stat: -rw-r--r-- 658 bytes parent folder | download | duplicates (2)
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
let () =
  let progress = ref 0. in
  let show_progress () =
    Printf.printf "%i%%\n%!" (int_of_float (!progress *. 100.)) in

  let notification =
    Luv.Async.init (fun _ -> show_progress ()) |> Result.get_ok in

  let rec do_work total n =
    if n >= total then
      ()
    else begin
      Luv.Time.sleep 1000;
      progress := float_of_int (n + 1) /. float_of_int total;
      ignore (Luv.Async.send notification);
      do_work total (n + 1)
    end
  in

  let finished _ =
    Luv.Handle.close notification ignore;
    print_endline "Done"
  in

  Luv.Thread_pool.queue_work (fun () -> do_work 3 0) finished;

  ignore (Luv.Loop.run () : bool)