Testing the ANSITerminal library and trying to build a progress bar, I am trying to slow down a bit the progression of the bar in order to emulate an heavy process with the sleep command. It does not work at all. Compilation is ok but launching the executable results in nothing happening. Without the sleep command my test runs though, but way too fast (I can see the progress bar at the end). Is the sleep function of the Unix module available on windows setup ?
I have ocaml version 4.14.0 on windows 10
Any help/hint would be welcome
open ANSITerminal
let rec bar n =
if n < 1 then ""
else "\u{258A}" ^ (bar (n-1))
let progress n percent =
let taille = int_of_float (percent *. (float_of_int n)) in
bar taille
let () =
for i = 1 to 100 do
ANSITerminal.erase Above;
ANSITerminal.printf [yellow] "%s" (progress 50 ((float_of_int i) /. 100.0));
Unix.sleep 1
done
It sleeps for 10 seconds and print “Slept for 10s” …If I copy and paste it within my project. Though in utop it says "Reference to undefined global ‘Unix’
The sleep function works well if I remove the erase Above part. I suspect an issue with flushing not at the right time but I am also surprised by the speed of the program (it ends immediatly) without outputing anything whereas I was at least expecting 100 seconds before ending.
The code is segfaulting in ANSITerminal.erase (which is why it exits immediately). A quick glance at the source of ANSITerminal revealed the problem, a NULL pointer was being passed to the system call GetConsoleScreenBufferInfo. The following diff should be enough to fix the problem: