Kernel/Terminal: Fix ANSI SGR m for multiple values

This commit is contained in:
Bananymous 2025-04-23 05:29:22 +03:00
parent 8834241417
commit 8a00b53050
3 changed files with 14 additions and 13 deletions

View File

@ -306,8 +306,7 @@ namespace Kernel
dprintln_if(DEBUG_VTTY, "Unsupported ANSI CSI character f"); dprintln_if(DEBUG_VTTY, "Unsupported ANSI CSI character f");
return; return;
case 'm': case 'm':
handle_ansi_csi_color(BAN::Math::max(m_ansi_state.nums[0], 0)); for (int i = 0; i <= m_ansi_state.index && i < static_cast<int>(max_ansi_args); i++)
for (int i = 1; i < m_ansi_state.index; i++)
handle_ansi_csi_color(BAN::Math::max(m_ansi_state.nums[i], 0)); handle_ansi_csi_color(BAN::Math::max(m_ansi_state.nums[i], 0));
return reset_ansi(); return reset_ansi();
case 's': case 's':

View File

@ -276,9 +276,9 @@ bool Terminal::read_shell()
return true; return true;
} }
void Terminal::handle_sgr() void Terminal::handle_sgr(int32_t value)
{ {
switch (m_csi_info.fields[0]) switch (value)
{ {
case -1: case 0: case -1: case 0:
m_bg_color = s_default_bg_color; m_bg_color = s_default_bg_color;
@ -298,25 +298,25 @@ void Terminal::handle_sgr()
m_colors_inverted = false; m_colors_inverted = false;
break; break;
case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37:
m_fg_color = s_colors_dark[m_csi_info.fields[0] - 30]; m_fg_color = s_colors_dark[value - 30];
break; break;
case 39: case 39:
m_fg_color = s_default_fg_color; m_fg_color = s_default_fg_color;
break; break;
case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47:
m_bg_color = s_colors_dark[m_csi_info.fields[0] - 40]; m_bg_color = s_colors_dark[value - 40];
break; break;
case 49: case 49:
m_bg_color = s_default_bg_color; m_bg_color = s_default_bg_color;
break; break;
case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97:
m_fg_color = s_colors_bright[m_csi_info.fields[0] - 90]; m_fg_color = s_colors_bright[value - 90];
break; break;
case 100: case 101: case 102: case 103: case 104: case 105: case 106: case 107: case 100: case 101: case 102: case 103: case 104: case 105: case 106: case 107:
m_bg_color = s_colors_bright[m_csi_info.fields[0] - 100]; m_bg_color = s_colors_bright[value - 100];
break; break;
default: default:
dprintln("TODO: SGR {}", m_csi_info.fields[0]); dprintln("TODO: SGR {}", value);
break; break;
} }
} }
@ -337,7 +337,7 @@ Rectangle Terminal::handle_csi(char ch)
if (isdigit(ch)) if (isdigit(ch))
{ {
if (m_csi_info.index <= 1) if (m_csi_info.index < m_csi_info.max_fields)
{ {
auto& field = m_csi_info.fields[m_csi_info.index]; auto& field = m_csi_info.fields[m_csi_info.index];
field = (BAN::Math::max(field, 0) * 10) + (ch - '0'); field = (BAN::Math::max(field, 0) * 10) + (ch - '0');
@ -504,7 +504,8 @@ Rectangle Terminal::handle_csi(char ch)
m_cursor.y = BAN::Math::clamp<int32_t>(m_csi_info.fields[0], 1, rows()) - 1; m_cursor.y = BAN::Math::clamp<int32_t>(m_csi_info.fields[0], 1, rows()) - 1;
break; break;
case 'm': case 'm':
handle_sgr(); for (size_t i = 0; i <= m_csi_info.index && i < m_csi_info.max_fields; i++)
handle_sgr(m_csi_info.fields[i]);
break; break;
case 's': case 's':
m_saved_cursor = m_cursor; m_saved_cursor = m_cursor;

View File

@ -35,7 +35,7 @@ public:
uint32_t rows() const { return m_window->height() / m_font.height(); } uint32_t rows() const { return m_window->height() / m_font.height(); }
private: private:
void handle_sgr(); void handle_sgr(int32_t value);
Rectangle handle_csi(char ch); Rectangle handle_csi(char ch);
Rectangle putcodepoint(uint32_t codepoint); Rectangle putcodepoint(uint32_t codepoint);
Rectangle putchar(uint8_t ch); Rectangle putchar(uint8_t ch);
@ -70,7 +70,8 @@ private:
struct CSIInfo struct CSIInfo
{ {
int32_t fields[2]; static constexpr size_t max_fields = 2;
int32_t fields[max_fields];
size_t index; size_t index;
bool question; bool question;
}; };