forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutionProgress.cpp
More file actions
369 lines (322 loc) · 10.9 KB
/
ExecutionProgress.cpp
File metadata and controls
369 lines (322 loc) · 10.9 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "ExecutionProgress.h"
namespace AppInstaller::CLI::Execution
{
using namespace Settings;
using namespace VirtualTerminal;
using namespace std::string_view_literals;
namespace
{
struct BytesFormatData
{
uint64_t PowerOfTwo;
std::string_view Name;
};
BytesFormatData s_bytesFormatData[] =
{
// Multi-terabyate installers should be fairly rare for the foreseeable future...
{ 40, "TB"sv },
{ 30, "GB"sv },
{ 20, "MB"sv },
{ 10, "KB"sv },
{ 0, "B"sv },
};
const BytesFormatData& GetFormatForSize(uint64_t bytes)
{
for (const auto& format : s_bytesFormatData)
{
if (bytes > (1ull << format.PowerOfTwo))
{
return format;
}
}
// Just to make the compiler happy, return the last in the list if we get here.
return s_bytesFormatData[ARRAYSIZE(s_bytesFormatData) - 1];
}
void OutputBytes(std::ostream& out, uint64_t byteCount)
{
const BytesFormatData& bfd = GetFormatForSize(byteCount);
uint64_t integralAmount = byteCount >> bfd.PowerOfTwo;
uint64_t remainder = byteCount & ((1ull << bfd.PowerOfTwo) - 1);
size_t remainderDigits = 0;
if (integralAmount < 10)
{
remainder *= 100;
remainderDigits = 2;
}
else if (integralAmount < 100)
{
remainder *= 10;
remainderDigits = 1;
}
else if (integralAmount < 1000)
{
// Put an extra space to ensure a consistent 4 chars per numeric output
out << ' ';
}
out << integralAmount;
if (remainderDigits)
{
remainder = remainder >> bfd.PowerOfTwo;
out << '.' << std::setw(remainderDigits) << std::setfill('0') << remainder;
}
out << ' ' << bfd.Name;
}
void SetColor(std::ostream& out, const TextFormat::Color& color, bool enabled)
{
if (enabled)
{
out << TextFormat::Foreground::Extended(color);
}
else
{
constexpr uint8_t divisor = 3;
auto reduced = color;
reduced.R /= divisor;
reduced.G /= divisor;
reduced.B /= divisor;
out << TextFormat::Foreground::Extended(reduced);
}
}
void SetRainbowColor(std::ostream& out, size_t i, size_t max, bool enabled)
{
TextFormat::Color rainbow[] =
{
{ 0xff, 0x00, 0x00 },
{ 0xff, 0x77, 0x00 },
{ 0xff, 0xdd, 0x00 },
{ 0x00, 0xff, 0x00 },
{ 0x00, 0x00, 0xff },
{ 0x8a, 0x2b, 0xe2 },
{ 0xc7, 0x7d, 0xf3 },
};
double target = (static_cast<double>(i) / (max - 1)) * (ARRAYSIZE(rainbow) - 1);
size_t lower = static_cast<size_t>(std::floor(target));
const auto& lowerVal = rainbow[lower];
TextFormat::Color result;
if (lower == (ARRAYSIZE(rainbow) - 1))
{
result = lowerVal;
}
else
{
double upperContribution = target - lower;
#define AICLI_AVERAGE(v) static_cast<uint8_t>(((lowerVal.v * (1.0 - upperContribution)) + (rainbow[lower + 1].v * upperContribution)))
result = { AICLI_AVERAGE(R), AICLI_AVERAGE(G), AICLI_AVERAGE(B) };
}
SetColor(out, result, enabled);
}
}
namespace details
{
void ProgressVisualizerBase::ApplyStyle(size_t i, size_t max, bool enabled)
{
if (!UseVT())
{
// Either no style set or VT disabled
return;
}
switch (m_style)
{
case VisualStyle::Retro:
if (enabled)
{
m_out << TextFormat::Default;
}
else
{
m_out << TextFormat::Negative;
}
break;
case VisualStyle::Accent:
SetColor(m_out, TextFormat::Color::GetAccentColor(), enabled);
break;
case VisualStyle::Rainbow:
SetRainbowColor(m_out, i, max, enabled);
break;
default:
LOG_HR(E_UNEXPECTED);
}
}
}
void IndefiniteSpinner::ShowSpinner()
{
if (!m_spinnerJob.valid() && !m_spinnerRunning && !m_canceled)
{
m_spinnerRunning = true;
m_spinnerJob = std::async(std::launch::async, (UseVT() ? &IndefiniteSpinner::ShowSpinnerInternalWithVT : &IndefiniteSpinner::ShowSpinnerInternalNoVT), this);
}
}
void IndefiniteSpinner::StopSpinner()
{
if (!m_canceled && m_spinnerJob.valid() && m_spinnerRunning)
{
m_canceled = true;
m_spinnerJob.get();
}
}
void IndefiniteSpinner::ShowSpinnerInternalNoVT()
{
char spinnerChars[] = { '-', '\\', '|', '/' };
// First wait for a small amount of time to enable a fast task to skip
// showing anything, or a progress task to skip straight to progress.
Sleep(100);
// Indent two spaces for the spinner, but three here so that we can overwrite it in the loop.
m_out << " ";
for (size_t i = 0; !m_canceled; ++i)
{
constexpr size_t repetitionCount = 20;
ApplyStyle(i % repetitionCount, repetitionCount, true);
m_out << '\b' << spinnerChars[i % ARRAYSIZE(spinnerChars)] << std::flush;
Sleep(250);
}
m_out << "\b \r";
m_canceled = false;
m_spinnerRunning = false;
}
void IndefiniteSpinner::ShowSpinnerInternalWithVT()
{
// Nothing special to do at the moment, can use NoVT version.
ShowSpinnerInternalNoVT();
}
void ProgressBar::ShowProgress(uint64_t current, uint64_t maximum, ProgressType type)
{
if (current < m_lastCurrent)
{
ClearLine();
}
if (UseVT())
{
ShowProgressWithVT(current, maximum, type);
}
else
{
ShowProgressNoVT(current, maximum, type);
}
m_lastCurrent = current;
m_isVisible = true;
}
void ProgressBar::EndProgress(bool hideProgressWhenDone)
{
if (m_isVisible)
{
if (hideProgressWhenDone)
{
ClearLine();
}
else
{
m_out << std::endl;
}
m_isVisible = false;
}
}
void ProgressBar::ClearLine()
{
if (UseVT())
{
m_out << TextModification::EraseLineEntirely << '\r';
}
else
{
// Best effort when no VT (arbitrary number of spaces that seems to work)
m_out << "\r \r";
}
}
void ProgressBar::ShowProgressNoVT(uint64_t current, uint64_t maximum, ProgressType type)
{
m_out << "\r ";
if (maximum)
{
const char* const blockOn = u8"\x2588";
const char* const blockOff = u8"\x2592";
constexpr size_t blockWidth = 30;
double percentage = static_cast<double>(current) / maximum;
size_t blocksOn = static_cast<size_t>(std::floor(percentage * blockWidth));
for (size_t i = 0; i < blocksOn; ++i)
{
m_out << blockOn;
}
for (size_t i = 0; i < blockWidth - blocksOn; ++i)
{
m_out << blockOff;
}
m_out << " ";
switch (type)
{
case AppInstaller::ProgressType::Bytes:
OutputBytes(m_out, current);
m_out << " / ";
OutputBytes(m_out, maximum);
break;
case AppInstaller::ProgressType::Percent:
default:
m_out << static_cast<int>(percentage * 100) << '%';
break;
}
}
else
{
switch (type)
{
case AppInstaller::ProgressType::Bytes:
OutputBytes(m_out, current);
break;
case AppInstaller::ProgressType::Percent:
m_out << current << '%';
break;
default:
m_out << current << " unknowns";
break;
}
}
}
void ProgressBar::ShowProgressWithVT(uint64_t current, uint64_t maximum, ProgressType type)
{
m_out << "\r ";
if (maximum)
{
const char* const blockOn = u8"\x2588";
constexpr size_t blockWidth = 30;
double percentage = static_cast<double>(current) / maximum;
size_t blocksOn = static_cast<size_t>(std::floor(percentage * blockWidth));
TextFormat::Color accent = TextFormat::Color::GetAccentColor();
for (size_t i = 0; i < blockWidth; ++i)
{
ApplyStyle(i, blockWidth, i < blocksOn);
m_out << blockOn;
}
m_out << TextFormat::Default;
m_out << " ";
switch (type)
{
case AppInstaller::ProgressType::Bytes:
OutputBytes(m_out, current);
m_out << " / ";
OutputBytes(m_out, maximum);
break;
case AppInstaller::ProgressType::Percent:
default:
m_out << static_cast<int>(percentage * 100) << '%';
break;
}
}
else
{
switch (type)
{
case AppInstaller::ProgressType::Bytes:
OutputBytes(m_out, current);
break;
case AppInstaller::ProgressType::Percent:
m_out << current << '%';
break;
default:
m_out << current << " unknowns";
break;
}
}
}
}