diff --git a/Applications/Terminal/Terminal.cpp b/Applications/Terminal/Terminal.cpp index 8144990ee3..41c52f1fcb 100644 --- a/Applications/Terminal/Terminal.cpp +++ b/Applications/Terminal/Terminal.cpp @@ -68,12 +68,9 @@ Terminal::Terminal(int ptm_fd, RetainPtr config) m_config->read_num_entry("Window", "Height", 25)); } -Terminal::Line::Line(word columns) - : length(columns) +Terminal::Line::Line(word length) { - characters = new byte[length]; - attributes = new Attribute[length]; - memset(characters, ' ', length); + set_length(length); } Terminal::Line::~Line() @@ -82,20 +79,34 @@ Terminal::Line::~Line() 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) { if (dirty) { - memset(characters, ' ', length); - for (word i = 0; i < length; ++i) + memset(characters, ' ', m_length); + for (word i = 0; i < m_length; ++i) attributes[i] = attribute; return; } - for (unsigned i = 0; i < length; ++i) { + for (unsigned i = 0; i < m_length; ++i) { if (characters[i] != ' ') dirty = true; characters[i] = ' '; } - for (unsigned i = 0; i < length; ++i) { + for (unsigned i = 0; i < m_length; ++i) { if (attributes[i] != attribute) dirty = true; attributes[i] = attribute; @@ -877,6 +888,9 @@ void Terminal::set_size(word columns, word rows) m_lines.resize(rows); } + for (int i = 0; i < rows; ++i) + m_lines[i]->set_length(columns); + m_columns = columns; m_rows = rows; @@ -927,11 +941,11 @@ Rect Terminal::row_rect(word row) bool Terminal::Line::has_only_one_background_color() const { - if (!length) + if (!m_length) return true; // FIXME: Cache this result? 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) return false; } diff --git a/Applications/Terminal/Terminal.h b/Applications/Terminal/Terminal.h index 564a1ff268..ecadcf7682 100644 --- a/Applications/Terminal/Terminal.h +++ b/Applications/Terminal/Terminal.h @@ -127,10 +127,11 @@ private: ~Line(); void clear(Attribute); bool has_only_one_background_color() const; + void set_length(word); byte* characters { nullptr }; Attribute* attributes { nullptr }; bool dirty { false }; - word length { 0 }; + word m_length { 0 }; }; Line& line(size_t index) {