1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

LibVT: Correct color handling

VT100's documentation says that more than one SGR (Set Graphics
Rendition) parameters may be included in a single escape sequence.
However, we treated those with more than 3 parameters as color
sequences, so this behavior was not replicated.
This commit is contained in:
Daniel Bertalan 2021-05-17 16:33:47 +02:00 committed by Andreas Kling
parent 507ace5174
commit c1e292e5bc

View file

@ -66,7 +66,7 @@ void Terminal::alter_mode(bool should_set, Parameters params, Intermediates inte
dbgln("Terminal: Show Cursor escapecode received. Not needed: ignored."); dbgln("Terminal: Show Cursor escapecode received. Not needed: ignored.");
break; break;
default: default:
dbgln("Terminal::alter_mode: Unimplemented private mode {}", mode); dbgln("Terminal::alter_mode: Unimplemented private mode {} (should_set={})", mode, should_set);
break; break;
} }
} }
@ -75,7 +75,7 @@ void Terminal::alter_mode(bool should_set, Parameters params, Intermediates inte
switch (mode) { switch (mode) {
// FIXME: implement *something* for this // FIXME: implement *something* for this
default: default:
dbgln("Terminal::alter_mode: Unimplemented mode {} (set={})", mode, should_set); dbgln("Terminal::alter_mode: Unimplemented mode {} (should_set={})", mode, should_set);
break; break;
} }
} }
@ -98,39 +98,42 @@ void Terminal::SGR(Parameters params)
m_current_attribute.reset(); m_current_attribute.reset();
return; return;
} }
if (params.size() >= 3) {
bool should_set = true;
auto kind = params[1];
u32 color = 0;
switch (kind) {
case 5: // 8-bit
color = xterm_colors[params[2]];
break;
case 2: // 24-bit
for (size_t i = 0; i < 3; ++i) {
u8 component = 0;
if (params.size() - 2 > i) {
component = params[i + 2];
}
color <<= 8;
color |= component;
}
break;
default:
should_set = false;
break;
}
if (should_set) { auto parse_color = [&]() -> Optional<u32> {
if (params.size() < 2) {
dbgln("Color code has no type");
return {};
}
u32 color = 0;
switch (params[1]) {
case 5: // 8-bit
if (params.size() < 3) {
dbgln("8-bit color code has too few parameters");
return {};
}
return xterm_colors[params[2]];
case 2: // 24-bit
if (params.size() < 5) {
dbgln("24-bit color code has too few parameters");
return {};
}
for (size_t i = 0; i < 3; ++i) {
color <<= 8;
color |= params[i + 2];
}
return color;
default:
dbgln("Unknown color type {}", params[1]);
return {};
}
};
if (params[0] == 38) { if (params[0] == 38) {
m_current_attribute.foreground_color = color; m_current_attribute.foreground_color = parse_color().value_or(m_current_attribute.foreground_color);
return;
} else if (params[0] == 48) { } else if (params[0] == 48) {
m_current_attribute.background_color = color; m_current_attribute.background_color = parse_color().value_or(m_current_attribute.background_color);
return; } else {
} // A single escape sequence may set multiple parameters.
}
}
for (auto param : params) { for (auto param : params) {
switch (param) { switch (param) {
case 0: case 0:
@ -206,6 +209,7 @@ void Terminal::SGR(Parameters params)
} }
} }
} }
}
void Terminal::SCOSC() void Terminal::SCOSC()
{ {