@@ -19,6 +19,15 @@ namespace wsl::windows::wslc::services {
1919
2020using 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+
2231BuildImageCallback::~BuildImageCallback ()
2332try
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+
7089HRESULT BuildImageCallback::OnProgress (LPCSTR status, LPCSTR id, ULONGLONG current, ULONGLONG total)
7190try
7291{
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 }
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 }
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 }
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}
184190CATCH_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;
0 commit comments