libatomprobe
Library for Atom Probe Tomography (APT) computation
progress.cpp
Go to the documentation of this file.
1 /*
2  * progress.cpp: Simple progress bar
3  * Copyright (C) 2015 D Haley
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 #include <iostream>
22 #include <cstdlib>
23 
24 using std::endl;
25 using std::cerr;
26 
27 namespace AtomProbe{
28 
30 {
31  if(printEnd)
32  finish();
33 }
34 
36 {
37  cerr << endl << "|" ;
38  for(size_t ui=0;ui<length;ui++)
39  cerr << "-";
40  cerr << "|" << endl <<"|";
41 }
42 
44 {
45  accumulatedTicks=0;
46  lastProg=0;
47  length=100;
48  printEnd=true;
49 }
50 
51 
53 {
54  update(100);
55 }
56 
57 
58 void ProgressBar::update(unsigned int progress)
59 {
60  if(!printEnd)
61  return;
62  unsigned int delta;
63 
64  delta = progress-lastProg;
65  lastProg=progress;
66 
67  float ticks = ((float)delta*(float)length/100.0f);
68  accumulatedTicks+=ticks;
69 
70  if(accumulatedTicks >=1.0f)
71  {
72 
73  delta=accumulatedTicks;
74  accumulatedTicks-=delta;
75 
76  while(delta--)
77  cerr << ".";
78  }
79 
80  if(progress >= 100)
81  {
82  cerr << "| Done." << endl;
83  printEnd=false;
84  }
85 }
86 
87 #ifdef DEBUG
88 bool testProgressBar()
89 {
90  ProgressBar pb;
91  pb.setLength(50);
92 
93  pb.init();
94  pb.update(24);
95  pb.finish();
96 
97  return true;
98 }
99 #endif
100 }
void finish()
Finalise the progress bar. It is not necessary for the progress to be set to 100%, this is done for you.
Definition: progress.cpp:52
void init()
Draw the initial progress bar.
Definition: progress.cpp:35
void setLength(unsigned int l)
Set the number of markers in the progress bar.
Definition: progress.h:40
unsigned int progress
Definition: kd-example.cpp:26
void reset()
reset the progress bar internals, in case we want to re-use it
Definition: progress.cpp:43
void update(unsigned int newProgress)
Draw the progress bar as needed, using the given progress value [0,100].
Definition: progress.cpp:58