Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include <cmath>
42 
43 #ifdef _WIN32
44 #include <io.h>
45 #define ISTTY _isatty(_fileno(stdout))
46 #else
47 #include <unistd.h>
48 #define ISTTY isatty(fileno(stdout))
49 #endif
50 
51 namespace Nektar
52 {
53 namespace LibUtilities
54 {
55 using namespace std;
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  std::cout.unsetf ( std::ios::floatfield );
72  if (ISTTY)
73  {
74  // carriage return
75  cout << "\r";
76 
77  cout << message << ": ";
78  float progress = position / float(goal);
79  cout << setw(3) << ceil(100 * progress) << "% [";
80  for (int j = 0; j < ceil(progress * 49); j++)
81  {
82  cout << "=";
83  }
84  for (int j = ceil(progress * 49); j < 49; j++)
85  {
86  cout << " ";
87  }
88  cout << "]" << flush;
89  }
90  else
91  {
92  // print only every 2 percent
93  if (int(ceil(double(100 * position / goal))) % 2 == 0)
94  {
95  cout << "." << flush;
96  }
97  }
98 }
99 
100 }
101 }
102 
103 #endif // NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
STL namespace.
#define ISTTY
Definition: Progressbar.hpp:48
void PrintProgressbar(const int position, const int goal, const string message)
Prints a progressbar.
Definition: Progressbar.hpp:69