From c285a1585ea03b4f3ecead4a034adcf94a2a3050 Mon Sep 17 00:00:00 2001 From: Jessica Hamilton Date: Fri, 15 Apr 2022 22:20:25 +0000 Subject: [PATCH] Terminal: parse foreground+background in a single SGR escape. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Whilst testing `starship`, it was apparent that setting both the foreground & background colours in a single SGR escape is deemed a valid escape sequence. * E.g. `ESC[48;2;58;149;199;38;2;47;121;161m` would set the background to RGB(58,149,199) and foreground to RGB(47,121,161). Change-Id: I3c14afe2ddf673c85d1678a01e7258587b17f21b Reviewed-on: https://review.haiku-os.org/c/haiku/+/5210 Reviewed-by: Jérôme Duval Tested-by: Commit checker robot --- src/apps/terminal/TermParse.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/apps/terminal/TermParse.cpp b/src/apps/terminal/TermParse.cpp index c99e7c1726..adcb2359a5 100644 --- a/src/apps/terminal/TermParse.cpp +++ b/src/apps/terminal/TermParse.cpp @@ -836,12 +836,16 @@ TermParse::EscParse() case 38: { - if (nparam == 3 && param[1] == 5) - attributes.SetIndexedForeground(param[2]); - else if (nparam == 5 && param[1] == 2) - attributes.SetDirectForeground(param[2], param[3], param[4]); + if (nparam >= 3 && param[row+1] == 5) { + attributes.SetIndexedForeground(param[row+2]); + row += 2; + } else if (nparam >= 5 && param[row+1] == 2) { + attributes.SetDirectForeground(param[row+2], param[row+3], param[row+4]); + row += 4; + } else { + row = nparam; // force exit of the parsing + } - row = nparam; // force exit of the parsing break; } @@ -871,12 +875,16 @@ TermParse::EscParse() case 48: { - if (nparam == 3 && param[1] == 5) - attributes.SetIndexedBackground(param[2]); - else if (nparam == 5 && param[1] == 2) - attributes.SetDirectBackground(param[2], param[3], param[4]); + if (nparam >= 3 && param[row+1] == 5) { + attributes.SetIndexedBackground(param[row+2]); + row += 2; + } else if (nparam >= 5 && param[row+1] == 2) { + attributes.SetDirectBackground(param[row+2], param[row+3], param[row+4]); + row += 4; + } else { + row = nparam; // force exit of the parsing + } - row = nparam; // force exit of the parsing break; }