Skip to main content

module util::Progress

rascal-0.34.0

Usage

import util::Progress;

Dependencies

import String;
import IO;
import util::Math;

function progressBar

This progressbar can be used in terminal applications to show the progress of some process in the terminal.

tuple[void(str) report, void() finished] progressBar(int total, str prefix = "Progress:", int length = 50, int limit = total, str fill = "\u2588", str unfill = "-", str printEnd = "\r")

The total number of steps is the only required parameter to be passed in. All other parameters are optional.

  • prefix is the string that is displayed in front of the progress bar (default "").

  • length is the length (number of characters) of the displayed bar (default 50).

  • limit allows for the throtteling of the number of times the progress bar is printed. For instance if the total is 1000 and the limit is set to 100 then the progress bar will be updated every 10 iterations.

  • fill is the character used for the percentage used (default "\u2588").

  • unfill is the character used for the unused part (default "-").

  • printEnd is the character used at the end of the line (default "\r").

    The return is a tuple with 2 functions, the report and the finished function.

  • report(str suffix) needs to be called for every iteration update. The suffix is displayed after the progressbar and can differ per iteration

  • finished() can be called at the end of the iteration to add a new line to the terminal

    It is inspired on the progressbar described here: https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console

Examples

rascal>  import util::Progress;
ok
rascal> int total = 10;
int: 10
rascal> pb = progressBar(total, length = 15, limit = 100);
tuple[void (str) report,void () finished]: <function(|lib://rascal/org/rascalmpl/library/util/Progress.rsc|(2259,340,<48,12>,<56,5>)),function(|lib://rascal/org/rascalmpl/library/util/Progress.rsc|(2601,22,<56,7>,<56,29>))>
rascal> for (i <- [0..total]) {
>>>>>>> pb.report(" : <i+1> of <total>");
>>>>>>> }

Progress: |█--------------| 10% : 1 of 10

Progress: |███------------| 20% : 2 of 10

Progress: |████-----------| 30% : 3 of 10

Progress: |██████---------| 40% : 4 of 10

Progress: |███████--------| 50% : 5 of 10

Progress: |█████████------| 60% : 6 of 10

Progress: |██████████-----| 70% : 7 of 10

Progress: |████████████---| 80% : 8 of 10

Progress: |█████████████--| 90% : 9 of 10

Progress: |███████████████| 100% : 10 of 10
list[void]: []
rascal> pb.finished();
ok

function spinner

Simple spinner to display progress for some terminal process for which the total number of steps is not known.

void (str) spinner(str prefix = " ", str printEnd = "\r")

prefix - Contains the string displayed in front the spinner (default " ").

It returns a function that can be called to make the spinner spin one rotation. This function takes a suffix string parameter that will be displayed behind the spinner

Examples

rascal>  import util::Progress;
ok
rascal> import util::Math;
ok
rascal> sp = spinner();
void (str): function(|lib://rascal/org/rascalmpl/library/util/Progress.rsc|(3270,305,<79,9>,<88,3>))
rascal> while (n := arbInt(100), n != 1) {
>>>>>>> sp("<n>");
>>>>>>> }

\ 88

| 54

/ 8

- 64

\ 5

| 70

/ 62

- 47

\ 59

| 9

/ 35

- 44

\ 57

| 28
list[void]: []