[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(): |
---|
| 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): |
---|
| 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 | |
---|
[1384] | 71 | void ProgressBar::init(unsigned int size) |
---|
[528] | 72 | { |
---|
[1267] | 73 | /// @details |
---|
| 74 | /// This initialises the bar to deal with a loop of a certain size. |
---|
| 75 | /// This size will imply a certain step size, dependent on the number |
---|
| 76 | /// of hashes that will be written. A blank bar is written out as |
---|
| 77 | /// well, and we remain at the end. |
---|
| 78 | /// |
---|
| 79 | /// \param size The maximum number of iterations to be covered by the |
---|
| 80 | /// progress bar. |
---|
[1384] | 81 | this->itsSize=size; |
---|
| 82 | if(size < this->itsLength){ |
---|
| 83 | this->itsLength = size; |
---|
| 84 | } |
---|
| 85 | if(this->itsLength > 1) { |
---|
| 86 | this->itsStepSize = float(size) / float(this->itsLength); |
---|
| 87 | std::cout << "|"; |
---|
| 88 | this->space(this->itsLength); |
---|
| 89 | std::cout << "|"; |
---|
| 90 | std::cout.setf(std::ios::fixed); |
---|
| 91 | std::cout << std::setw(3) << this->itsPercentage; |
---|
| 92 | std::cout << "%"; |
---|
| 93 | std::cout << std::flush; |
---|
| 94 | this->itsLoc = END; |
---|
| 95 | } |
---|
[419] | 96 | } |
---|
[220] | 97 | |
---|
[1384] | 98 | void ProgressBar::update(unsigned int num) |
---|
[528] | 99 | { |
---|
[1267] | 100 | /// @details |
---|
| 101 | /// This makes sure the correct number of hashes are drawn. |
---|
| 102 | /// |
---|
| 103 | /// Based on the number provided, as well as the stepsize, we compare |
---|
| 104 | /// the number of hashes we expect to see with the number that are |
---|
| 105 | /// there, and if they differ, the correct number are drawn. Again, |
---|
| 106 | /// we remain at the end. |
---|
| 107 | /// |
---|
| 108 | /// \param num The loop counter to be translated into the progress |
---|
| 109 | /// bar. |
---|
[528] | 110 | |
---|
[1384] | 111 | this->itsPercentage = 100 * num / this->itsSize; |
---|
| 112 | |
---|
| 113 | if( this->itsLength > 1 ){ |
---|
[1267] | 114 | int numNeeded = 0; |
---|
[1384] | 115 | for(unsigned int i=0;i<this->itsLength;i++) |
---|
| 116 | if(num>(i*this->itsStepSize)) numNeeded++; |
---|
[220] | 117 | |
---|
[1384] | 118 | if(numNeeded != this->itsNumVisible){ |
---|
| 119 | this->itsNumVisible = numNeeded; |
---|
| 120 | if(this->itsLoc==END) this->backSpace(this->itsLength); |
---|
[1267] | 121 | std::cout << "|"; |
---|
[1384] | 122 | this->hash(this->itsNumVisible); |
---|
| 123 | this->space(this->itsLength-this->itsNumVisible); |
---|
| 124 | std::cout << "|"; |
---|
| 125 | std::cout << std::setw(3) << this->itsPercentage; |
---|
| 126 | std::cout << "%"; |
---|
| 127 | std::cout << std::flush; |
---|
| 128 | this->itsLoc=END; |
---|
[1267] | 129 | } |
---|
| 130 | |
---|
| 131 | } |
---|
[419] | 132 | } |
---|
[220] | 133 | |
---|
[528] | 134 | void ProgressBar::rewind() |
---|
| 135 | { |
---|
[1267] | 136 | /// @details |
---|
| 137 | /// If we are at the end, we print out enough backspaces to wipe out |
---|
| 138 | /// the entire bar. If we are not, the erasing does not need to be |
---|
| 139 | /// done. |
---|
[1384] | 140 | if(this->itsLoc==END) this->backSpace(this->itsLength); |
---|
| 141 | std::cout << std::flush; |
---|
[419] | 142 | } |
---|
[220] | 143 | |
---|
[528] | 144 | void ProgressBar::remove() |
---|
| 145 | { |
---|
[1267] | 146 | /// @details |
---|
| 147 | /// We first rewind() to the beginning, overwrite the bar with blank spaces, |
---|
| 148 | /// and then rewind(). We end up at the beginning. |
---|
| 149 | rewind(); |
---|
[1384] | 150 | this->space(this->itsLength+ExtraSpaces); |
---|
| 151 | this->itsLoc=END; |
---|
[1267] | 152 | rewind(); |
---|
| 153 | std::cout << std::flush; |
---|
[419] | 154 | } |
---|
[220] | 155 | |
---|
[528] | 156 | void ProgressBar::fillSpace(std::string someString) |
---|
| 157 | { |
---|
[1267] | 158 | /// @details |
---|
| 159 | /// We first remove() the bar and then write out the requested string. |
---|
| 160 | /// \param someString The string to be written over the bar area. |
---|
| 161 | remove(); |
---|
| 162 | std::cout << someString; |
---|
[1384] | 163 | this->itsLoc=END; |
---|
[220] | 164 | } |
---|
| 165 | |
---|