source: tags/release-1.1/src/Utils/feedback.cc @ 1323

Last change on this file since 1323 was 301, checked in by Matthew Whiting, 17 years ago

Mostly adding the distribution text to the start of files, with a few additional comments added too.

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