xuty преди 4 години
родител
ревизия
102b4e5d8b
променени са 2 файла, в които са добавени 45 реда и са изтрити 15 реда
  1. 35 5
      lib/buffer/buffer.dart
  2. 10 10
      lib/terminal/terminal.dart

+ 35 - 5
lib/buffer/buffer.dart

@@ -11,12 +11,16 @@ import 'package:xterm/utli/unicode_v11.dart';
 class Buffer {
   Buffer(this.terminal) {
     resetVerticalMargins();
+    lines = List.generate(terminal.viewHeight, (_) => BufferLine());
   }
 
   final Terminal terminal;
-  final lines = <BufferLine>[];
   final charset = Charset();
 
+  /// lines of the buffer. the length of [lines] should always be equal or
+  /// greater than [Terminal.viewHeight].
+  late final List<BufferLine> lines;
+
   int? _savedCursorX;
   int? _savedCursorY;
   CellAttr? _savedCellAttr;
@@ -102,10 +106,10 @@ class Buffer {
       return lines.last;
     }
 
-    while (index >= lines.length) {
-      final newLine = BufferLine();
-      lines.add(newLine);
-    }
+    // while (index >= lines.length) {
+    //   final newLine = BufferLine();
+    //   lines.add(newLine);
+    // }
 
     return lines[convertViewLineToRawLine(index)];
   }
@@ -503,4 +507,30 @@ class Buffer {
 
     lines.removeAt(index);
   }
+
+  void resize(int newWidth, int newHeight) {
+    if (newHeight > terminal.viewHeight) {
+      // Grow larger
+      for (var i = 0; i < newHeight - terminal.viewHeight; i++) {
+        if (_cursorY < terminal.viewHeight - 1) {
+          lines.add(BufferLine());
+        } else {
+          _cursorY++;
+        }
+      }
+    } else {
+      // Shrink smaller
+      for (var i = 0; i < terminal.viewHeight - newHeight; i++) {
+        if (_cursorY < terminal.viewHeight - 1) {
+          lines.removeLast();
+        } else {
+          _cursorY++;
+        }
+      }
+    }
+
+    // Ensure cursor is within the screen.
+    _cursorX = _cursorX.clamp(0, newWidth - 1);
+    _cursorY = _cursorY.clamp(0, newHeight - 1);
+  }
 }

+ 10 - 10
lib/terminal/terminal.dart

@@ -324,19 +324,19 @@ class Terminal with Observable {
     return _buffer == _altBuffer;
   }
 
-  void resize(int width, int height) {
-    if (height > _viewHeight) {
-      // todo
-    } else {
-      // todo
-    }
+  /// Resize the terminal screen. [newWidth] and [newHeight] should be greater
+  /// than 0. Text reflow is currently not implemented and will be avaliable in
+  /// the future.
+  void resize(int newWidth, int newHeight) {
+    newWidth = max(newWidth, 1);
+    newHeight = max(newHeight, 1);
 
-    final cursorY = buffer.convertViewLineToRawLine(buffer.cursorY);
+    buffer.resize(newWidth, newHeight);
 
-    _viewWidth = max(width, 1);
-    _viewHeight = max(height, 1);
+    // maybe reflow should happen here.
 
-    buffer.setCursorY(buffer.convertRawLineToViewLine(cursorY));
+    _viewWidth = newWidth;
+    _viewHeight = newHeight;
 
     if (buffer == _altBuffer) {
       buffer.clearScrollback();