source: trunk/src/Utils/feedback.cc @ 1385

Last change on this file since 1385 was 1385, checked in by MatthewWhiting, 10 years ago

Improving the output of the percentage, so that it updates even if the hashes don't. It will only update when the number changes.

File size: 5.6 KB
Line 
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 <iomanip>
30#include <string>
31#include <duchamp/Utils/feedback.hh>
32
33void printBackSpace(std::ostream &stream, int num)
34{
35    for(int i=0;i<num;i++) stream << '\b';
36}
37
38void printSpace(std::ostream &stream, int num)
39{
40    for(int i=0;i<num;i++) stream << ' ';
41}
42
43void printHash(std::ostream &stream, int num)
44{
45    for(int i=0;i<num;i++) stream << '#';
46}
47
48
49ProgressBar::ProgressBar():
50  itsLoc(BEG),itsStepSize(0.),itsLength(DefaultLength),itsNumVisible(0),itsSize(0),itsPercentage(0.)
51{
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.
56}
57
58ProgressBar::ProgressBar(int newlength):
59  itsLoc(BEG),itsStepSize(0.),itsLength(newlength),itsNumVisible(0),itsSize(0),itsPercentage(0.)
60{
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.
67}
68
69ProgressBar::~ProgressBar(){}
70
71void ProgressBar::init(unsigned int size)
72{
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.
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  }
96}
97
98void ProgressBar::update(unsigned int num)
99{
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.
110
111    int newPercentage = 100 * num / this->itsSize;
112
113    if( this->itsLength > 1 ){
114        int numNeeded = 0;
115        for(unsigned int i=0;i<this->itsLength;i++)
116            if(num>(i*this->itsStepSize)) numNeeded++;
117   
118//      if(numNeeded != this->itsNumVisible){
119        if(numNeeded > this->itsNumVisible){
120            this->itsPercentage = newPercentage;
121            this->itsNumVisible = numNeeded;
122            if(this->itsLoc==END) this->backSpace(this->itsLength);
123            std::cout << "|";
124            this->hash(this->itsNumVisible);
125            this->space(this->itsLength-this->itsNumVisible);
126            std::cout << "|";
127            std::cout << std::setw(3) << this->itsPercentage;
128            std::cout << "%";
129            std::cout << std::flush;
130            this->itsLoc=END;
131        }
132        else if(newPercentage > this->itsPercentage){
133            this->itsPercentage = newPercentage;
134            if(this->itsLoc==END) printBackSpace(std::cout,4);
135            std::cout << std::setw(3) << this->itsPercentage;
136            std::cout << "%";
137            std::cout << std::flush;
138            this->itsLoc=END;
139        }
140
141    }
142}
143
144void ProgressBar::rewind()
145{
146    /// @details
147    /// If we are at the end, we print out enough backspaces to wipe out
148    /// the entire bar.  If we are not, the erasing does not need to be
149    /// done.
150  if(this->itsLoc==END) this->backSpace(this->itsLength);
151  std::cout << std::flush;
152}
153
154void ProgressBar::remove()
155{
156    /// @details
157    /// We first rewind() to the beginning, overwrite the bar with blank spaces,
158    /// and then rewind(). We end up at the beginning.
159    rewind();
160    this->space(this->itsLength+ExtraSpaces);
161    this->itsLoc=END;
162    rewind();
163    std::cout << std::flush;
164}
165
166void ProgressBar::fillSpace(std::string someString)
167{
168    /// @details
169    /// We first remove() the bar and then write out the requested string.
170    /// \param someString The string to be written over the bar area.
171    remove();
172    std::cout << someString;
173    this->itsLoc=END;
174}
175
Note: See TracBrowser for help on using the repository browser.