123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423(*————————————————————————————————————————————————————————————————————————————
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
Distributed under the MIT license. See terms at the end of this file.
————————————————————————————————————————————————————————————————————————————*)open!Import(** These values are documented as part of {!DSL}.*)moduletypeInteger_dependent=sigtypeintegertypecolortypedurationtype'aprintertypebar_style(**)type'atvalsum:?pp:integerprinter->width:int->unit->integertvalcount_to:?pp:integerprinter->?sep:unitt->integer->integertvalbytes:integertvalbytes_per_sec:integertvalpercentage_of:integer->integertvalrate:floatprinter->integertvaleta:?pp:durationprinter->integer->integerttypebar_style:=[`ASCII|`UTF8|`Customofbar_style]valbar:?style:bar_style->?color:color->?width:[`Fixedofint|`Expand]->?data:[`Sum|`Latest]->integer->integertend(** The 'main' set of combinators, specialised to a particular integer type. *)moduletypeDSL=sigtypeintegertypecolortypedurationtype'aprintertype'at(** The type of progress lines for reported values of type ['a]. This module
provides a selection of {{!basic} individual line segments} that can be
{{!combinators} combined} to produce more interesting layouts. You may
wish to look over the {{!examples} examples} for inspiration. *)(** {1:basic Basic line segments} *)valconst:string->_t(** [const s] is the segment that always displays [s]. *)valconstf:('a,Format.formatter,unit,_t)format4->'a(** Like {!const}, but takes a format string and corresponding arguments.
[constf "..." a b c ...] is equivalent to
[const (Format.asprintf "..." a b c ...)], except that colours added with
[Fmt.styled] are not discarded. *)valstring:stringt(** A line segment that displays a dynamically-sized string message. Use
{!lpad} and {!rpad} to pad the message up to a given length. *)vallpad:int->'at->'at(** [lpad n t] left-pads the segment [t] to size [n] by adding blank space at
the start. *)valrpad:int->'at->'at(** [rpad n t] right-pads the segment [t] to size [n] by adding blank space at
the end. *)valof_printer:?init:'a->'aprinter->'at(** [of_printer p] is a segment that renders the {i latest} reported value
using printer [p]. See {!sum} for a variant that reports accumulated
values instead. *)(** {2:counting Counting segments}
These segments all consume integer values and display the accumulated
total of reported values in some way. The top-level [Line] segments are
specialised to [int] values; see "{!integers}" for variants supporting
[int32], [int64] etc. *)valcount_to:?pp:integerprinter->?sep:unitt->integer->integert(** [count_to target] renders both the current running total of reported
values and the fixed value [target], separated by the the given separator,
i.e. [42/100]. [sep] defaults to [const "/"]. *)valbytes:integert(** Prints the running total as a number of bytes, using ISO/IEC binary
prefixes (e.g. [10.4 MiB]). See also {!bytes_per_sec}. *)valpercentage_of:integer->integert(** [percentage_of target] renders the running total as a percentage of
[target], i.e. [42%]. Values outside the range [\[0, 100\]] will be
clamped to either [0] or [100]. *)valsum:?pp:integerprinter->width:int->unit->integert(** [sum ~width ()] displays a running total of reported values using
[width]-many terminal columns. If passed, [pp] overrides the printer used
for rendering the count. *)(** {2:graphical Graphical segments} *)moduleBar_style:sigtypet(** The type of progress bar style specifications. *)valascii:t(** The style used by [bar ~style:`ASCII] (which is the default). Generates
bars of the form [\[######---\]]. *)valutf8:t(** {!utf8} is the style used by [bar ~style:`UTF8]. Uses the UTF-8 block
element characters ([U+2588]–[U+258F]) for progress stages, and a
box-drawing character ([U+2502]) for delimiters. *)(** {1 Custom styles} *)valv:?delims:string*string->?color:color->?color_empty:color->stringlist->t(** [v stages] is a bar that uses the given string {i stages} to render
progress. The first stage is interpreted as a "full" segment, with
subsequent stages denoting progressively {i less}-full segments until a
final "empty" stage (which is implicitly a space if only one stage is
provided).
The optional parameters are as follows:
- [?delims]: a pair of left and right delimiters used to wrap the body
of the progress bar;
- [?color]: the color of non-empty segments (including the in-progress
one);
- [?color_empty]: the color of empty segments.
{2 Examples}
- [v \[ "#" \]] renders like "[####### ]";
- [v \[ "="; ">"; " " \]] renders like "[======> ]";
- [v \[ "4"; "3"; "2"; "1"; "0" \]] renders like "[444444410000]";
- ... see [examples/bar_styles.ml] in the source repository for more.
{2 Specifics}
Each segment of a rendering progress bar is in one of three states:
full, empty or in-progress. At any given time, either the bar is
entirely full or or there is exactly one in-progress segment. Given the
style [v \[s1; s2; ... sN\]], these states are rendered as follows:
- {b full}: rendered as [s1];
- {b empty}: rendered as [sN] if [N >= 1], otherwise [' '];
- {b in-progress}: if [N <= 1], then equivalent to the empty state.
Otherwise, the intermediate stages [s2], [s3], ... [s{N-1}] denote
decreasing progress. For example, if there are four intermediate
stages ([N = 6]) then [s2] is used for progress in the range
[\[0, 25%)], [s3] for [\[25%, 50%)] etc.
For the progress bar to render within a fixed size, the user must ensure
that each of the [stages] must have the same rendered width. *)(** {1 Setters} *)valwith_color:color->t->tvalwith_empty_color:color->t->tvalwith_delims:(string*string)option->t->tvalwith_stages:stringlist->t->tendvalbar:?style:[`ASCII|`UTF8|`CustomofBar_style.t]->?color:color->?width:[`Fixedofint|`Expand]->?data:[`Sum|`Latest]->integer->integert(** [bar total] is a progress bar of the form:
[\[#################...............\]]
The proportion of the bar that is filled is given by
[<reported_so_far> / total]. Optional parameters are as follows:
- [?style] specifies whether to use a UTF-8 or an ASCII encoding for the
progress bar. The UTF-8 encoding shows a higher resolution of progress,
but may not be supported in all terminals. The default is [`ASCII].
- [?color] causes the filled portion of the bar to be rendered with the
given colour. (Equivalent to setting the colour with
{!Bar_style.with_color}.)
- [?width] is the width of the bar in columns. Defaults to [`Expand],
which causes the bar to occupy the remaining rendering space after
accounting for other line segments on the same line.
- [?data] changes the metric that is indicated by the progress bar. [`Sum]
(the default) causes the progress bar to correspond to the
{i running total} of values reported so far. [`Latest] causes each
reported value to overwrite the previous one instead. *)valspinner:?frames:stringlist->?color:color->?min_interval:durationoption->unit->_t(** [spinner ()] is a small segment that cycles over a fixed number of frames
each time a value is reported. e.g.
{[
⠋ → ⠙ → ⠹ → ⠸ → ⠼ → ⠴ → ⠦ → ⠧ → ⠇ → ⠏ → ...
]}
Optional prameters are as follows:
- [?frames] alters the sequence of frames rendered by the spinner;
- [?color] causes each frame to be rendered with the given colour;
- [?min_interval] is the minimum time interval between frame transitions
of the spinner (i.e. a debounce threshold). The default is [Some 80ms]. *)(** {2:time Time-sensitive segments} *)valbytes_per_sec:integert(** [bytes_per_sec] renders the rate of change of the running total as a
number of bytes per second, using ISO/IEC binary prefixes (e.g.
[10.4 MiB/s]). *)valelapsed:?pp:durationprinter->unit->_t(** Displays the time for which the bar has been rendering in [MM:SS] form. *)valeta:?pp:durationprinter->integer->integert(** Displays an estimate of the remaining time until [total] is accumulated by
the reporters, in [MM:SS] form. *)valrate:floatprinter->integert(** [rate pp] is an integer segment that uses [pp] to print the {i rate} of
reported values per second. (For instance, [bytes_per_sec] is
[rate Units.Bytes.of_float].)*)(** {1:combinators Combining segments} *)val(++):'at->'at->'at(** Horizontally join two segments of the same reported value type. *)vallist:?sep:'at->'atlist->'at(** Horizontally join a list of segments, with a given separator. [sep]
defaults to [const " "]. *)valpair:?sep:unitt->'at->'bt->('a*'b)t(** Horizontally join a pair of segments consuming different reported values
into a single segment that consumes a pair. *)valusing:('a->'b)->'bt->'at(** [using f s] is a segment that first applies [f] to the reported value and
then behaves as segment [s]. *)(** {1 Utilities}
The following line segments are definable in terms of the others, but
provided for convenience: *)valparens:'at->'at(** [parens t] is [const "(" ++ t ++ const ")"]. *)valbrackets:'at->'at(** [brackets t] is [const "\[" ++ t ++ const "\]"]. *)valbraces:'at->'at(** [braces t] is [const "{" ++ t ++ const "}"]. *)valnoop:unit->_t(** A zero-width line segment that does nothing. This segment will not be
surrounded with separators when used in a {!list}, making it a useful
"off" state for conditionally-enabled segments. *)valspacer:int->_t(** [spacer n] is [const (String.make n ' ')]. *)valticker_to:?sep:unitt->integer->_t(** [ticker_to total] is [using ~f:(fun _ -> 1) (counter_to total)]. i.e. it
renders the total {i number} of reported values of some arbitrary type. *)endmoduleAssert_subtype(X:DSL):Integer_dependentwithtypebar_style:=X.Bar_style.t=XmoduletypeS=sigincludeDSLwithtypeinteger:=int(** @inline *)(** {1:integers Alternative integer types}
Many of the line segments above are specialised to [int] values for
simplicity (and performance), but certain use-cases may require different
types (e.g. for file transfers greater than [2 GiB] on 32-bit platforms).
The following modules re-export the [Line] DSL with different integer
specialisations, and are intended to be opened locally, e.g.
{[
let my_line =
let open Progress.Line.Using_int64 in
list [ const "Downloading large file"; bar total; bytes ]
]} *)moduleInteger_dependent:sig(** {!S} contains just the line segments that can be specialised to an
underlying integer implementation. *)moduletypeS=Integer_dependentwithtype'at:='atandtypecolor:=colorandtypeduration:=durationandtype'aprinter:='aprinterandtypebar_style:=Bar_style.tmoduleMake(Integer:Integer.S):Swithtypeinteger:=Integer.t(** {!Ext} is {!S} extended with non-integer-dependent segments as well. *)moduletypeExt=DSLwithtype'at:='atandtypecolor:=colorandtypeduration:=durationandtype'aprinter:='aprinterandtypeBar_style.t:=Bar_style.tendmoduleUsing_int32:Integer_dependent.Extwithtypeinteger:=int32moduleUsing_int63:Integer_dependent.Extwithtypeinteger:=int63moduleUsing_int64:Integer_dependent.Extwithtypeinteger:=int64moduleUsing_float:Integer_dependent.Extwithtypeinteger:=float(** {1:examples Examples}
{[
(* Renders: "[######---------------------------------------] 14/100" *)
bar 100 ++ const " " ++ count_to 100
]}
{[
(* Renders: "⠏ [01:04] [####-----------------] 293.9 MiB (eta: 07:12)" *)
list
[ spinner ()
; brackets (elapsed ())
; bar total
; bytes
; parens (const "eta: " ++ eta total)
]
]}
{[
(* Renders: " a.txt │██████████████████████████▋ │ 91.4 MiB/s 92%" *)
list
[ lpad 7 (const file_name)
; bar ~style:`UTF8 total
; bytes_per_sec
; percentage_of total
]
]}
See the [examples/] directory of the source repository for more. *)(** {1 Library internals} *)moduleInternals:sig(** Exposes the underlying implementation of line segments for testing. This
API is unstable, unsafe and mostly undocumented; here be dragons etc. *)type'alinemoduleLine_buffer=Line_bufferincludeLine_primitives.Swithtype'at='aLine_primitives.t(** @inline *)valbox_winsize:?max:int->?fallback:int->'at->'at(** A box that takes on the current size of the terminal (or [fallback] if
stdout is not attached to a terminal).
@param fallback defaults to [80].
@param max defaults to no limit. *)valto_line:'at->'alineendwithtype'aline:='atendmoduletypeLine=sigmoduletypeS=Stype'atmoduleMake(_:Platform.S):sigincludeSwithtype'at:='atandtypecolor:=Terminal.Color.tandtypeduration:=Duration.tandtype'aprinter:='aPrinter.tvalto_primitive:Config.t->'at->'aInternals.tendend(*————————————————————————————————————————————————————————————————————————————
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
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.
————————————————————————————————————————————————————————————————————————————*)