Nektar++
Progressbar.hpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Progressbar.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2015 Kilian Lackhove
10 //
11 // License for the specific language governing rights and limitations under
12 // Permission is hereby granted, free of charge, to any person obtaining a
13 // copy of this software and associated documentation files (the "Software"),
14 // to deal in the Software without restriction, including without limitation
15 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 // and/or sell copies of the Software, and to permit persons to whom the
17 // Software is furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included
20 // in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 // DEALINGS IN THE SOFTWARE.
29 //
30 // Description: Print a simple progress bar
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 
34 #ifndef NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
35 #define NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
36 
37 #include <stdio.h>
38 #include <iostream>
39 #include <iomanip>
40 #include <string>
41 
42 #ifdef _WIN32
43 #include <io.h>
44 #define ISTTY _isatty(_fileno(stdout))
45 #else
46 #include <unistd.h>
47 #define ISTTY isatty(fileno(stdout))
48 #endif
49 
50 using namespace std;
51 
52 namespace Nektar
53 {
54 namespace LibUtilities
55 {
56 
57 /**
58  * @brief Prints a progressbar
59  *
60  * @param position State of the current process
61  * @param goal Goal of the current process
62  * @param message Short Description of the current process
63  *
64  * This function plots a simple progressbar to the console or log file to
65  * visualize the current state of an ongoing process. Make sure you minimize
66  * calling this routine. Ideally, this should be called only when the
67  * percentage is increased by an integer.
68  */
69 inline void PrintProgressbar(const int position, const int goal, const string message)
70 {
71  if (ISTTY)
72  {
73  // carriage return
74  cout << "\r";
75 
76  cout << message << ": ";
77  float progress = position / float(goal);
78  cout << setw(3) << int(100 * progress) << "% [";
79  for (int j = 0; j < int(progress * 49); j++)
80  {
81  cout << "=";
82  }
83  for (int j = int(progress * 49); j < 49; j++)
84  {
85  cout << " ";
86  }
87  cout << "]" << flush;
88  }
89  else
90  {
91  // print only every 2 percent
92  if (int(100 * position / goal) % 2 == 0)
93  {
94  cout << "." << flush;
95  }
96  }
97 }
98 
99 }
100 }
101 
102 #endif // NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
STL namespace.
#define ISTTY
Definition: Progressbar.hpp:47
void PrintProgressbar(const int position, const int goal, const string message)
Prints a progressbar.
Definition: Progressbar.hpp:69