source: tags/release-1.6.1/src/Utils/feedback.hh @ 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: 4.3 KB
Line 
1// -----------------------------------------------------------------------
2// feedback.hh: Definition of the ProgressBar class, used to indicate
3//              progress through a lengthy iteration.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#ifndef FEEDBACK_HH
30#define FEEDBACK_HH
31
32#include <iostream>
33#include <string>
34
35// Simple functions to print a given number of backspaces or spaces
36//  to std::cout
37void printBackSpace(std::ostream &stream, int num);
38void printSpace(std::ostream &stream, int num);
39void printHash(std::ostream &stream, int num);
40
41/// @brief
42///  Controls printing out a progress bar.
43/// @details
44///   A class that prints out a progress bar in the form
45///    \f$|\#\#\#\ \ \ \ \ \ \ \ \ \ |\f$
46///    that shows how far through a function or loop you are.
47///   The length of it defaults to 20 hashes, but can be set when
48///   declaring the object.
49///   There are five functions:
50///    <ul><li>init(int)   Prints an empty bar, and defines the increment
51///        <li>update(int) Prints the correct number of hashes, but only when
52///                         num is a multiple of the increment.
53///        <li>rewind()    Prints backspaces to cover the entire bar.
54///        <li>remove()    Does a rewind(), then prints spaces to overwrite
55///                         the bar area -- more clean.
56///        <li>fillSpace(std::string) Does a remove(), then writes
57///                                    the string into the same space.
58/// </ul>
59
60const int DefaultLength=20;             ///< Default number of hashes
61const int PercentWidth=3;               ///< Space required for percent value
62const int ExtraSpaces=PercentWidth+1+2; ///< Extra spaces other than hashes - the percent value, plus two '|' and a '%'
63
64class ProgressBar
65{
66public:
67    ProgressBar();                          ///< Default Constructor
68    ProgressBar(int newlength);             ///< Alternative constructor
69    virtual ~ProgressBar();                 ///< Destructor.
70    enum POS {BEG=0,END};                   ///< So that we can record where we are.
71
72    void init(unsigned int size);           ///< Prints empty bar, defines increment.
73    void update(unsigned int num);          ///< Prints correct number of hashes
74    void rewind();                          ///< Prints backspaces over bar.
75    void remove();                          ///< Overwrites bar with blanks
76    void fillSpace(std::string someString); ///< Overwrites bar with a string.
77
78    void printPercentage();
79    void backSpace(int num){printBackSpace(std::cout,num+ExtraSpaces); itsLoc=BEG;};
80    void space(int num){printSpace(std::cout,num);};
81    void hash(int num){printHash(std::cout,num);};
82
83
84private:
85    POS itsLoc;                                ///< Are we at the start or end?
86    float itsStepSize;                         ///< What is the interval between hashes?
87    unsigned int itsLength;                    ///< What's the maximum number of hashes?
88    unsigned int itsNumVisible;                ///< How many hashes are there currently visible?
89    unsigned int itsSize;                      ///< The total number of things to be considered
90    unsigned int itsPercentage;                ///< What percentage of the way through are we?
91};
92
93
94
95#endif // FEEDBACK_HH
Note: See TracBrowser for help on using the repository browser.