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 using namespace std;
52 
53 namespace Nektar
54 {
55 namespace LibUtilities
56 {
57 
58 /**
59  * @brief Prints a progressbar
60  *
61  * @param position State of the current process
62  * @param goal Goal of the current process
63  * @param message Short Description of the current process
64  *
65  * This function plots a simple progressbar to the console or log file to
66  * visualize the current state of an ongoing process. Make sure you minimize
67  * calling this routine. Ideally, this should be called only when the
68  * percentage is increased by an integer.
69  */
70 inline void PrintProgressbar(const int position, const int goal, const string message)
71 {
72  std::cout.unsetf ( std::ios::floatfield );
73  if (ISTTY)
74  {
75  // carriage return
76  cout << "\r";
77 
78  cout << message << ": ";
79  float progress = position / float(goal);
80  cout << setw(3) << ceil(100 * progress) << "% [";
81  for (int j = 0; j < ceil(progress * 49); j++)
82  {
83  cout << "=";
84  }
85  for (int j = ceil(progress * 49); j < 49; j++)
86  {
87  cout << " ";
88  }
89  cout << "]" << flush;
90  }
91  else
92  {
93  // print only every 2 percent
94  if (int(ceil(double(100 * position / goal))) % 2 == 0)
95  {
96  cout << "." << flush;
97  }
98  }
99 }
100 
101 }
102 }
103 
104 #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:70