Skip to content

Commit 8d04877

Browse files
authored
Fix ANSI escape color codes (#570)
1 parent f7791b5 commit 8d04877

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

libctru/source/console.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,17 @@ static void consoleHandleColorEsc(int code)
366366
}
367367
break;
368368
case ESC_BUILDING_FORMAT_FG:
369-
if (escapeSeq.color.args[0] == 5)
369+
if (code == 5)
370370
escapeSeq.state = ESC_BUILDING_FORMAT_FG_NONRGB;
371-
else if (escapeSeq.color.args[0] == 2)
371+
else if (code == 2)
372372
escapeSeq.state = ESC_BUILDING_FORMAT_FG_RGB;
373373
else
374374
escapeSeq.state = ESC_BUILDING_FORMAT_UNKNOWN;
375375
break;
376376
case ESC_BUILDING_FORMAT_BG:
377-
if (escapeSeq.color.args[0] == 5)
377+
if (code == 5)
378378
escapeSeq.state = ESC_BUILDING_FORMAT_BG_NONRGB;
379-
else if (escapeSeq.color.args[0] == 2)
379+
else if (code == 2)
380380
escapeSeq.state = ESC_BUILDING_FORMAT_BG_RGB;
381381
else
382382
escapeSeq.state = ESC_BUILDING_FORMAT_UNKNOWN;
@@ -448,6 +448,7 @@ static void consoleColorStateShift(void)
448448
if (escapeSeq.hasArg[1])
449449
consoleHandleColorEsc(escapeSeq.color.args[1]);
450450
escapeSeq.argIdx = 0;
451+
escapeSeq.rawBuf[0] = escapeSeq.rawBuf[1] = 0;
451452
escapeSeq.hasArg[0] = escapeSeq.hasArg[1] = false;
452453
break;
453454
case ESC_BUILDING_FORMAT_BG:
@@ -456,11 +457,12 @@ static void consoleColorStateShift(void)
456457
case ESC_BUILDING_FORMAT_BG_NONRGB:
457458
consoleHandleColorEsc(escapeSeq.color.args[0]);
458459
escapeSeq.argIdx = 0;
460+
escapeSeq.rawBuf[0] = escapeSeq.rawBuf[1] = 0;
459461
escapeSeq.hasArg[0] = escapeSeq.hasArg[1] = false;
460462
break;
461463
case ESC_BUILDING_FORMAT_FG_RGB:
462464
case ESC_BUILDING_FORMAT_BG_RGB:
463-
if (escapeSeq.argIdx < 3)
465+
if (escapeSeq.argIdx < 2)
464466
escapeSeq.argIdx++;
465467
else
466468
consoleHandleColorEsc(0); // Nothing passed here because three RGB items
@@ -538,7 +540,7 @@ ssize_t con_write(struct _reent *r,void *fd,const char *ptr, size_t len) {
538540
escapeSeq.rawBuf[escapeSeq.argIdx] = escapeSeq.rawBuf[escapeSeq.argIdx] * 10 + (chr - '0');
539541
break;
540542
case ';':
541-
if (escapeSeq.argIdx < 2)
543+
if (escapeSeq.argIdx < 1)
542544
escapeSeq.argIdx++;
543545
else
544546
consoleColorStateShift();
@@ -836,7 +838,13 @@ void consoleDrawChar(int c) {
836838
}
837839

838840
if (!(currentConsole->flags & CONSOLE_BG_CUSTOM)) {
839-
bg = colorTable[bg];
841+
if (currentConsole->flags & CONSOLE_COLOR_BOLD) {
842+
bg = colorTable[bg + 8];
843+
} else if (currentConsole->flags & CONSOLE_COLOR_FAINT) {
844+
bg = colorTable[bg + 16];
845+
} else {
846+
bg = colorTable[bg];
847+
}
840848
}
841849

842850
if (currentConsole->flags & CONSOLE_COLOR_REVERSE) {

0 commit comments

Comments
 (0)