فهرست منبع

Fix new line width in reflow

xuty 3 سال پیش
والد
کامیت
8d324715ce
3فایلهای تغییر یافته به همراه39 افزوده شده و 12 حذف شده
  1. 7 7
      lib/src/core/buffer/buffer.dart
  2. 7 5
      lib/src/core/buffer/line.dart
  3. 25 0
      test/src/core/buffer/buffer_test.dart

+ 7 - 7
lib/src/core/buffer/buffer.dart

@@ -407,15 +407,13 @@ class Buffer {
   }
 
   void resize(int oldWidth, int oldHeight, int newWidth, int newHeight) {
-    if (newWidth > oldWidth) {
-      lines.forEach((item) => item.resize(newWidth));
-    }
+    lines.forEach((item) => item.resize(newWidth));
 
     if (newHeight > oldHeight) {
       // Grow larger
       for (var i = 0; i < newHeight - oldHeight; i++) {
         if (newHeight > lines.length) {
-          lines.push(_newEmptyLine());
+          lines.push(_newEmptyLine(newWidth));
         } else {
           _cursorY++;
         }
@@ -439,15 +437,17 @@ class Buffer {
       final reflowResult = reflow(lines, oldWidth, newWidth);
 
       while (reflowResult.length < newHeight) {
-        reflowResult.add(_newEmptyLine());
+        reflowResult.add(_newEmptyLine(newWidth));
       }
 
       lines.replaceWith(reflowResult);
     }
   }
 
-  BufferLine _newEmptyLine() {
-    final line = BufferLine(viewWidth);
+  /// Create a new empty [BufferLine] with the current [viewWidth] if [width]
+  /// is not specified.
+  BufferLine _newEmptyLine([int? width]) {
+    final line = BufferLine(width ?? viewWidth);
     return line;
   }
 

+ 7 - 5
lib/src/core/buffer/line.dart

@@ -200,12 +200,14 @@ class BufferLine {
       return;
     }
 
-    final newBufferSize = _calcCapacity(length) * _cellSize;
+    if (length > _length) {
+      final newBufferSize = _calcCapacity(length) * _cellSize;
 
-    if (newBufferSize > _data.length) {
-      final newBuffer = Uint32List(newBufferSize);
-      newBuffer.setRange(0, _data.length, _data);
-      _data = newBuffer;
+      if (newBufferSize > _data.length) {
+        final newBuffer = Uint32List(newBufferSize);
+        newBuffer.setRange(0, _data.length, _data);
+        _data = newBuffer;
+      }
     }
 
     _length = length;

+ 25 - 0
test/src/core/buffer/buffer_test.dart

@@ -69,4 +69,29 @@ void main() {
       );
     });
   });
+
+  group('Buffer.resize()', () {
+    test('should resize the buffer', () {
+      final terminal = Terminal();
+      terminal.resize(10, 10);
+
+      expect(terminal.viewWidth, 10);
+      expect(terminal.viewHeight, 10);
+
+      for (var i = 0; i < terminal.lines.length; i++) {
+        final line = terminal.lines[i];
+        expect(line.length, 10);
+      }
+
+      terminal.resize(20, 20);
+
+      expect(terminal.viewWidth, 20);
+      expect(terminal.viewHeight, 20);
+
+      for (var i = 0; i < terminal.lines.length; i++) {
+        final line = terminal.lines[i];
+        expect(line.length, 20);
+      }
+    });
+  });
 }