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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
/* ######################################################################
OpProgress - Operation Progress
##################################################################### */
/*}}}*/
// Include Files /*{{{*/
#include <config.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/progress.h>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <sys/time.h>
#include <apti18n.h>
/*}}}*/
using namespace std;
// OpProgress::OpProgress - Constructor /*{{{*/
// ---------------------------------------------------------------------
/* */
OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
LastPercent(0), Percent(0)
{
memset(&LastTime,0,sizeof(LastTime));
}
/*}}}*/
// OpProgress::Progress - Sub progress with no state change /*{{{*/
// ---------------------------------------------------------------------
/* Current is the Base Overall progress in units of Total. Cur is the sub
progress in units of SubTotal. Size is a scaling factor that says what
percent of Total SubTotal is. */
void OpProgress::Progress(unsigned long long Cur)
{
if (Total == 0 || Size == 0 || SubTotal == 0)
Percent = 0;
else
Percent = (Current + Cur/((double)SubTotal)*Size)*100.0/Total;
Update();
}
/*}}}*/
// OpProgress::OverallProgress - Set the overall progress /*{{{*/
// ---------------------------------------------------------------------
/* */
void OpProgress::OverallProgress(unsigned long long Current, unsigned long long Total,
unsigned long long Size,const string &Op)
{
this->Current = Current;
this->Total = Total;
this->Size = Size;
this->Op = Op;
SubOp = string();
if (Total == 0)
Percent = 0;
else
Percent = Current*100.0/Total;
Update();
}
/*}}}*/
// OpProgress::SubProgress - Set the sub progress state /*{{{*/
// ---------------------------------------------------------------------
/* */
void OpProgress::SubProgress(unsigned long long SubTotal,const string &Op,
float const Percent)
{
this->SubTotal = SubTotal;
if (Op.empty() == false)
SubOp = Op;
if (Total == 0 || Percent == 0)
this->Percent = 0;
else if (Percent != -1)
this->Percent = this->Current += (Size*Percent)/SubTotal;
else
this->Percent = Current*100.0/Total;
Update();
}
/*}}}*/
// OpProgress::CheckChange - See if the display should be updated /*{{{*/
// ---------------------------------------------------------------------
/* Progress calls are made so frequently that if every one resulted in
an update the display would be swamped and the system much slower.
This provides an upper bound on the update rate. */
bool OpProgress::CheckChange(float Interval)
{
// For absolute progress, we assume every call is relevant.
if (_config->FindB("APT::Internal::OpProgress::Absolute", false))
return true;
// New major progress indication
if (Op != LastOp)
{
MajorChange = true;
LastOp = Op;
return true;
}
MajorChange = false;
if (SubOp != LastSubOp)
{
LastSubOp = SubOp;
return true;
}
if (std::lround(LastPercent) == std::lround(Percent))
return false;
LastPercent = Percent;
if (Interval == 0)
return false;
// Check time delta
auto const Now = std::chrono::steady_clock::now().time_since_epoch();
auto const Now_sec = std::chrono::duration_cast<std::chrono::seconds>(Now);
auto const Now_usec = std::chrono::duration_cast<std::chrono::microseconds>(Now - Now_sec);
struct timeval NowTime = { static_cast<time_t>(Now_sec.count()), static_cast<suseconds_t>(Now_usec.count()) };
std::chrono::duration<decltype(Interval)> Delta =
std::chrono::seconds(NowTime.tv_sec - LastTime.tv_sec) +
std::chrono::microseconds(NowTime.tv_usec - LastTime.tv_usec);
if (Delta.count() < Interval)
return false;
LastTime = NowTime;
return true;
}
/*}}}*/
// OpTextProgress::OpTextProgress - Constructor /*{{{*/
// ---------------------------------------------------------------------
/* */
OpTextProgress::OpTextProgress(Configuration &Config) :
NoUpdate(false), NoDisplay(false), LastLen(0)
{
if (Config.FindI("quiet",0) >= 1 || Config.FindB("quiet::NoUpdate", false) == true)
NoUpdate = true;
if (Config.FindI("quiet",0) >= 2 || Config.FindB("quiet::NoProgress", false) == true)
NoDisplay = true;
}
/*}}}*/
// OpTextProgress::Done - Clean up the display /*{{{*/
// ---------------------------------------------------------------------
/* */
void OpTextProgress::Done()
{
if (NoUpdate == false && OldOp.empty() == false)
{
char S[300] = {};
if (_error->PendingError() == true)
snprintf(S,sizeof(S),_("%c%s... Error!"),'\r',OldOp.c_str());
else if (not _config->FindB("APT::Internal::OpProgress::EraseLines", _config->FindI("APT::Output-Version") >= 30))
snprintf(S,sizeof(S),_("%c%s... Done"),'\r',OldOp.c_str());
Write(S);
// FIXME: apt-cdrom relies on this end of line being printed
if (_error->PendingError() || not _config->FindB("APT::Internal::OpProgress::EraseLines", _config->FindI("APT::Output-Version") >= 30))
cout << endl;
OldOp = string();
}
if (NoUpdate == true && NoDisplay == false && OldOp.empty() == false)
{
OldOp = string();
cout << endl;
}
}
/*}}}*/
// OpTextProgress::Update - Simple text spinner /*{{{*/
// ---------------------------------------------------------------------
/* */
void OpTextProgress::Update()
{
if (CheckChange((NoUpdate == true?0:0.7)) == false)
return;
// No percent spinner
if (NoUpdate == true)
{
if (MajorChange == false)
return;
if (NoDisplay == false)
{
if (OldOp.empty() == false)
cout << endl;
OldOp = "a";
cout << Op << _("...") << flush;
}
return;
}
// Erase the old text and 'log' the event
char S[300];
if (MajorChange == true && OldOp.empty() == false)
{
snprintf(S,sizeof(S),"\r%s",OldOp.c_str());
Write(S);
if (_config->FindB("APT::Internal::OpProgress::EraseLines", _config->FindI("APT::Output-Version") >= 30))
cout << endl;
}
// Print the spinner. Absolute progress shows us a time progress.
if (_config->FindB("APT::Internal::OpProgress::Absolute", false) && Total != -1llu)
snprintf(S, sizeof(S), _("%c%s... %llu/%llus"), '\r', Op.c_str(), Current, Total);
else if (_config->FindB("APT::Internal::OpProgress::Absolute", false))
snprintf(S, sizeof(S), _("%c%s... %llus"), '\r', Op.c_str(), Current);
else
snprintf(S, sizeof(S), _("%c%s... %u%%"), '\r', Op.c_str(), (unsigned int)Percent);
Write(S);
OldOp = Op;
}
/*}}}*/
// OpTextProgress::Write - Write the progress string /*{{{*/
// ---------------------------------------------------------------------
/* This space fills the end to overwrite the previous text */
void OpTextProgress::Write(const char *S)
{
cout << S;
for (unsigned int I = strlen(S); I < LastLen; I++)
cout << ' ';
cout << '\r' << flush;
LastLen = strlen(S);
}
/*}}}*/
|