source: tags/release-1.2.2/src/Utils/feedback.cc

Last change on this file was 528, checked in by MatthewWhiting, 15 years ago

Changing the documentation comments to match the askapsoft style. Also have split ChanMap? and Object3D into separate files.

File size: 4.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 <string>
30#include <duchamp/Utils/feedback.hh>
31
32void printBackSpace(std::ostream &stream, int num)
33{
34  for(int i=0;i<num;i++) stream << '\b';
35}
36void printBackSpace(int num)
37{
38  printBackSpace(std::cout,num);
39}
40
41void printSpace(std::ostream &stream, int num)
42{
43  for(int i=0;i<num;i++) stream << ' ';
44}
45void printSpace(int num)
46{
47  printSpace(std::cout,num);
48}
49
50void printHash(std::ostream &stream, int num)
51{
52  for(int i=0;i<num;i++) stream << '#';
53}
54void printHash(int num)
55{
56  printHash(std::cout,num);
57}
58
59
60ProgressBar::ProgressBar()
61{
62  /// @details
63  /// The default constructor defines a bar with 20 hashes
64  /// (given by ProgressBar::length), sets the number visible to be 0
65  ///  and the location to be at the beginning.
66  length=20;
67  loc=BEG;
68  numVisible = 0;
69}
70
71ProgressBar::ProgressBar(int newlength)
72{
73  /// @details
74  /// This alternative constructor enables the user to define how many
75  /// hashes should appear. Again, the number visible is set to 0 and
76  /// the location to be at the beginning. 
77  ///
78  /// \param newlength The new number of hashes to appear in the bar.
79  length=newlength;
80  loc=BEG;
81  numVisible = 0;
82}
83
84ProgressBar::~ProgressBar(){}
85
86void ProgressBar::init(int size)
87{
88  /// @details
89  /// This initialises the bar to deal with a loop of a certain size.
90  /// This size will imply a certain step size, dependent on the number
91  /// of hashes that will be written.  A blank bar is written out as
92  /// well, and we remain at the end. 
93  ///
94  /// \param size The maximum number of iterations to be covered by the
95  /// progress bar.
96  stepSize = float(size) / float(length);
97  std::cout << "|";
98  printSpace(length);
99  std::cout << "|" << std::flush;
100  loc = END;
101}
102
103void ProgressBar::update(int num)
104{
105  /// @details
106  /// This makes sure the correct number of hashes are drawn.
107  ///
108  /// Based on the number provided, as well as the stepsize, we compare
109  /// the number of hashes we expect to see with the number that are
110  /// there, and if they differ, the correct number are drawn. Again,
111  /// we remain at the end. 
112  ///
113  /// \param num The loop counter to be translated into the progress
114  /// bar.
115
116  int numNeeded = 0;
117  for(int i=0;i<length;i++)
118    if(num>(i*stepSize)) numNeeded++;
119   
120  if(numNeeded != numVisible){
121    numVisible = numNeeded;
122    if(loc==END) printBackSpace(length+2);
123    std::cout << "|";
124    printHash(numNeeded);
125    printSpace(length-numNeeded);
126    std::cout << "|" << std::flush;
127    loc=END;
128  }
129}
130
131void ProgressBar::rewind()
132{
133  /// @details
134  /// If we are at the end, we print out enough backspaces to wipe out
135  /// the entire bar.  If we are not, the erasing does not need to be
136  /// done.
137  if(loc==END) printBackSpace(length+2);
138  loc=BEG;
139  std::cout << std::flush;
140}
141
142void ProgressBar::remove()
143{
144  /// @details
145  /// We first rewind() to the beginning, overwrite the bar with blank spaces,
146  /// and then rewind(). We end up at the beginning.
147  rewind();
148  printSpace(length+2);
149  loc=END;
150  rewind();
151  std::cout << std::flush;
152}
153
154void ProgressBar::fillSpace(std::string someString)
155{
156  /// @details
157  /// We first remove() the bar and then write out the requested string.
158  /// \param someString The string to be written over the bar area.
159  remove();
160  std::cout << someString;
161  loc=END;
162}
163
Note: See TracBrowser for help on using the repository browser.