Skip to main content

module Progress

rascal-0.28.2

Usage

import util::Progress;

Dependencies

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

function progressBar

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

.Description 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|(2277,340,<52,12>,<60,5>)),function(|lib://rascal/org/rascalmpl/library/util/Progress.rsc|(2619,22,<60,7>,<60,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
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")

function spinner

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

.Description 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|(3317,305,<86,9>,<95,3>))
rascal> while (n := arbInt(100), n != 1) {
>>>>>>> sp("<n>");
>>>>>>> }

\ 18

| 19

/ 10

- 33

\ 99

| 69

/ 86

- 76

\ 2

| 19

/ 44

- 21

\ 15

| 9

/ 13

- 26

\ 4

| 3

/ 30

- 55

\ 11

| 17

/ 49

- 40

\ 96

| 33

/ 73

- 29

\ 15

| 31

/ 12

- 31

\ 61

| 16

/ 26

- 68

\ 98

| 24

/ 84

- 90

\ 62

| 33

/ 51

- 41

\ 8

| 82

/ 40

- 50

\ 17

| 46

/ 37

- 37

\ 37

| 38

/ 17

- 27

\ 18

| 61

/ 41

- 21

\ 10

| 97

/ 11

- 64

\ 70

| 88

/ 12

- 22

\ 5

| 67

/ 19

- 75

\ 77

| 40

/ 61
list[void]: []
void (str) spinner(str prefix = " ", str printEnd = "\r")