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