10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-09 07:33:53 +01:00
quantum_package/ocaml/Progress_bar.ml

114 lines
2.6 KiB
OCaml
Raw Normal View History

2017-08-18 18:28:33 +02:00
open Core
2016-02-19 00:20:28 +01:00
type t =
{
title: string;
start_value: float;
cur_value : float;
end_value : float;
bar_length : int;
init_time : Time.t;
dirty : bool;
next : Time.t;
}
let init ?(bar_length=20) ?(start_value=0.) ?(end_value=1.) ~title =
{ title ; start_value ; end_value ; bar_length ; cur_value=start_value ;
2016-10-18 19:29:50 +02:00
init_time= Time.now () ; dirty = false ; next = Time.now () }
2016-02-19 00:20:28 +01:00
let update ~cur_value bar =
{ bar with cur_value ; dirty=true }
let increment_end bar =
2016-10-18 19:29:50 +02:00
{ bar with end_value=(bar.end_value +. 1.) ; dirty=false }
2016-02-19 00:20:28 +01:00
2017-05-02 16:18:02 +02:00
let clear bar =
Printf.eprintf " \r%!";
None
2016-02-19 00:20:28 +01:00
let increment_cur bar =
{ bar with cur_value=(bar.cur_value +. 1.) ; dirty=true }
let display_tty bar =
let percent =
100. *. (bar.cur_value -. bar.start_value) /.
(bar.end_value -. bar.start_value)
in
let n_hashes =
(Float.of_int bar.bar_length) *. percent /. 100.
|> Float.to_int
in
let hashes =
String.init bar.bar_length ~f:(fun i ->
if (i < n_hashes) then '#'
else ' '
)
in
let now =
Time.now ()
in
let running_time =
Time.abs_diff now bar.init_time
in
let stop_time =
let x =
2017-08-18 18:28:33 +02:00
Time.Span.to_sec running_time
2016-02-19 00:20:28 +01:00
in
if (percent > 0.) then
x *. 100. /. percent -. x
2017-08-18 18:28:33 +02:00
|> Time.Span.of_sec
2016-02-19 00:20:28 +01:00
else
2017-08-18 18:28:33 +02:00
Time.Span.of_sec 0.
2016-02-19 00:20:28 +01:00
in
2017-04-24 18:20:45 +02:00
Printf.eprintf "%s : [%s] %4.1f%% | %10s, ~%10s left\r%!"
2016-02-19 00:20:28 +01:00
bar.title
hashes
percent
(Time.Span.to_string running_time)
(stop_time |> Time.Span.to_string );
2017-08-18 18:28:33 +02:00
{ bar with dirty = false ; next = Time.add now (Time.Span.of_sec 0.1) }
2016-02-19 00:20:28 +01:00
let display_file bar =
let percent =
100. *. (bar.cur_value -. bar.start_value) /.
(bar.end_value -. bar.start_value)
in
let running_time =
Time.abs_diff (Time.now ()) bar.init_time
in
let stop_time =
let x =
2017-08-18 18:28:33 +02:00
Time.Span.to_sec running_time
2016-02-19 00:20:28 +01:00
in
if (percent > 0.) then
x *. 100. /. percent -. x
2017-08-18 18:28:33 +02:00
|> Time.Span.of_sec
2016-02-19 00:20:28 +01:00
else
2017-08-18 18:28:33 +02:00
Time.Span.of_sec 0.
2016-02-19 00:20:28 +01:00
in
2017-04-24 18:20:45 +02:00
Printf.eprintf "%5.2f %% in %20s, ~%20s left\n%!"
2016-02-19 00:20:28 +01:00
percent
(Time.Span.to_string running_time)
(Time.Span.to_string stop_time);
2017-08-18 18:28:33 +02:00
{ bar with dirty = false ; next = Time.add (Time.now ()) (Time.Span.of_sec 2.) }
2016-02-19 00:20:28 +01:00
let display bar =
if (not bar.dirty) then
bar
else if (Time.now () < bar.next) then
bar
else
begin
if (Unix.isatty Unix.stdout) then
display_tty bar
else
display_file bar
end