Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 int PrintProgressbar(const int position, const int goal, const string message,
70  int lastprogress = -1)
71 {
72  std::cout.unsetf ( std::ios::floatfield );
73  if (ISTTY)
74  {
75  float progress = position / float(goal);
76  int numeq = ceil(progress *49);
77  if(lastprogress == numeq)
78  {
79  return numeq;
80  }
81  else
82  {
83  // carriage return
84  cout << "\r";
85 
86  cout << message << ": ";
87 
88  cout << setw(3) << ceil(100 * progress) << "% [";
89  for (int j = 0; j < numeq; j++)
90  {
91  cout << "=";
92  }
93  for (int j = numeq; j < 49; j++)
94  {
95  cout << " ";
96  }
97  cout << "]" << flush;
98  return numeq;
99  }
100  }
101  else
102  {
103  // print only every 2 percent
104  if (int(ceil(double(100 * position / goal))) % 2 == 0)
105  {
106  cout << "." << flush;
107  }
108  return -1;
109  }
110 }
111 
112 }
113 }
114 
115 #endif // NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
int PrintProgressbar(const int position, const int goal, const string message, int lastprogress=-1)
Prints a progressbar.
Definition: Progressbar.hpp:69
STL namespace.
#define ISTTY
Definition: Progressbar.hpp:48