Bläddra i källkod

add lenght to BufferLine constructor

xuty 4 år sedan
förälder
incheckning
95bb7f368c
3 ändrade filer med 17 tillägg och 17 borttagningar
  1. 1 2
      lib/buffer/buffer.dart
  2. 14 11
      lib/buffer/buffer_line.dart
  3. 2 4
      lib/buffer/reflow_strategy_narrower.dart

+ 1 - 2
lib/buffer/buffer.dart

@@ -533,8 +533,7 @@ class Buffer {
   }
 
   BufferLine _newEmptyLine() {
-    final line = BufferLine();
-    line.ensure(terminal.viewWidth);
+    final line = BufferLine(length: terminal.viewWidth);
     return line;
   }
 

+ 14 - 11
lib/buffer/buffer_line.dart

@@ -26,8 +26,17 @@ const _cellBgColor = 8;
 const _cellWidth = 12;
 const _cellFlags = 13;
 
+int _nextLength(int lengthRequirement) {
+  var nextLength = 2;
+  while (nextLength < lengthRequirement) {
+    nextLength *= 2;
+  }
+  return nextLength;
+}
+
 class BufferLine {
-  BufferLine({this.isWrapped = false}) {
+  BufferLine({int length = 64, this.isWrapped = false}) {
+    _maxCols = _nextLength(length);
     _cells = ByteData(_maxCols * _cellSize);
   }
 
@@ -39,21 +48,15 @@ class BufferLine {
   int? _currentTrimmedLength;
 
   void ensure(int length) {
-    final expectedLengthInBytes = length * _cellSize;
-
-    if (expectedLengthInBytes < _cells.lengthInBytes) {
+    if (length <= _maxCols) {
       return;
     }
 
-    var newLengthInBytes = _cells.lengthInBytes;
-    while (newLengthInBytes < expectedLengthInBytes) {
-      newLengthInBytes *= 2;
-    }
-
-    final newCells = ByteData(newLengthInBytes);
+    final nextLength = _nextLength(length);
+    final newCells = ByteData(nextLength * _cellSize);
     newCells.buffer.asInt64List().setAll(0, _cells.buffer.asInt64List());
     _cells = newCells;
-    _maxCols = newLengthInBytes ~/ _cellSize;
+    _maxCols = nextLength;
   }
 
   void insert(int index) {

+ 2 - 4
lib/buffer/reflow_strategy_narrower.dart

@@ -39,8 +39,7 @@ class ReflowStrategyNarrower extends ReflowStrategy {
 
         //when we have aggregated a whole new line then insert it now
         while (cellsToCopy > newCols) {
-          final newLine = BufferLine(isWrapped: true);
-          newLine.ensure(newCols);
+          final newLine = BufferLine(length: newCols, isWrapped: true);
           newLine.copyCellsFrom(line, moveIndexStart, 0, newCols);
           // line.clearRange(moveIndexStart, moveIndexStart + newCols);
           line.removeN(moveIndexStart, newCols);
@@ -74,8 +73,7 @@ class ReflowStrategyNarrower extends ReflowStrategy {
           }
         }
 
-        final newLine = BufferLine(isWrapped: true);
-        newLine.ensure(newCols);
+        final newLine = BufferLine(length: newCols, isWrapped: true);
         newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
         // clean the cells that we moved
         line.removeN(moveIndexStart, cellsToCopy);