Skip to content

Commit bdeb751

Browse files
committed
FEAT: support for ANSI escape sequences on Windows also if compiled with Console subsystem
1 parent 639abc3 commit bdeb751

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/os/win32/dev-stdio.c

+30-23
Original file line numberDiff line numberDiff line change
@@ -272,27 +272,28 @@ static void close_stdio(void)
272272

273273
if (Std_Out) {
274274

275-
if (Redir_Out) { // Always UTF-8
276-
ok = WriteFile(Std_Out, req->data, req->length, &total, 0);
277-
if (!ok) {
278-
req->error = GetLastError();
279-
return DR_ERROR;
280-
}
281-
}
282-
else {
283-
// Convert UTF-8 buffer to Win32 wide-char format for console.
284-
// Thankfully, MS provides something other than mbstowcs();
285-
// however, if our buffer overflows, it's an error. There's no
286-
// efficient way at this level to split-up the input data,
287-
// because its UTF-8 with variable char sizes.
288-
bp = req->data;
289-
ep = bp + req->length;
290-
291-
do {
292-
//from some reason, I must decrement the tail pointer in function bellow,
293-
//else escape char is found past the end and processed in rare cases - like in console: do [help] do [help func]
294-
//It looks dangerous, but it should be safe as it looks the req->length is always at least 1.
295-
cp = Skip_To_Char(bp, ep-1, (REBYTE)27); //find ANSI escape char "^["
275+
bp = req->data;
276+
ep = bp + req->length;
277+
do {
278+
//from some reason, I must decrement the tail pointer in function bellow,
279+
//else escape char is found past the end and processed in rare cases - like in console: do [help] do [help func]
280+
//It looks dangerous, but it should be safe as it looks the req->length is always at least 1.
281+
cp = Skip_To_Char(bp, ep-1, (REBYTE)27); //find ANSI escape char "^["
282+
283+
if (Redir_Out) { // Always UTF-8 for Console app
284+
if (cp){
285+
ok = WriteFile(Std_Out, bp, cp - bp, &total, 0);
286+
bp = Parse_ANSI_sequence(++cp, ep);
287+
} else {
288+
ok = WriteFile(Std_Out, bp, ep - bp, &total, 0);
289+
bp = ep;
290+
}
291+
} else { // for Windows app
292+
// Convert UTF-8 buffer to Win32 wide-char format for console.
293+
// Thankfully, MS provides something other than mbstowcs();
294+
// however, if our buffer overflows, it's an error. There's no
295+
// efficient way at this level to split-up the input data,
296+
// because its UTF-8 with variable char sizes.
296297

297298
//if found, write to the console content before it starts, else everything
298299
if (cp){
@@ -312,8 +313,14 @@ static void close_stdio(void)
312313
if (cp) {
313314
bp = Parse_ANSI_sequence(++cp, ep);
314315
}
315-
} while (bp < ep);
316-
}
316+
}
317+
318+
if (!ok) {
319+
req->error = GetLastError();
320+
return DR_ERROR;
321+
}
322+
323+
} while (bp < ep);
317324

318325
req->actual = req->length; // do not use "total" (can be byte or wide)
319326

0 commit comments

Comments
 (0)