Browse Source

apply new reflow algorithm

xuty 4 năm trước cách đây
mục cha
commit
bbc5d6fb6a

+ 1 - 1
lib/buffer/buffer.dart

@@ -422,7 +422,7 @@ class Buffer {
   void deleteChars(int count) {
     final start = _cursorX.clamp(0, terminal.viewWidth);
     final end = min(_cursorX + count, terminal.viewWidth);
-    currentLine.removeRange(start, end);
+    currentLine.clearRange(start, end);
   }
 
   void clearScrollback() {

+ 11 - 6
lib/buffer/buffer_line.dart

@@ -51,7 +51,7 @@ class BufferLine {
     final newCells = ByteData(newLengthInBytes);
     newCells.buffer.asInt64List().setAll(0, _cells.buffer.asInt64List());
     _cells = newCells;
-    _maxCols = (newLengthInBytes / _cellSize).floor();
+    _maxCols = newLengthInBytes ~/ _cellSize;
   }
 
   void insert(int index) {
@@ -95,7 +95,7 @@ class BufferLine {
   }
 
   void clear() {
-    removeRange(0, _cells.lengthInBytes ~/ _cellSize);
+    clearRange(0, _cells.lengthInBytes ~/ _cellSize);
   }
 
   void erase(Cursor cursor, int start, int end, [bool resetIsWrapped = false]) {
@@ -108,6 +108,11 @@ class BufferLine {
     }
   }
 
+  void cellClear(int index) {
+    _cells.setInt64(index * _cellSize, 0x00);
+    _cells.setInt64(index * _cellSize + 8, 0x00);
+  }
+
   void cellInitialize(
     int index, {
     required int content,
@@ -134,7 +139,7 @@ class BufferLine {
   }
 
   void cellSetContent(int index, int content) {
-    return _cells.setInt32(index * _cellSize + _cellContent, content);
+    _cells.setInt32(index * _cellSize + _cellContent, content);
   }
 
   int cellGetFgColor(int index) {
@@ -220,7 +225,7 @@ class BufferLine {
     return 0;
   }
 
-  copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
+  void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
     final dstOffset = dstCol * _cellSize;
     final srcOffset = srcCol * _cellSize;
     final byteLen = len * _cellSize;
@@ -239,14 +244,14 @@ class BufferLine {
   //   return a ^ b;
   // }
 
-  void removeRange(int start, int end) {
+  void clearRange(int start, int end) {
     end = min(end, _maxCols);
     // start = start.clamp(0, _cells.length);
     // end ??= _cells.length;
     // end = end.clamp(start, _cells.length);
     // _cells.removeRange(start, end);
     for (var index = start; index < end; index++) {
-      cellSetContent(index, 0x00);
+      cellClear(index);
     }
   }
 

+ 25 - 9
lib/buffer/reflow_strategy_narrower.dart

@@ -1,10 +1,8 @@
 import 'dart:math';
 
-import 'package:flutter/material.dart';
 import 'package:xterm/buffer/buffer.dart';
 import 'package:xterm/buffer/buffer_line.dart';
 import 'package:xterm/buffer/reflow_strategy.dart';
-import 'package:xterm/utli/circular_list.dart';
 
 class ReflowStrategyNarrower extends ReflowStrategy {
   ReflowStrategyNarrower(Buffer buffer) : super(buffer);
@@ -33,19 +31,37 @@ class ReflowStrategyNarrower extends ReflowStrategy {
           addZero = true;
         }
 
+        var alreadyInserted = 0;
+
+        //when we have aggregated a whole new line then insert it now
+        while (cellsToCopy > newCols) {
+          final newLine = BufferLine(isWrapped: true);
+          newLine.ensure(newCols);
+          newLine.copyCellsFrom(line, moveIndexStart, 0, newCols);
+          // line.clearRange(moveIndexStart, moveIndexStart + newCols);
+          line.removeN(moveIndexStart, newCols);
+
+          buffer.lines.insert(i + 1, newLine);
+
+          cellsToCopy -= newCols;
+          alreadyInserted++;
+        }
+
         // we need to move cut cells to the next line
         // if the next line is wrapped anyway, we can push them onto the beginning of that line
         // otherwise, we need add a new wrapped line
-        if (i + 1 < buffer.lines.length) {
-          final nextLine = buffer.lines[i + 1];
+        final nextLineIndex = i + alreadyInserted + 1;
+        if (nextLineIndex < buffer.lines.length) {
+          final nextLine = buffer.lines[nextLineIndex];
           if (nextLine.isWrapped) {
             final nextLineLength = nextLine.getTrimmedLength();
             nextLine.ensure(nextLineLength + cellsToCopy + (addZero ? 1 : 0));
             nextLine.insertN(0, cellsToCopy + (addZero ? 1 : 0));
             nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
             // clean the cells that we moved
-            line.erase(buffer.terminal.cursor, moveIndexStart,
-                moveIndexStart + cellsToCopy);
+            line.removeN(moveIndexStart, cellsToCopy);
+            // line.erase(buffer.terminal.cursor, moveIndexStart,
+            //     moveIndexStart + cellsToCopy);
             //print('M: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
             //print(
             //    'N: ${i + 1 < 10 ? '0' : ''}${i + 1}: ${nextLine.toDebugString(oldCols)}');
@@ -54,12 +70,12 @@ class ReflowStrategyNarrower extends ReflowStrategy {
         }
 
         final newLine = BufferLine(isWrapped: true);
-        newLine.ensure(max(newCols, cellsToCopy));
+        newLine.ensure(newCols);
         newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
         // clean the cells that we moved
-        line.erase(buffer.terminal.cursor, moveIndexStart, lineLength);
+        line.removeN(moveIndexStart, cellsToCopy);
 
-        buffer.lines.insert(i + 1, newLine);
+        buffer.lines.insert(nextLineIndex, newLine);
 
         //TODO: scrolling is a bit weird afterwards
 

+ 2 - 2
lib/buffer/reflow_strategy_wider.dart

@@ -3,7 +3,6 @@ import 'dart:math';
 import 'package:xterm/buffer/buffer.dart';
 import 'package:xterm/buffer/buffer_line.dart';
 import 'package:xterm/buffer/reflow_strategy.dart';
-import 'package:xterm/utli/circular_list.dart';
 
 class ReflowStrategyWider extends ReflowStrategy {
   ReflowStrategyWider(Buffer buffer) : super(buffer);
@@ -20,7 +19,8 @@ class ReflowStrategyWider extends ReflowStrategy {
         final lineLength = line.getTrimmedLength();
 
         var copyDestIndex = lineLength;
-        if (line.cellGetWidth(copyDestIndex - 1) == 2 &&
+        if (copyDestIndex >= 1 &&
+            line.cellGetWidth(copyDestIndex - 1) == 2 &&
             line.cellGetContent(copyDestIndex) == 0) {
           //we would override a wide char placeholder => move index one to the right
           copyDestIndex += 1;