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 <duchamp/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 | /// @details |
---|
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 | length=20; |
---|
67 | loc=BEG; |
---|
68 | numVisible = 0; |
---|
69 | } |
---|
70 | |
---|
71 | ProgressBar::ProgressBar(int newlength) |
---|
72 | { |
---|
73 | /// @details |
---|
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 | length=newlength; |
---|
80 | loc=BEG; |
---|
81 | numVisible = 0; |
---|
82 | } |
---|
83 | |
---|
84 | ProgressBar::~ProgressBar(){} |
---|
85 | |
---|
86 | void ProgressBar::init(int size) |
---|
87 | { |
---|
88 | /// @details |
---|
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 | if(size < length){ |
---|
97 | length = size; |
---|
98 | } |
---|
99 | if(length > 1) { |
---|
100 | stepSize = float(size) / float(length); |
---|
101 | std::cout << "|"; |
---|
102 | printSpace(length); |
---|
103 | std::cout << "|" << std::flush; |
---|
104 | loc = END; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void ProgressBar::update(int num) |
---|
109 | { |
---|
110 | /// @details |
---|
111 | /// This makes sure the correct number of hashes are drawn. |
---|
112 | /// |
---|
113 | /// Based on the number provided, as well as the stepsize, we compare |
---|
114 | /// the number of hashes we expect to see with the number that are |
---|
115 | /// there, and if they differ, the correct number are drawn. Again, |
---|
116 | /// we remain at the end. |
---|
117 | /// |
---|
118 | /// \param num The loop counter to be translated into the progress |
---|
119 | /// bar. |
---|
120 | |
---|
121 | if( length > 1 ){ |
---|
122 | int numNeeded = 0; |
---|
123 | for(int i=0;i<length;i++) |
---|
124 | if(num>(i*stepSize)) numNeeded++; |
---|
125 | |
---|
126 | if(numNeeded != numVisible){ |
---|
127 | numVisible = numNeeded; |
---|
128 | if(loc==END) printBackSpace(length+2); |
---|
129 | std::cout << "|"; |
---|
130 | printHash(numNeeded); |
---|
131 | printSpace(length-numNeeded); |
---|
132 | std::cout << "|" << std::flush; |
---|
133 | loc=END; |
---|
134 | } |
---|
135 | |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | void ProgressBar::rewind() |
---|
140 | { |
---|
141 | /// @details |
---|
142 | /// If we are at the end, we print out enough backspaces to wipe out |
---|
143 | /// the entire bar. If we are not, the erasing does not need to be |
---|
144 | /// done. |
---|
145 | if(loc==END) printBackSpace(length+2); |
---|
146 | loc=BEG; |
---|
147 | std::cout << std::flush; |
---|
148 | } |
---|
149 | |
---|
150 | void ProgressBar::remove() |
---|
151 | { |
---|
152 | /// @details |
---|
153 | /// We first rewind() to the beginning, overwrite the bar with blank spaces, |
---|
154 | /// and then rewind(). We end up at the beginning. |
---|
155 | rewind(); |
---|
156 | printSpace(length+2); |
---|
157 | loc=END; |
---|
158 | rewind(); |
---|
159 | std::cout << std::flush; |
---|
160 | } |
---|
161 | |
---|
162 | void ProgressBar::fillSpace(std::string someString) |
---|
163 | { |
---|
164 | /// @details |
---|
165 | /// We first remove() the bar and then write out the requested string. |
---|
166 | /// \param someString The string to be written over the bar area. |
---|
167 | remove(); |
---|
168 | std::cout << someString; |
---|
169 | loc=END; |
---|
170 | } |
---|
171 | |
---|