source: tags/release-1.6.1/src/Utils/feedback.cc @ 1441

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

Reformatting, and taking the percentage-writing code out into its own function. Have a new const to handle the width of percent value.

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::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
82void ProgressBar::init(unsigned int size)
83{
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.
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    }
105}
106
107void ProgressBar::update(unsigned int num)
108{
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.
119
120    int newPercentage = 100 * num / this->itsSize;
121
122    if( this->itsLength > 1 ){
123        int numNeeded = 0;
124        for(unsigned int i=0;i<this->itsLength;i++)
125            if(num>(i*this->itsStepSize)) numNeeded++;
126   
127        if(numNeeded > this->itsNumVisible){
128            this->itsPercentage = newPercentage;
129            this->itsNumVisible = numNeeded;
130            if(this->itsLoc==END) this->backSpace(this->itsLength);
131            std::cout << "|";
132            this->hash(this->itsNumVisible);
133            this->space(this->itsLength-this->itsNumVisible);
134            std::cout << "|";
135            this->printPercentage();
136            std::cout << std::flush;
137            this->itsLoc=END;
138        }
139        else if(newPercentage > this->itsPercentage){
140            this->itsPercentage = newPercentage;
141            if(this->itsLoc==END) printBackSpace(std::cout,PercentWidth+1);
142            this->printPercentage();
143            std::cout << std::flush;
144            this->itsLoc=END;
145        }
146
147    }
148}
149
150void ProgressBar::rewind()
151{
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.
156    if(this->itsLoc==END) this->backSpace(this->itsLength);
157    std::cout << std::flush;
158}
159
160void ProgressBar::remove()
161{
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();
166    this->space(this->itsLength+ExtraSpaces);
167    this->itsLoc=END;
168    rewind();
169    std::cout << std::flush;
170}
171
172void ProgressBar::fillSpace(std::string someString)
173{
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;
179    this->itsLoc=END;
180}
181
Note: See TracBrowser for help on using the repository browser.