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>");
>>>>>>> }

\ 94

| 54

/ 36

- 9

\ 67

| 61

/ 26

- 43

\ 33

| 67

/ 80

- 35

\ 17

| 77

/ 20

- 71

\ 86

| 97

/ 12

- 40

\ 42

| 22

/ 7

- 4

\ 38

| 98

/ 74

- 20

\ 58

| 48

/ 65

- 55

\ 8

| 48

/ 90

- 83

\ 25

| 36

/ 30

- 33

\ 58

| 8

/ 68

- 61

\ 28

| 66

/ 24

- 49

\ 19

| 45

/ 56

- 59

\ 77

| 61

/ 89

- 58

\ 31

| 24

/ 32

- 24

\ 59

| 39

/ 68

- 8

\ 92

| 55

/ 41

- 66

\ 99

| 15

/ 32

- 21

\ 19

| 4

/ 57

- 37

\ 79

| 72

/ 31

- 45

\ 13

| 80

/ 38

- 45

\ 23

| 57

/ 59

- 71

\ 90

| 86

/ 37

- 45

\ 14

| 97

/ 67

- 83

\ 60

| 47

/ 76

- 16

\ 17

| 50

/ 34

- 96

\ 76

| 50

/ 40

- 92

\ 8

| 16

/ 63

- 10

\ 8

| 43

/ 23

- 2

\ 82

| 64

/ 47

- 22

\ 15

| 13

/ 22

- 23

\ 88

| 78

/ 18

- 66

\ 31

| 66

/ 87

- 59

\ 14

| 97

/ 38

- 32

\ 34

| 67

/ 92

- 90

\ 53

| 16

/ 30

- 36

\ 64

| 46

/ 90

- 60

\ 95

| 33

/ 75

- 64

\ 6

| 29

/ 7

- 74

\ 73

| 67
list[void]: []