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

Last change on this file since 1385 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: 4.2 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 ExtraSpaces=6;                ///< Extra spaces other than hashes - the two '|', 3 chars for percentage, plus '%'
62
63class ProgressBar
64{
65public:
66  ProgressBar();                          ///< Default Constructor
67  ProgressBar(int newlength);             ///< Alternative constructor
68  virtual ~ProgressBar();                 ///< Destructor.
69  enum POS {BEG=0,END};                   ///< So that we can record where we are.
70
71  void init(unsigned int size);           ///< Prints empty bar, defines increment.
72  void update(unsigned int num);          ///< Prints correct number of hashes
73  void rewind();                          ///< Prints backspaces over bar.
74  void remove();                          ///< Overwrites bar with blanks
75  void fillSpace(std::string someString); ///< Overwrites bar with a string.
76
77  void backSpace(int num){printBackSpace(std::cout,num+ExtraSpaces); itsLoc=BEG;};
78  void space(int num){printSpace(std::cout,num);};
79  void hash(int num){printHash(std::cout,num);};
80
81
82private:
83  POS itsLoc;                                ///< Are we at the start or end?
84  float itsStepSize;                         ///< What is the interval between hashes?
85  unsigned int itsLength;                    ///< What's the maximum number of hashes?
86  unsigned int itsNumVisible;                ///< How many hashes are there currently visible?
87  unsigned int itsSize;                      ///< The total number of things to be considered
88  unsigned int itsPercentage;                ///< What percentage of the way through are we?
89};
90
91
92
93#endif // FEEDBACK_HH
Note: See TracBrowser for help on using the repository browser.