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 <cmath>
37#include <iomanip>
38#include <iostream>
39#include <stdio.h>
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
50namespace Nektar
51{
52namespace 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 */
67inline int PrintProgressbar(const int position, const int goal,
68 const std::string message, int lastprogress = -1)
69{
70 std::cout.unsetf(std::ios::floatfield);
71 if (ISTTY)
72 {
73 float progress = position / float(goal);
74 int numeq = static_cast<int>(ceil(progress * 49));
75 if (lastprogress == numeq)
76 {
77 return numeq;
78 }
79 else
80 {
81 // carriage return
82 std::cout << "\r";
83
84 std::cout << message << ": ";
85
86 std::cout << std::setw(3) << ceil(100 * progress) << "% [";
87 for (int j = 0; j < numeq; j++)
88 {
89 std::cout << "=";
90 }
91 for (int j = numeq; j < 49; j++)
92 {
93 std::cout << " ";
94 }
95 std::cout << "]" << std::flush;
96 return numeq;
97 }
98 }
99 else
100 {
101 // print only every 2 percent
102 if (int(ceil(double(100 * position / goal))) % 2 == 0)
103 {
104 std::cout << "." << std::flush;
105 }
106 return -1;
107 }
108}
109
110} // namespace LibUtilities
111} // namespace Nektar
112
113#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:2