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

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

A refactoring of the progress bar class, improving the code and adding a percentage done at the end of the bar (for clarity). Have also made some changes to other code that call the printing commands directly.

File size: 5.3 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  this->itsPercentage = 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            this->itsNumVisible = numNeeded;
120            if(this->itsLoc==END) this->backSpace(this->itsLength);
121            std::cout << "|";
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;
129        }
130
131    }
132}
133
134void ProgressBar::rewind()
135{
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.
140  if(this->itsLoc==END) this->backSpace(this->itsLength);
141  std::cout << std::flush;
142}
143
144void ProgressBar::remove()
145{
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();
150    this->space(this->itsLength+ExtraSpaces);
151    this->itsLoc=END;
152    rewind();
153    std::cout << std::flush;
154}
155
156void ProgressBar::fillSpace(std::string someString)
157{
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;
163    this->itsLoc=END;
164}
165
Note: See TracBrowser for help on using the repository browser.