1 | #include <iostream> |
---|
2 | #include <string> |
---|
3 | #include <Utils/feedback.hh> |
---|
4 | |
---|
5 | |
---|
6 | ProgressBar::ProgressBar() |
---|
7 | { |
---|
8 | /** |
---|
9 | * The default constructor defines a bar with 20 hashes |
---|
10 | * (given by ProgressBar::length), sets the number visible to be 0 |
---|
11 | * and the location to be at the beginning. |
---|
12 | */ |
---|
13 | length=20; |
---|
14 | loc=BEG; |
---|
15 | numVisible = 0; |
---|
16 | }; |
---|
17 | |
---|
18 | ProgressBar::ProgressBar(int newlength){ |
---|
19 | /** |
---|
20 | * This alternative constructor enables the user to define how many |
---|
21 | * hashes should appear. Again, the number visible is set to 0 and |
---|
22 | * the location to be at the beginning. |
---|
23 | * |
---|
24 | * \param newlength The new number of hashes to appear in the bar. |
---|
25 | */ |
---|
26 | length=newlength; |
---|
27 | loc=BEG; |
---|
28 | numVisible = 0; |
---|
29 | }; |
---|
30 | |
---|
31 | ProgressBar::~ProgressBar(){}; |
---|
32 | |
---|
33 | void ProgressBar::init(int size){ |
---|
34 | /** |
---|
35 | * This initialises the bar to deal with a loop of a certain size. |
---|
36 | * This size will imply a certain step size, dependent on the number |
---|
37 | * of hashes that will be written. A blank bar is written out as |
---|
38 | * well, and we remain at the end. |
---|
39 | * |
---|
40 | * \param size The maximum number of iterations to be covered by the |
---|
41 | * progress bar. |
---|
42 | */ |
---|
43 | stepSize = float(size) / float(length); |
---|
44 | std::cout << "|"; |
---|
45 | printSpace(length); |
---|
46 | std::cout << "|" << std::flush; |
---|
47 | loc = END; |
---|
48 | }; |
---|
49 | |
---|
50 | void ProgressBar::update(int num){ |
---|
51 | /** |
---|
52 | * This makes sure the correct number of hashes are drawn. |
---|
53 | * |
---|
54 | * Based on the number provided, as well as the stepsize, we compare |
---|
55 | * the number of hashes we expect to see with the number that are |
---|
56 | * there, and if they differ, the correct number are drawn. Again, |
---|
57 | * we remain at the end. |
---|
58 | * |
---|
59 | * \param num The loop counter to be translated into the progress |
---|
60 | * bar. |
---|
61 | */ |
---|
62 | int numNeeded = 0; |
---|
63 | for(int i=0;i<length;i++) |
---|
64 | if(num>(i*stepSize)) numNeeded++; |
---|
65 | |
---|
66 | if(numNeeded != numVisible){ |
---|
67 | numVisible = numNeeded; |
---|
68 | if(loc==END) printBackSpace(length+2); |
---|
69 | std::cout << "|"; |
---|
70 | printHash(numNeeded); |
---|
71 | printSpace(length-numNeeded); |
---|
72 | std::cout << "|" << std::flush; |
---|
73 | loc=END; |
---|
74 | } |
---|
75 | }; |
---|
76 | |
---|
77 | void ProgressBar::rewind(){ |
---|
78 | /** |
---|
79 | * If we are at the end, we print out enough backspaces to wipe out |
---|
80 | * the entire bar. If we are not, the erasing does not need to be |
---|
81 | * done. |
---|
82 | */ |
---|
83 | if(loc==END) printBackSpace(length+2); |
---|
84 | loc=BEG; |
---|
85 | std::cout << std::flush; |
---|
86 | }; |
---|
87 | |
---|
88 | void ProgressBar::remove(){ |
---|
89 | /** |
---|
90 | * We first rewind() to the beginning, overwrite the bar with blank spaces, |
---|
91 | * and then rewind(). We end up at the beginning. |
---|
92 | */ |
---|
93 | rewind(); |
---|
94 | printSpace(length+2); |
---|
95 | loc=END; |
---|
96 | rewind(); |
---|
97 | std::cout << std::flush; |
---|
98 | }; |
---|
99 | |
---|
100 | void ProgressBar::fillSpace(std::string someString){ |
---|
101 | /** |
---|
102 | * We first remove() the bar and then write out the requested string. |
---|
103 | * \param someString The string to be written over the bar area. |
---|
104 | */ |
---|
105 | remove(); |
---|
106 | std::cout << someString; |
---|
107 | loc=END; |
---|
108 | } |
---|
109 | |
---|