1 | // ----------------------------------------------------------------------- |
---|
2 | // feedback.cc: Member functions for the ProgressBar class. |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <string> |
---|
30 | #include <Utils/feedback.hh> |
---|
31 | |
---|
32 | void printBackSpace(std::ostream &stream, int num) |
---|
33 | { |
---|
34 | for(int i=0;i<num;i++) stream << '\b'; |
---|
35 | } |
---|
36 | void printBackSpace(int num) |
---|
37 | { |
---|
38 | printBackSpace(std::cout,num); |
---|
39 | } |
---|
40 | |
---|
41 | void printSpace(std::ostream &stream, int num) |
---|
42 | { |
---|
43 | for(int i=0;i<num;i++) stream << ' '; |
---|
44 | } |
---|
45 | void printSpace(int num) |
---|
46 | { |
---|
47 | printSpace(std::cout,num); |
---|
48 | } |
---|
49 | |
---|
50 | void printHash(std::ostream &stream, int num) |
---|
51 | { |
---|
52 | for(int i=0;i<num;i++) stream << '#'; |
---|
53 | } |
---|
54 | void printHash(int num) |
---|
55 | { |
---|
56 | printHash(std::cout,num); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | ProgressBar::ProgressBar() |
---|
61 | { |
---|
62 | /** |
---|
63 | * The default constructor defines a bar with 20 hashes |
---|
64 | * (given by ProgressBar::length), sets the number visible to be 0 |
---|
65 | * and the location to be at the beginning. |
---|
66 | */ |
---|
67 | length=20; |
---|
68 | loc=BEG; |
---|
69 | numVisible = 0; |
---|
70 | }; |
---|
71 | |
---|
72 | ProgressBar::ProgressBar(int newlength){ |
---|
73 | /** |
---|
74 | * This alternative constructor enables the user to define how many |
---|
75 | * hashes should appear. Again, the number visible is set to 0 and |
---|
76 | * the location to be at the beginning. |
---|
77 | * |
---|
78 | * \param newlength The new number of hashes to appear in the bar. |
---|
79 | */ |
---|
80 | length=newlength; |
---|
81 | loc=BEG; |
---|
82 | numVisible = 0; |
---|
83 | }; |
---|
84 | |
---|
85 | ProgressBar::~ProgressBar(){}; |
---|
86 | |
---|
87 | void ProgressBar::init(int size){ |
---|
88 | /** |
---|
89 | * This initialises the bar to deal with a loop of a certain size. |
---|
90 | * This size will imply a certain step size, dependent on the number |
---|
91 | * of hashes that will be written. A blank bar is written out as |
---|
92 | * well, and we remain at the end. |
---|
93 | * |
---|
94 | * \param size The maximum number of iterations to be covered by the |
---|
95 | * progress bar. |
---|
96 | */ |
---|
97 | stepSize = float(size) / float(length); |
---|
98 | std::cout << "|"; |
---|
99 | printSpace(length); |
---|
100 | std::cout << "|" << std::flush; |
---|
101 | loc = END; |
---|
102 | }; |
---|
103 | |
---|
104 | void ProgressBar::update(int num){ |
---|
105 | /** |
---|
106 | * This makes sure the correct number of hashes are drawn. |
---|
107 | * |
---|
108 | * Based on the number provided, as well as the stepsize, we compare |
---|
109 | * the number of hashes we expect to see with the number that are |
---|
110 | * there, and if they differ, the correct number are drawn. Again, |
---|
111 | * we remain at the end. |
---|
112 | * |
---|
113 | * \param num The loop counter to be translated into the progress |
---|
114 | * bar. |
---|
115 | */ |
---|
116 | int numNeeded = 0; |
---|
117 | for(int i=0;i<length;i++) |
---|
118 | if(num>(i*stepSize)) numNeeded++; |
---|
119 | |
---|
120 | if(numNeeded != numVisible){ |
---|
121 | numVisible = numNeeded; |
---|
122 | if(loc==END) printBackSpace(length+2); |
---|
123 | std::cout << "|"; |
---|
124 | printHash(numNeeded); |
---|
125 | printSpace(length-numNeeded); |
---|
126 | std::cout << "|" << std::flush; |
---|
127 | loc=END; |
---|
128 | } |
---|
129 | }; |
---|
130 | |
---|
131 | void ProgressBar::rewind(){ |
---|
132 | /** |
---|
133 | * If we are at the end, we print out enough backspaces to wipe out |
---|
134 | * the entire bar. If we are not, the erasing does not need to be |
---|
135 | * done. |
---|
136 | */ |
---|
137 | if(loc==END) printBackSpace(length+2); |
---|
138 | loc=BEG; |
---|
139 | std::cout << std::flush; |
---|
140 | }; |
---|
141 | |
---|
142 | void ProgressBar::remove(){ |
---|
143 | /** |
---|
144 | * We first rewind() to the beginning, overwrite the bar with blank spaces, |
---|
145 | * and then rewind(). We end up at the beginning. |
---|
146 | */ |
---|
147 | rewind(); |
---|
148 | printSpace(length+2); |
---|
149 | loc=END; |
---|
150 | rewind(); |
---|
151 | std::cout << std::flush; |
---|
152 | }; |
---|
153 | |
---|
154 | void ProgressBar::fillSpace(std::string someString){ |
---|
155 | /** |
---|
156 | * We first remove() the bar and then write out the requested string. |
---|
157 | * \param someString The string to be written over the bar area. |
---|
158 | */ |
---|
159 | remove(); |
---|
160 | std::cout << someString; |
---|
161 | loc=END; |
---|
162 | } |
---|
163 | |
---|