I’m pleased to announce the first release of
Progress, now available on Opam.
Progress is a small library for quickly defining and using progress bars in OCaml programs. It aims to provide the following:
- support for rendering multiple progress bars simultaneously;
- responds dynamically to changes in terminal size;
- allows user-defined progress bar layouts.
Defining your own progress bars
The example animation above uses a pre-provided progress bar layout that should meet many needs (
Progress_unix.counter), but it’s fairly easy to re-define it ourselves using the low-level
let counter filename = let proportion i = Int64.to_float i /. 1_000_000. in let open Progress in Segment.( list [ const filename; Units.bytes of_pp; Progress_unix.stopwatch (); bar ~mode:`ASCII proportion; using proportion (Units.percentage of_pp); ] |> box_winsize ~fallback:80 (* Dynamically scale to window size *) |> periodic 100 (* Re-render once every 100 updates *) |> accumulator Int64.add 0L (* Accumulate progress updates *)) |> make ~init:0L
Segment combinators are similar to those of general-purpose pretty-printing libraries (e.g.
fmt), but are equipped with extra logic for “stateful” segments and segments that can have dynamic width. Together, these make for a convenient way to express common patterns when pretty-printing progress bars. For instance, the stateful segment
periodic seen above can be used to ensure that very frequent updates from a hot-loop do not result in too much time spent re-rendering the output.
The library is not yet feature-complete, but should still be reasonably useful Happy hacking!