textProgressBar
Loading...
Searching...
No Matches
textProgressBar.cc
Go to the documentation of this file.
1// textProgressBar
2#include "textProgressBar.h"
3
4// c++
5#include <iostream>
6using std::cout, std::endl;
7
9 // See header for API docs.
10
11 // step too small
12 if (p < indexStep) return;
13
14 // step too small
15 if (singleStep == 0) return;
16
17 // outside boundaries
18 if (p < min || p > max) return;
19
20 // progress is between 1 and 100
21 // Note: progress is computed from the current threshold (indexStep) and step size (singleStep),
22 // not directly from p, because printing is throttled to discrete internal steps.
23 double progress = indexStep / singleStep;
24
25 // progress outside boundaries
26 if (progress > TEXTPROGRESSBARNSTEPS) return;
27
28 // adding the single step
29 indexStep += singleStep;
30
31 // printing header of the bar
32 cout << title << startBarChar;
33
34 // pos is relative position within the bar
35 int pos = barWidth * (progress / TEXTPROGRESSBARNSTEPS);
36
37 // print bar, and advance char, empty space at the right places
38 for (int i = 0; i < barWidth; ++i) {
39 if (i < pos) cout << middleBarChar;
40 else if (i == pos) cout << advanceBarChar;
41 else cout << " ";
42 }
43 // print end bar and progress percentage
44 cout << endBarChar << " " << progress << " %\r";
45
46 // carriage return if needed
47 if (progress == TEXTPROGRESSBARNSTEPS) cout << endl;
48
49 cout.flush();
50}
void setProgress(int p)
Updates the progress bar based on the provided progress value.
#define TEXTPROGRESSBARNSTEPS
Number of discrete progress bar steps used for throttled printing.