Skip to content

Commit efd67e6

Browse files
Nit fixes for cleanup
1 parent d421da5 commit efd67e6

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

src/windows/wslc/services/BuildImageCallback.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ namespace wsl::windows::wslc::services {
1919

2020
using wsl::windows::common::string::MultiByteToWide;
2121

22+
namespace {
23+
constexpr std::wstring_view c_escapeMoveCursorUpAndClear = L"\033[{}A\033[J";
24+
constexpr std::wstring_view c_escapeBrightGreen = L"\033[92m";
25+
constexpr std::wstring_view c_escapeResetAttributes = L"\033[0m";
26+
constexpr std::wstring_view c_escapeHideCursorDim = L"\033[?25l\033[2m";
27+
constexpr std::wstring_view c_escapeClearLineAndNewline = L"\033[K\n";
28+
constexpr std::wstring_view c_escapeUndimShowCursor = L"\033[22m\033[?25h";
29+
} // namespace
30+
2231
BuildImageCallback::~BuildImageCallback()
2332
try
2433
{
@@ -58,7 +67,7 @@ void BuildImageCallback::CollapseWindow()
5867
{
5968
if (m_displayedLines > 0)
6069
{
61-
WriteTerminal(std::format(L"\033[{}A\033[J", m_displayedLines));
70+
WriteTerminal(std::format(c_escapeMoveCursorUpAndClear, m_displayedLines));
6271
m_displayedLines = 0;
6372
}
6473

@@ -67,6 +76,16 @@ void BuildImageCallback::CollapseWindow()
6776
m_pullLines.clear();
6877
}
6978

79+
void BuildImageCallback::RedrawIfNeeded()
80+
{
81+
auto now = std::chrono::steady_clock::now();
82+
if (now - m_lastRedraw >= c_redrawInterval)
83+
{
84+
Redraw();
85+
m_lastRedraw = now;
86+
}
87+
}
88+
7089
HRESULT BuildImageCallback::OnProgress(LPCSTR status, LPCSTR id, ULONGLONG current, ULONGLONG total)
7190
try
7291
{
@@ -101,13 +120,7 @@ try
101120
if (isPullProgress)
102121
{
103122
m_pullLines[id] = status;
104-
105-
auto now = std::chrono::steady_clock::now();
106-
if (now - m_lastRedraw >= c_redrawInterval)
107-
{
108-
Redraw();
109-
m_lastRedraw = now;
110-
}
123+
RedrawIfNeeded();
111124

112125
return S_OK;
113126
}
@@ -143,11 +156,9 @@ try
143156
{
144157
// Flush a throttled redraw before clearing so \r-based progress
145158
// updates are visible even when batched in a single OnProgress call.
146-
auto now = std::chrono::steady_clock::now();
147-
if (!m_pendingLine.empty() && now - m_lastRedraw >= c_redrawInterval)
159+
if (!m_pendingLine.empty())
148160
{
149-
Redraw();
150-
m_lastRedraw = now;
161+
RedrawIfNeeded();
151162
}
152163
m_pendingLine.clear();
153164
}
@@ -161,12 +172,7 @@ try
161172
// Throttle redraws to avoid blocking the server's IO loop with console writes
162173
// during rapid output. Lines accumulate in the deque immediately; the display
163174
// catches up at ~20fps.
164-
auto now = std::chrono::steady_clock::now();
165-
if (now - m_lastRedraw >= c_redrawInterval)
166-
{
167-
Redraw();
168-
m_lastRedraw = now;
169-
}
175+
RedrawIfNeeded();
170176

171177
return S_OK;
172178
}
@@ -178,7 +184,7 @@ try
178184
const auto newlines = wide.substr(bodyLength);
179185
wide.resize(bodyLength);
180186

181-
WriteTerminal(std::format(L"\033[92m{}\033[0m{}", wide, newlines));
187+
WriteTerminal(std::format(L"{}{}{}{}", c_escapeBrightGreen, wide, c_escapeResetAttributes, newlines));
182188
return S_OK;
183189
}
184190
CATCH_RETURN();
@@ -208,14 +214,14 @@ void BuildImageCallback::Redraw()
208214
// during the redraw so the user doesn't see it bouncing through the cursor movement,
209215
// then show it again at the final position. The dim attribute (\033[2m) renders the
210216
// scrolling lines de-emphasized regardless of the user's theme.
211-
std::wstring buffer = L"\033[?25l\033[2m";
217+
std::wstring buffer{c_escapeHideCursorDim};
212218

213219
// Move cursor to the start of the display area and erase from there to the end of
214220
// the screen. \033[J handles the case where the new display is shorter than the
215221
// previous one (e.g. when \r clears the pending line without a replacement).
216222
if (m_displayedLines > 0)
217223
{
218-
buffer += std::format(L"\033[{}A\033[J", m_displayedLines);
224+
buffer += std::format(c_escapeMoveCursorUpAndClear, m_displayedLines);
219225
}
220226

221227
auto appendLine = [&](const std::string& line) {
@@ -225,7 +231,7 @@ void BuildImageCallback::Redraw()
225231
wline.resize(consoleWidth);
226232
}
227233
buffer += wline;
228-
buffer += L"\033[K\n";
234+
buffer += c_escapeClearLineAndNewline;
229235
};
230236

231237
// Print completed lines (skip older ones if we need room for the pending line).
@@ -251,7 +257,7 @@ void BuildImageCallback::Redraw()
251257
appendLine(line);
252258
}
253259

254-
buffer += L"\033[22m\033[?25h";
260+
buffer += c_escapeUndimShowCursor;
255261

256262
WriteTerminal(buffer);
257263
m_displayedLines = displayCount;

src/windows/wslc/services/BuildImageCallback.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DECLSPEC_UUID("3EDD5DBF-CA6C-4CF7-923A-AD94B6A732E5") BuildImageCallback
3636

3737
void CollapseWindow();
3838
void Redraw();
39+
void RedrawIfNeeded();
3940
// Use WriteConsoleW directly rather than wprintf: wprintf is noticeably slower for
4041
// the per-redraw scrolling display and produces visible flicker.
4142
void WriteTerminal(std::wstring_view content) const;

0 commit comments

Comments
 (0)