1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:07:35 +00:00

Terminal: Reallocate kept lines when resizing the terminal.

Otherwise we end up with garbage text when making the window bigger.
This commit is contained in:
Andreas Kling 2019-06-06 14:59:18 +02:00
parent e8f35ef311
commit 6e4f0b3cc5
2 changed files with 27 additions and 12 deletions

View file

@ -68,12 +68,9 @@ Terminal::Terminal(int ptm_fd, RetainPtr<CConfigFile> config)
m_config->read_num_entry("Window", "Height", 25)); m_config->read_num_entry("Window", "Height", 25));
} }
Terminal::Line::Line(word columns) Terminal::Line::Line(word length)
: length(columns)
{ {
characters = new byte[length]; set_length(length);
attributes = new Attribute[length];
memset(characters, ' ', length);
} }
Terminal::Line::~Line() Terminal::Line::~Line()
@ -82,20 +79,34 @@ Terminal::Line::~Line()
delete[] attributes; delete[] attributes;
} }
void Terminal::Line::set_length(word new_length)
{
if (m_length == new_length)
return;
auto* new_characters = new byte[new_length];
auto* new_attributes = new Attribute[new_length];
memset(new_characters, ' ', new_length);
delete[] characters;
delete[] attributes;
characters = new_characters;
attributes = new_attributes;
m_length = new_length;
}
void Terminal::Line::clear(Attribute attribute) void Terminal::Line::clear(Attribute attribute)
{ {
if (dirty) { if (dirty) {
memset(characters, ' ', length); memset(characters, ' ', m_length);
for (word i = 0; i < length; ++i) for (word i = 0; i < m_length; ++i)
attributes[i] = attribute; attributes[i] = attribute;
return; return;
} }
for (unsigned i = 0; i < length; ++i) { for (unsigned i = 0; i < m_length; ++i) {
if (characters[i] != ' ') if (characters[i] != ' ')
dirty = true; dirty = true;
characters[i] = ' '; characters[i] = ' ';
} }
for (unsigned i = 0; i < length; ++i) { for (unsigned i = 0; i < m_length; ++i) {
if (attributes[i] != attribute) if (attributes[i] != attribute)
dirty = true; dirty = true;
attributes[i] = attribute; attributes[i] = attribute;
@ -877,6 +888,9 @@ void Terminal::set_size(word columns, word rows)
m_lines.resize(rows); m_lines.resize(rows);
} }
for (int i = 0; i < rows; ++i)
m_lines[i]->set_length(columns);
m_columns = columns; m_columns = columns;
m_rows = rows; m_rows = rows;
@ -927,11 +941,11 @@ Rect Terminal::row_rect(word row)
bool Terminal::Line::has_only_one_background_color() const bool Terminal::Line::has_only_one_background_color() const
{ {
if (!length) if (!m_length)
return true; return true;
// FIXME: Cache this result? // FIXME: Cache this result?
auto color = attributes[0].background_color; auto color = attributes[0].background_color;
for (size_t i = 1; i < length; ++i) { for (size_t i = 1; i < m_length; ++i) {
if (attributes[i].background_color != color) if (attributes[i].background_color != color)
return false; return false;
} }

View file

@ -127,10 +127,11 @@ private:
~Line(); ~Line();
void clear(Attribute); void clear(Attribute);
bool has_only_one_background_color() const; bool has_only_one_background_color() const;
void set_length(word);
byte* characters { nullptr }; byte* characters { nullptr };
Attribute* attributes { nullptr }; Attribute* attributes { nullptr };
bool dirty { false }; bool dirty { false };
word length { 0 }; word m_length { 0 };
}; };
Line& line(size_t index) Line& line(size_t index)
{ {