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
439 lines (378 loc) · 13.7 KB
/
Copy pathExecutionProgress.cpp
File metadata and controls
439 lines (378 loc) · 13.7 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// 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-terabyte 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(BaseStream& 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(BaseStream& out, const TextFormat::Color& color, bool foregroundOnly)
{
out << TextFormat::Foreground::Extended(color);
if (!foregroundOnly)
{
constexpr uint8_t divisor = 3;
auto reduced = color;
reduced.R /= divisor;
reduced.G /= divisor;
reduced.B /= divisor;
out << TextFormat::Background::Extended(reduced);
}
}
void SetRainbowColor(BaseStream& out, size_t i, size_t max, bool foregroundOnly)
{
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, foregroundOnly);
}
}
namespace details
{
void ProgressVisualizerBase::ApplyStyle(size_t i, size_t max, bool foregroundOnly)
{
if (!UseVT())
{
// Either no style set or VT disabled
return;
}
switch (m_style)
{
case VisualStyle::Retro:
m_out << TextFormat::Default;
break;
case VisualStyle::Accent:
SetColor(m_out, TextFormat::Color::GetAccentColor(), foregroundOnly);
break;
case VisualStyle::Rainbow:
SetRainbowColor(m_out, i, max, foregroundOnly);
break;
default:
LOG_HR(E_UNEXPECTED);
}
}
void ProgressVisualizerBase::ClearLine()
{
if (UseVT())
{
m_out << TextModification::EraseLineEntirely << '\r';
}
else
{
m_out << '\r' << std::string(GetConsoleWidth(), ' ') << '\r';
}
}
void ProgressVisualizerBase::Message(std::string_view message)
{
std::atomic_store(&m_message, std::make_shared<Utility::NormalizedString>(message));
}
std::shared_ptr<Utility::NormalizedString> ProgressVisualizerBase::Message()
{
return std::atomic_load(&m_message);
}
}
void IndefiniteSpinner::ShowSpinner()
{
if (!m_spinnerJob.valid() && !m_spinnerRunning && !m_canceled)
{
m_spinnerRunning = true;
m_spinnerJob = std::async(std::launch::async, &IndefiniteSpinner::ShowSpinnerInternal, this);
}
}
void IndefiniteSpinner::StopSpinner()
{
if (!m_canceled && m_spinnerJob.valid() && m_spinnerRunning)
{
m_canceled = true;
m_spinnerJob.get();
}
}
void IndefiniteSpinner::ShowSpinnerInternal()
{
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);
if (!m_canceled)
{
if (UseVT())
{
// Additional VT-based progress reporting, for terminals that support it
m_out << Progress::Construct(Progress::State::Indeterminate);
}
// Indent two spaces for the spinner, but three here so that we can overwrite it in the loop.
std::string_view indent = " ";
std::shared_ptr<Utility::NormalizedString> message = this->Message();
size_t messageLength = message ? Utility::UTF8ColumnWidth(*message) : 0;
for (size_t i = 0; !m_canceled; ++i)
{
constexpr size_t repetitionCount = 20;
ApplyStyle(i % repetitionCount, repetitionCount, true);
m_out << '\r' << indent << spinnerChars[i % ARRAYSIZE(spinnerChars)];
m_out.RestoreDefault();
std::shared_ptr<Utility::NormalizedString> newMessage = this->Message();
std::string eraser;
if (newMessage)
{
size_t newLength = Utility::UTF8ColumnWidth(*newMessage);
if (newLength < messageLength)
{
eraser = std::string(messageLength - newLength, ' ');
}
message = newMessage;
messageLength = newLength;
}
m_out << ' ' << (message ? *message : std::string{}) << eraser << std::flush;
Sleep(250);
}
ClearLine();
if (UseVT())
{
m_out << Progress::Construct(Progress::State::None);
}
}
m_canceled = false;
m_spinnerRunning = false;
}
void ProgressBar::ShowProgress(uint64_t current, uint64_t maximum, ProgressType type)
{
if (current < m_lastCurrent)
{
ClearLine();
}
// TODO: Progress bar does not currently use message
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;
}
if (UseVT())
{
// We always clear the VT-based progress bar, even if hideProgressWhenDone is false
// since it would be confusing for users if progress continues to be shown after winget exits
// (it is typically not automatically cleared by terminals on process exit)
m_out << Progress::Construct(Progress::State::None);
}
m_isVisible = false;
}
}
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 << TextFormat::Default;
m_out << "\r ";
if (maximum)
{
const char* const blocks[] =
{
u8" ", // block off
u8"\x258F", // block 1/8
u8"\x258E", // block 2/8
u8"\x258D", // block 3/8
u8"\x258C", // block 4/8
u8"\x258B", // block 5/8
u8"\x258A", // block 6/8
u8"\x2589", // block 7/8
u8"\x2588" // block on
};
const char* const blockOn = blocks[8];
const char* const blockOff = blocks[0];
constexpr size_t blockWidth = 30;
double percentage = static_cast<double>(current) / maximum;
size_t blocksOn = static_cast<size_t>(std::floor(percentage * blockWidth));
size_t partialBlockIndex = static_cast<size_t>((percentage * blockWidth - blocksOn) * 8);
TextFormat::Color accent = TextFormat::Color::GetAccentColor();
for (size_t i = 0; i < blockWidth; ++i)
{
ApplyStyle(i, blockWidth, false);
if (i < blocksOn)
{
m_out << blockOn;
}
else if (i == blocksOn)
{
m_out << blocks[partialBlockIndex];
}
else
{
m_out << blockOff;
}
}
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;
}
// Additional VT-based progress reporting, for terminals that support it
m_out << Progress::Construct(Progress::State::Normal, static_cast<int>(percentage * 100));
}
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;
}
}
}
}