123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2019 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openLwt.Infixletanimation=[|"|.....|";"|o....|";"|oo...|";"|ooo..|";"|.ooo.|";"|..ooo|";"|...oo|";"|....o|";"|.....|";"|.....|";"|.....|";"|.....|";|]letinit=String.make(String.lengthanimation.(0))' 'letclean=String.make(String.lengthanimation.(0))'\b'letanimation=Array.map(funx->clean^x)animationletnumber_of_frames=Array.lengthanimationletmake_with_animationppf~make~on_retryseed=Format.fprintfppf"%s%!"init;letrecloopnseed=letstart=Mtime_clock.counter()inFormat.fprintfppf"%s%!"animation.(nmodnumber_of_frames);makeseed>>=function|Okv->Lwt.returnv|Errorr->lettime=Mtime_clock.countstartinon_retrytimer>>=funv->loop(n+1)vinloop0seed>|=funresult->Format.fprintfppf"%s%s\n%!"cleaninit;resultletdisplay_progress?(every=1)?(out=Lwt_unix.stdout)~pp_print_stepf=ifevery<=0thenraise(Invalid_argument"display_progress: negative or null repetition period");(* pp_print_step must only write on a single-line with no carriage return *)Lwt_unix.isattyout>>=funis_a_tty->ifnotis_a_ttythenf(fun()->Lwt.return_unit)elseletclear_linefmt=Format.fprintffmt"\027[2K\r"inlet(stream,notifier)=Lwt_stream.create()inletwrapped_notifier()=notifier(Some());Lwt_unix.yield()inletmain_promise=Lwt.finalize(fun()->fwrapped_notifier)(fun()->notifierNone;Lwt.return_unit)inletoc=Unix.out_channel_of_descr(Lwt_unix.unix_file_descrout)inletfmt=Format.formatter_of_out_channelocinletcpt=ref0inletpp_cpt=ref0inletrectick_promise()=Lwt_unix.sleep1.>>=fun()->incrpp_cpt;tick_promise()inletloop=tick_promise()inletdot_array=[|"";".";"..";"..."|]inletdots()=dot_array.(!pp_cptmod4)inletpp()=clear_linefmt;Format.fprintffmt"%a%s%!"pp_print_step!cpt(dots())inletpp_done()=clear_linefmt;Format.fprintffmt"%a Done@\n%!"pp_print_step!cptinpp();incrcpt;letprinter=Lwt_stream.iter_s(fun()->(if!cptmodevery=0then(pp();Lwt.return_unit)elseLwt.return_unit)>>=fun()->incrcpt;Lwt.return_unit)streaminmain_promise>>=fune->Lwt.cancelloop;printer>>=fun()->decrcpt;pp_done();Lwt.returne