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 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 //
29 // Description: Print a simple progress bar
30 //
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 #ifndef NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
34 #define NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
35 
36 #include <stdio.h>
37 #include <iostream>
38 #include <iomanip>
39 #include <string>
40 #include <cmath>
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 namespace Nektar
51 {
52 namespace LibUtilities
53 {
54 
55 /**
56  * @brief Prints a progressbar
57  *
58  * @param position State of the current process
59  * @param goal Goal of the current process
60  * @param message Short Description of the current process
61  *
62  * This function plots a simple progressbar to the console or log file to
63  * visualize the current state of an ongoing process. Make sure you minimize
64  * calling this routine. Ideally, this should be called only when the
65  * percentage is increased by an integer.
66  */
67 inline int PrintProgressbar(
68  const int position, const int goal, const std::string message,
69  int lastprogress = -1)
70 {
71  std::cout.unsetf ( std::ios::floatfield );
72  if (ISTTY)
73  {
74  float progress = position / float(goal);
75  int numeq = static_cast<int>(ceil(progress *49));
76  if(lastprogress == numeq)
77  {
78  return numeq;
79  }
80  else
81  {
82  // carriage return
83  std::cout << "\r";
84 
85  std::cout << message << ": ";
86 
87  std::cout << std::setw(3) << ceil(100 * progress) << "% [";
88  for (int j = 0; j < numeq; j++)
89  {
90  std::cout << "=";
91  }
92  for (int j = numeq; j < 49; j++)
93  {
94  std::cout << " ";
95  }
96  std::cout << "]" << std::flush;
97  return numeq;
98  }
99  }
100  else
101  {
102  // print only every 2 percent
103  if (int(ceil(double(100 * position / goal))) % 2 == 0)
104  {
105  std::cout << "." << std::flush;
106  }
107  return -1;
108  }
109 }
110 
111 }
112 }
113 
114 #endif // NEKTAR_LIBUTILITIES_PROGRESSBAR_HPP
#define ISTTY
Definition: Progressbar.hpp:47
int PrintProgressbar(const int position, const int goal, const std::string message, int lastprogress=-1)
Prints a progressbar.
Definition: Progressbar.hpp:67
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1