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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
// Copyright Epic Games, Inc. All Rights Reserved.
// Zen command line client utility
//
#include "consoleprogress.h"
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/timer.h>
#include <zencore/windows.h>
#include <zenutil/consoletui.h>
#include <zenutil/progress.h>
#if !ZEN_PLATFORM_WINDOWS
# include <csignal>
#endif
ZEN_THIRD_PARTY_INCLUDES_START
#include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
//////////////////////////////////////////////////////////////////////////
namespace zen {
// Global tracking for scroll region cleanup on abnormal termination (Ctrl+C etc.)
// Only one ProgressBar can own a scroll region at a time.
static std::atomic<class ConsoleProgressBar*> g_ActiveScrollRegionOwner{nullptr};
static std::atomic<uint32_t> g_ActiveScrollRegionRows{0};
static void
ResetScrollRegionRaw()
{
// Signal-safe: emit raw escape sequences to restore terminal state.
// These are async-signal-safe on POSIX (write()) and safe in console
// ctrl handlers on Windows (WriteConsole is allowed).
uint32_t Rows = g_ActiveScrollRegionRows.load(std::memory_order_acquire);
if (Rows >= 3)
{
// Move to status line, erase it, reset scroll region, move cursor to end of content
TuiMoveCursor(Rows, 1);
TuiEraseLine();
TuiResetScrollRegion();
TuiMoveCursor(Rows - 1, 1);
}
else
{
TuiResetScrollRegion();
}
TuiShowCursor(true);
TuiFlush();
}
#if ZEN_PLATFORM_WINDOWS
static BOOL WINAPI
ScrollRegionCtrlHandler(DWORD CtrlType)
{
if (CtrlType == CTRL_C_EVENT || CtrlType == CTRL_BREAK_EVENT)
{
ResetScrollRegionRaw();
}
// Return FALSE so the default handler (process termination) still runs
return FALSE;
}
#else
static struct sigaction s_PrevSigIntAction;
static struct sigaction s_PrevSigTermAction;
static void
ScrollRegionSignalHandler(int Signal)
{
ResetScrollRegionRaw();
// Re-raise with the previous handler
struct sigaction* PrevAction = (Signal == SIGINT) ? &s_PrevSigIntAction : &s_PrevSigTermAction;
sigaction(Signal, PrevAction, nullptr);
raise(Signal);
}
#endif
static void
InstallScrollRegionCleanupHandler()
{
#if ZEN_PLATFORM_WINDOWS
SetConsoleCtrlHandler(ScrollRegionCtrlHandler, TRUE);
#else
struct sigaction Action = {};
Action.sa_handler = ScrollRegionSignalHandler;
Action.sa_flags = SA_RESETHAND; // one-shot
sigemptyset(&Action.sa_mask);
sigaction(SIGINT, &Action, &s_PrevSigIntAction);
sigaction(SIGTERM, &Action, &s_PrevSigTermAction);
#endif
}
static void
RemoveScrollRegionCleanupHandler()
{
#if ZEN_PLATFORM_WINDOWS
SetConsoleCtrlHandler(ScrollRegionCtrlHandler, FALSE);
#else
sigaction(SIGINT, &s_PrevSigIntAction, nullptr);
sigaction(SIGTERM, &s_PrevSigTermAction, nullptr);
#endif
}
#if ZEN_PLATFORM_WINDOWS
static HANDLE
GetConsoleHandle()
{
static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
return hStdOut;
}
#endif
static void
OutputToConsoleRaw(const char* String, size_t Length)
{
#if ZEN_PLATFORM_WINDOWS
HANDLE hStdOut = GetConsoleHandle();
if (TuiIsStdoutTty())
{
WriteConsoleA(hStdOut, String, (DWORD)Length, 0, 0);
}
else
{
::WriteFile(hStdOut, (LPCVOID)String, (DWORD)Length, 0, 0);
}
#else
fwrite(String, 1, Length, stdout);
#endif
}
static void
OutputToConsoleRaw(const std::string& String)
{
OutputToConsoleRaw(String.c_str(), String.length());
}
static void
OutputToConsoleRaw(const StringBuilderBase& SB)
{
OutputToConsoleRaw(SB.c_str(), SB.Size());
}
static uint32_t
GetUpdateDelayMS(ConsoleProgressMode InMode)
{
switch (InMode)
{
case ConsoleProgressMode::Plain:
return 5000;
case ConsoleProgressMode::Pretty:
return 200;
case ConsoleProgressMode::Log:
return 2000;
case ConsoleProgressMode::Quiet:
return 5000;
default:
ZEN_ASSERT(false);
return 0;
}
}
class ConsoleProgressBar : public ProgressBase::ProgressBar
{
public:
explicit ConsoleProgressBar(ConsoleProgressMode InMode, std::string_view InSubTask);
~ConsoleProgressBar();
void UpdateState(const State& NewState, bool DoLinebreak) override;
void ForceLinebreak() override;
void Finish() override;
private:
void SetupScrollRegion();
void TeardownScrollRegion();
void RenderStatusLine(std::string_view Line);
const ConsoleProgressMode m_Mode;
Stopwatch m_SW;
uint64_t m_LastUpdateMS;
uint64_t m_PausedMS;
State m_State;
const std::string m_SubTask;
size_t m_LastOutputLength = 0;
bool m_ScrollRegionActive = false;
uint32_t m_ScrollRegionRows = 0;
};
ConsoleProgressBar::ConsoleProgressBar(ConsoleProgressMode InMode, std::string_view InSubTask)
: m_Mode((!TuiIsStdoutTty() && InMode == ConsoleProgressMode::Pretty) ? ConsoleProgressMode::Plain : InMode)
, m_LastUpdateMS((uint64_t)-1)
, m_PausedMS(0)
, m_SubTask(InSubTask)
{
ZEN_ASSERT(InSubTask.find('\"') == std::string_view::npos);
if (!m_SubTask.empty())
{
if (m_Mode == ConsoleProgressMode::Log)
{
std::string String = fmt::format("@progress push \"{}\"\n", m_SubTask);
OutputToConsoleRaw(String);
}
}
if (m_Mode == ConsoleProgressMode::Pretty)
{
SetupScrollRegion();
}
}
ConsoleProgressBar::~ConsoleProgressBar()
{
try
{
TeardownScrollRegion();
ForceLinebreak();
if (!m_SubTask.empty())
{
if (m_Mode == ConsoleProgressMode::Log)
{
const std::string String("@progress pop\n");
OutputToConsoleRaw(String);
}
}
}
catch (const std::exception& Ex)
{
ZEN_ERROR("ConsoleProgressBar::~ConsoleProgressBar() failed with {}", Ex.what());
}
}
void
ConsoleProgressBar::SetupScrollRegion()
{
// Only one scroll region owner at a time; nested bars fall back to the inline \r path.
if (g_ActiveScrollRegionOwner.load(std::memory_order_acquire) != nullptr)
{
return;
}
uint32_t Rows = TuiConsoleRows(0);
if (Rows < 3)
{
return;
}
TuiEnableOutput();
// Ensure cursor is not on the last row before we install the region.
// Print a newline to push content up if needed, then set the region.
OutputToConsoleRaw("\n", 1);
TuiSetScrollRegion(1, Rows - 1);
// Move cursor into the scroll region so normal output stays there
TuiMoveCursor(Rows - 1, 1);
m_ScrollRegionActive = true;
m_ScrollRegionRows = Rows;
g_ActiveScrollRegionRows.store(Rows, std::memory_order_release);
g_ActiveScrollRegionOwner.store(this, std::memory_order_release);
InstallScrollRegionCleanupHandler();
}
void
ConsoleProgressBar::TeardownScrollRegion()
{
if (!m_ScrollRegionActive)
{
return;
}
m_ScrollRegionActive = false;
RemoveScrollRegionCleanupHandler();
g_ActiveScrollRegionOwner.store(nullptr, std::memory_order_release);
g_ActiveScrollRegionRows.store(0, std::memory_order_release);
// Emit all teardown escape sequences as a single atomic write
ExtendableStringBuilder<128> Buf;
Buf << fmt::format("\x1b[{};1H", m_ScrollRegionRows) // move to status line
<< "\x1b[2K" // erase it
<< "\x1b[r" // reset scroll region
<< fmt::format("\x1b[{};1H", m_ScrollRegionRows - 1); // move to end of content
OutputToConsoleRaw(Buf);
TuiFlush();
}
void
ConsoleProgressBar::RenderStatusLine(std::string_view Line)
{
// Handle terminal resizes by re-querying row count
uint32_t CurrentRows = TuiConsoleRows(0);
if (CurrentRows >= 3 && CurrentRows != m_ScrollRegionRows)
{
// Terminal was resized - reinstall scroll region
TuiSetScrollRegion(1, CurrentRows - 1);
m_ScrollRegionRows = CurrentRows;
}
// Build the entire escape sequence as a single string so the console write
// is atomic and log output from other threads cannot interleave.
ExtendableStringBuilder<512> Buf;
Buf << "\x1b"
"7" // ESC 7 - save cursor
<< fmt::format("\x1b[{};1H", m_ScrollRegionRows) // move to bottom row
<< "\x1b[2K" // erase entire line
<< Line // progress bar content
<< "\x1b"
"8"; // ESC 8 - restore cursor
OutputToConsoleRaw(Buf);
}
void
ConsoleProgressBar::UpdateState(const State& NewState, bool DoLinebreak)
{
ZEN_ASSERT(NewState.TotalCount >= NewState.RemainingCount);
ZEN_ASSERT(NewState.Task.find('\"') == std::string::npos);
if (DoLinebreak == false && m_State == NewState)
{
return;
}
uint64_t ElapsedTimeMS = NewState.OptionalElapsedTime == (uint64_t)-1 ? m_SW.GetElapsedTimeMs() : NewState.OptionalElapsedTime;
if (m_LastUpdateMS != (uint64_t)-1)
{
if (!DoLinebreak && (NewState.Status == m_State.Status) && (NewState.Task == m_State.Task) &&
((m_LastUpdateMS + 200) > ElapsedTimeMS))
{
return;
}
if (m_State.Status == State::EStatus::Paused)
{
uint64_t ElapsedSinceLast = ElapsedTimeMS - m_LastUpdateMS;
m_PausedMS += ElapsedSinceLast;
}
}
m_LastUpdateMS = ElapsedTimeMS;
std::string Task = NewState.Task;
switch (NewState.Status)
{
case State::EStatus::Aborted:
Task = "Aborting";
break;
case State::EStatus::Paused:
Task = "Paused";
break;
default:
break;
}
if (NewState.Task.length() > Task.length())
{
Task += std::string(NewState.Task.length() - Task.length(), ' ');
}
const size_t PercentDone =
NewState.TotalCount > 0u ? gsl::narrow<uint8_t>((100 * (NewState.TotalCount - NewState.RemainingCount)) / NewState.TotalCount) : 0u;
uint64_t Completed = NewState.TotalCount - NewState.RemainingCount;
uint64_t ETAElapsedMS = ElapsedTimeMS - m_PausedMS;
uint64_t ETAMS = ((m_State.TotalCount == NewState.TotalCount) && (NewState.Status == State::EStatus::Running)) && (PercentDone > 5)
? (ETAElapsedMS * NewState.RemainingCount) / Completed
: 0;
const std::string ETAString = (ETAMS > 0) ? fmt::format(" ETA {}", NiceTimeSpanMs(ETAMS)) : "";
if (m_Mode == ConsoleProgressMode::Plain)
{
const std::string Details = (!NewState.Details.empty()) ? fmt::format(": {}", NewState.Details) : "";
const std::string Output = fmt::format("{} {}% {}{}{}\n", Task, PercentDone, NiceTimeSpanMs(ElapsedTimeMS), ETAString, Details);
OutputToConsoleRaw(Output);
m_State = NewState;
}
else if (m_Mode == ConsoleProgressMode::Pretty)
{
size_t ProgressBarSize = 20;
size_t ProgressBarCount = (ProgressBarSize * PercentDone) / 100;
uint32_t ConsoleColumns = TuiConsoleColumns(1024);
const std::string PercentString = fmt::format("{:#3}%", PercentDone);
const std::string ProgressBarString =
fmt::format(": |{}{}|", std::string(ProgressBarCount, '#'), std::string(ProgressBarSize - ProgressBarCount, ' '));
const std::string ElapsedString = fmt::format(": {}", NiceTimeSpanMs(ElapsedTimeMS));
const std::string DetailsString = (!NewState.Details.empty()) ? fmt::format(". {}", NewState.Details) : "";
ExtendableStringBuilder<256> OutputBuilder;
OutputBuilder << Task << " " << PercentString;
if (OutputBuilder.Size() + 1 < ConsoleColumns)
{
size_t RemainingSpace = ConsoleColumns - (OutputBuilder.Size() + 1);
bool ElapsedFits = RemainingSpace >= ElapsedString.length();
RemainingSpace -= ElapsedString.length();
bool ETAFits = ElapsedFits && RemainingSpace >= ETAString.length();
RemainingSpace -= ETAString.length();
bool DetailsFits = ETAFits && RemainingSpace >= DetailsString.length();
RemainingSpace -= DetailsString.length();
bool ProgressBarFits = DetailsFits && RemainingSpace >= ProgressBarString.length();
RemainingSpace -= ProgressBarString.length();
if (ProgressBarFits)
{
OutputBuilder << ProgressBarString;
}
if (ElapsedFits)
{
OutputBuilder << ElapsedString;
}
if (ETAFits)
{
OutputBuilder << ETAString;
}
if (DetailsFits)
{
OutputBuilder << DetailsString;
}
}
if (m_ScrollRegionActive)
{
// Render on the pinned bottom status line
RenderStatusLine(OutputBuilder.ToView());
}
else
{
// Fallback: inline \r-based overwrite (terminal too small for scroll region)
std::string_view Output = OutputBuilder.ToView();
std::string::size_type EraseLength =
m_LastOutputLength > (Output.length() + 1) ? (m_LastOutputLength - Output.length() - 1) : 0;
ExtendableStringBuilder<256> LineToPrint;
if (Output.length() + 1 + EraseLength >= ConsoleColumns)
{
if (m_LastOutputLength > 0)
{
LineToPrint << "\n";
}
LineToPrint << Output;
DoLinebreak = true;
}
else
{
LineToPrint << "\r" << Output << std::string(EraseLength, ' ');
}
if (DoLinebreak)
{
LineToPrint << "\n";
}
OutputToConsoleRaw(LineToPrint);
m_LastOutputLength = DoLinebreak ? 0 : (Output.length() + 1); // +1 for \r prefix
}
m_State = NewState;
}
else if (m_Mode == ConsoleProgressMode::Log)
{
if (m_State.Task != NewState.Task ||
m_State.Details != NewState.Details) // TODO: Should we output just because details change? Will this spam the log collector?
{
std::string Details = (!NewState.Details.empty()) ? fmt::format(": {}", NewState.Details) : "";
for (std::string::value_type& Char : Details)
{
if (Char == '"')
{
Char = '\'';
}
}
const std::string Message =
fmt::format("@progress \"{} {}{}{}\"\n", NewState.Task, NiceTimeSpanMs(ElapsedTimeMS), ETAString, Details);
OutputToConsoleRaw(Message);
}
const size_t OldPercentDone =
m_State.TotalCount > 0u ? gsl::narrow<uint8_t>((100 * (m_State.TotalCount - m_State.RemainingCount)) / m_State.TotalCount) : 0u;
if (OldPercentDone != PercentDone)
{
const std::string Progress = fmt::format("@progress {}%\n", PercentDone);
OutputToConsoleRaw(Progress);
}
m_State = NewState;
}
}
void
ConsoleProgressBar::ForceLinebreak()
{
if (m_LastOutputLength > 0)
{
State NewState = m_State;
UpdateState(NewState, /*DoLinebreak*/ true);
}
}
void
ConsoleProgressBar::Finish()
{
TeardownScrollRegion();
if (m_LastOutputLength > 0 || m_State.RemainingCount > 0)
{
State NewState = m_State;
NewState.RemainingCount = 0;
NewState.Details = "";
UpdateState(NewState, /*DoLinebreak*/ true);
}
m_State = State{};
m_LastOutputLength = 0;
m_SW.Reset();
}
class ConsoleProgress : public ProgressBase
{
public:
ConsoleProgress(ConsoleProgressMode InMode) : m_Mode(InMode) {}
virtual void SetLogOperationName(std::string_view Name) override
{
ZEN_ASSERT(Name.find('\"') == std::string_view::npos);
if (m_Mode == ConsoleProgressMode::Log)
{
std::string String = fmt::format("@progress \"{}\"\n", Name);
OutputToConsoleRaw(String);
}
}
virtual void SetLogOperationProgress(uint32_t StepIndex, uint32_t StepCount) override
{
if (m_Mode == ConsoleProgressMode::Log)
{
const size_t PercentDone = StepCount > 0u ? gsl::narrow<uint8_t>((100 * StepIndex) / StepCount) : 0u;
std::string String = fmt::format("@progress {}%\n", PercentDone);
OutputToConsoleRaw(String);
}
}
virtual void PushLogOperation(std::string_view Name) override
{
ZEN_ASSERT(Name.find('\"') == std::string_view::npos);
if (m_Mode == ConsoleProgressMode::Log)
{
std::string String = fmt::format("@progress push \"{}\"\n", Name);
OutputToConsoleRaw(String);
}
}
virtual void PopLogOperation() override
{
if (m_Mode == ConsoleProgressMode::Log)
{
const std::string String("@progress pop\n");
OutputToConsoleRaw(String);
}
}
virtual uint32_t GetProgressUpdateDelayMS() const override { return GetUpdateDelayMS(m_Mode); }
virtual std::unique_ptr<ProgressBase::ProgressBar> CreateProgressBar(std::string_view InSubTask) override
{
return std::make_unique<ConsoleProgressBar>(m_Mode, InSubTask);
}
private:
ConsoleProgressMode m_Mode;
};
ProgressBase*
CreateConsoleProgress(ConsoleProgressMode InMode)
{
return new ConsoleProgress(InMode);
}
} // namespace zen
|