[301] | 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 | // ----------------------------------------------------------------------- |
---|
[220] | 28 | #include <iostream> |
---|
[1384] | 29 | #include <iomanip> |
---|
[220] | 30 | #include <string> |
---|
[393] | 31 | #include <duchamp/Utils/feedback.hh> |
---|
[220] | 32 | |
---|
[365] | 33 | void printBackSpace(std::ostream &stream, int num) |
---|
| 34 | { |
---|
[1267] | 35 | for(int i=0;i<num;i++) stream << '\b'; |
---|
[365] | 36 | } |
---|
[220] | 37 | |
---|
[365] | 38 | void printSpace(std::ostream &stream, int num) |
---|
| 39 | { |
---|
[1267] | 40 | for(int i=0;i<num;i++) stream << ' '; |
---|
[365] | 41 | } |
---|
| 42 | |
---|
| 43 | void printHash(std::ostream &stream, int num) |
---|
| 44 | { |
---|
[1267] | 45 | for(int i=0;i<num;i++) stream << '#'; |
---|
[365] | 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
[1384] | 49 | ProgressBar::ProgressBar(): |
---|
[1388] | 50 | itsLoc(BEG),itsStepSize(0.),itsLength(DefaultLength),itsNumVisible(0),itsSize(0),itsPercentage(0.) |
---|
[220] | 51 | { |
---|
[1267] | 52 | /// @details |
---|
| 53 | /// The default constructor defines a bar with 20 hashes |
---|
| 54 | /// (given by ProgressBar::length), sets the number visible to be 0 |
---|
| 55 | /// and the location to be at the beginning. |
---|
[419] | 56 | } |
---|
[220] | 57 | |
---|
[1384] | 58 | ProgressBar::ProgressBar(int newlength): |
---|
[1388] | 59 | itsLoc(BEG),itsStepSize(0.),itsLength(newlength),itsNumVisible(0),itsSize(0),itsPercentage(0.) |
---|
[528] | 60 | { |
---|
[1267] | 61 | /// @details |
---|
| 62 | /// This alternative constructor enables the user to define how many |
---|
| 63 | /// hashes should appear. Again, the number visible is set to 0 and |
---|
| 64 | /// the location to be at the beginning. |
---|
| 65 | /// |
---|
| 66 | /// \param newlength The new number of hashes to appear in the bar. |
---|
[419] | 67 | } |
---|
[220] | 68 | |
---|
[419] | 69 | ProgressBar::~ProgressBar(){} |
---|
[220] | 70 | |
---|
[1388] | 71 | void ProgressBar::printPercentage() |
---|
| 72 | { |
---|
| 73 | /// @details |
---|
| 74 | /// Function to print out the percentage, getting the formatting correct |
---|
| 75 | |
---|
| 76 | std::cout << std::setw(PercentWidth) << this->itsPercentage; |
---|
| 77 | std::cout << "%"; |
---|
| 78 | |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
[1384] | 82 | void ProgressBar::init(unsigned int size) |
---|
[528] | 83 | { |
---|
[1267] | 84 | /// @details |
---|
| 85 | /// This initialises the bar to deal with a loop of a certain size. |
---|
| 86 | /// This size will imply a certain step size, dependent on the number |
---|
| 87 | /// of hashes that will be written. A blank bar is written out as |
---|
| 88 | /// well, and we remain at the end. |
---|
| 89 | /// |
---|
| 90 | /// \param size The maximum number of iterations to be covered by the |
---|
| 91 | /// progress bar. |
---|
[1388] | 92 | this->itsSize=size; |
---|
| 93 | if(size < this->itsLength){ |
---|
| 94 | this->itsLength = size; |
---|
| 95 | } |
---|
| 96 | if(this->itsLength > 1) { |
---|
| 97 | this->itsStepSize = float(size) / float(this->itsLength); |
---|
| 98 | std::cout << "|"; |
---|
| 99 | this->space(this->itsLength); |
---|
| 100 | std::cout << "|"; |
---|
| 101 | this->printPercentage(); |
---|
| 102 | std::cout << std::flush; |
---|
| 103 | this->itsLoc = END; |
---|
| 104 | } |
---|
[419] | 105 | } |
---|
[220] | 106 | |
---|
[1384] | 107 | void ProgressBar::update(unsigned int num) |
---|
[528] | 108 | { |
---|
[1267] | 109 | /// @details |
---|
| 110 | /// This makes sure the correct number of hashes are drawn. |
---|
| 111 | /// |
---|
| 112 | /// Based on the number provided, as well as the stepsize, we compare |
---|
| 113 | /// the number of hashes we expect to see with the number that are |
---|
| 114 | /// there, and if they differ, the correct number are drawn. Again, |
---|
| 115 | /// we remain at the end. |
---|
| 116 | /// |
---|
| 117 | /// \param num The loop counter to be translated into the progress |
---|
| 118 | /// bar. |
---|
[528] | 119 | |
---|
[1385] | 120 | int newPercentage = 100 * num / this->itsSize; |
---|
[1384] | 121 | |
---|
| 122 | if( this->itsLength > 1 ){ |
---|
[1267] | 123 | int numNeeded = 0; |
---|
[1384] | 124 | for(unsigned int i=0;i<this->itsLength;i++) |
---|
| 125 | if(num>(i*this->itsStepSize)) numNeeded++; |
---|
[220] | 126 | |
---|
[1385] | 127 | if(numNeeded > this->itsNumVisible){ |
---|
| 128 | this->itsPercentage = newPercentage; |
---|
[1384] | 129 | this->itsNumVisible = numNeeded; |
---|
| 130 | if(this->itsLoc==END) this->backSpace(this->itsLength); |
---|
[1267] | 131 | std::cout << "|"; |
---|
[1384] | 132 | this->hash(this->itsNumVisible); |
---|
| 133 | this->space(this->itsLength-this->itsNumVisible); |
---|
| 134 | std::cout << "|"; |
---|
[1388] | 135 | this->printPercentage(); |
---|
[1384] | 136 | std::cout << std::flush; |
---|
| 137 | this->itsLoc=END; |
---|
[1267] | 138 | } |
---|
[1385] | 139 | else if(newPercentage > this->itsPercentage){ |
---|
| 140 | this->itsPercentage = newPercentage; |
---|
[1388] | 141 | if(this->itsLoc==END) printBackSpace(std::cout,PercentWidth+1); |
---|
| 142 | this->printPercentage(); |
---|
[1385] | 143 | std::cout << std::flush; |
---|
| 144 | this->itsLoc=END; |
---|
| 145 | } |
---|
[1267] | 146 | |
---|
| 147 | } |
---|
[419] | 148 | } |
---|
[220] | 149 | |
---|
[528] | 150 | void ProgressBar::rewind() |
---|
| 151 | { |
---|
[1267] | 152 | /// @details |
---|
| 153 | /// If we are at the end, we print out enough backspaces to wipe out |
---|
| 154 | /// the entire bar. If we are not, the erasing does not need to be |
---|
| 155 | /// done. |
---|
[1388] | 156 | if(this->itsLoc==END) this->backSpace(this->itsLength); |
---|
| 157 | std::cout << std::flush; |
---|
[419] | 158 | } |
---|
[220] | 159 | |
---|
[528] | 160 | void ProgressBar::remove() |
---|
| 161 | { |
---|
[1267] | 162 | /// @details |
---|
| 163 | /// We first rewind() to the beginning, overwrite the bar with blank spaces, |
---|
| 164 | /// and then rewind(). We end up at the beginning. |
---|
| 165 | rewind(); |
---|
[1384] | 166 | this->space(this->itsLength+ExtraSpaces); |
---|
| 167 | this->itsLoc=END; |
---|
[1267] | 168 | rewind(); |
---|
| 169 | std::cout << std::flush; |
---|
[419] | 170 | } |
---|
[220] | 171 | |
---|
[528] | 172 | void ProgressBar::fillSpace(std::string someString) |
---|
| 173 | { |
---|
[1267] | 174 | /// @details |
---|
| 175 | /// We first remove() the bar and then write out the requested string. |
---|
| 176 | /// \param someString The string to be written over the bar area. |
---|
| 177 | remove(); |
---|
| 178 | std::cout << someString; |
---|
[1384] | 179 | this->itsLoc=END; |
---|
[220] | 180 | } |
---|
| 181 | |
---|