소스 검색

Terminal: Reallocate kept lines when resizing the terminal.

Otherwise we end up with garbage text when making the window bigger.
Andreas Kling 6 년 전
부모
커밋
6e4f0b3cc5
2개의 변경된 파일27개의 추가작업 그리고 12개의 파일을 삭제
  1. 25 11
      Applications/Terminal/Terminal.cpp
  2. 2 1
      Applications/Terminal/Terminal.h

+ 25 - 11
Applications/Terminal/Terminal.cpp

@@ -68,12 +68,9 @@ Terminal::Terminal(int ptm_fd, RetainPtr<CConfigFile> 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;
     }

+ 2 - 1
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)
     {