-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
101 lines (82 loc) · 1.71 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* =====================================================================================
*
* Filename: utils.cpp
*
* Description:
*
* Version: 1.0
* Created: 2013-01-31 17:16:06
* Revision: none
* Compiler: gcc
*
* Author: Didzis Gosko (dg), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "utils.hpp"
using namespace std;
void outputDuration(double duration)
{
cout << setprecision(7) << /* defaultfloat << */ duration << " s";
if(duration > 60)
{
cout << fixed;
if(duration < 600)
cout << setprecision(2);
else
cout << setprecision(1);
cout << " = " << duration/60.0 << " min";
if(duration > 3600)
{
cout << setprecision(2);
cout << " = " << duration/3600.0 << " h";
}
}
}
void RollbackOutput::append(const string& s)
{
cout << s;
cout.flush();
rollbackSize += s.size();
}
void RollbackOutput::replace(const string& s)
{
rollback();
cout << s;
cout.flush();
rollbackSize = s.size();
}
void RollbackOutput::rollback()
{
if(rollbackSize > 0)
{
if(fillSymbol)
{
// rollback for fill
cout << "\033[" << rollbackSize << "D";
// remove (fill with fillSymbol)
cout << setfill(fillSymbol) << setw(rollbackSize) << " ";
}
// rollback
cout << "\033[" << rollbackSize << "D";
rollbackSize = 0;
}
}
void ProgressIndicator::display()
{
_prevDisplay = _current;
char buf[20];
double percents = ((double)_current)/_total * 100.0;
sprintf(buf, "%.01f %%", percents);
output = buf;
output += postfix;
}
void ProgressIndicator::clear()
{
output.rollback();
}