reflow_strategy_narrower.dart 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'package:xterm/buffer/buffer.dart';
  2. import 'package:xterm/buffer/line/line.dart';
  3. import 'package:xterm/buffer/reflow_strategy.dart';
  4. class ReflowStrategyNarrower extends ReflowStrategy {
  5. ReflowStrategyNarrower(Buffer buffer) : super(buffer);
  6. @override
  7. void reflow(int newCols, int newRows, int oldCols, int oldRows) {
  8. final linesAfterReflow = <BufferLine>[];
  9. //print('Reflow narrower $oldCols -> $newCols');
  10. for (var i = 0; i < buffer.lines.length; i++) {
  11. final line = buffer.lines[i];
  12. final lineLength = line.getTrimmedLength();
  13. linesAfterReflow.add(line);
  14. if (lineLength > newCols) {
  15. var moveIndexStart = newCols;
  16. var cellsToCopy = lineLength - newCols;
  17. // when we have a double width character and are about to move the "0" placeholder,
  18. // then we have to move the double width character as well
  19. if (line.cellGetContent(moveIndexStart) == 0 &&
  20. line.cellGetWidth(moveIndexStart - 1) == 2) {
  21. moveIndexStart -= 1;
  22. cellsToCopy += 1;
  23. }
  24. var addZero = false;
  25. //when the last cell to copy is a double width cell, then add a "0"
  26. if (line.cellGetWidth(moveIndexStart + cellsToCopy - 1) == 2) {
  27. addZero = true;
  28. }
  29. // var alreadyInserted = 0;
  30. //when we have aggregated a whole new line then insert it now
  31. while (cellsToCopy > newCols) {
  32. final newLine = BufferLine(length: newCols, isWrapped: true);
  33. newLine.copyCellsFrom(line, moveIndexStart, 0, newCols);
  34. // line.clearRange(moveIndexStart, moveIndexStart + newCols);
  35. line.removeN(moveIndexStart, newCols);
  36. linesAfterReflow.add(newLine);
  37. cellsToCopy -= newCols;
  38. // alreadyInserted++;
  39. }
  40. // we need to move cut cells to the next line
  41. // if the next line is wrapped anyway, we can push them onto the beginning of that line
  42. // otherwise, we need add a new wrapped line
  43. // final nextLineIndex = i + alreadyInserted + 1;
  44. final nextLineIndex = i + 1;
  45. if (nextLineIndex < buffer.lines.length) {
  46. final nextLine = buffer.lines[nextLineIndex];
  47. if (nextLine.isWrapped) {
  48. final nextLineLength = nextLine.getTrimmedLength();
  49. nextLine.ensure(nextLineLength + cellsToCopy + (addZero ? 1 : 0));
  50. nextLine.insertN(0, cellsToCopy + (addZero ? 1 : 0));
  51. nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  52. // clean the cells that we moved
  53. line.removeN(moveIndexStart, cellsToCopy);
  54. // line.erase(buffer.terminal.cursor, moveIndexStart,
  55. // moveIndexStart + cellsToCopy);
  56. //print('M: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  57. //print(
  58. // 'N: ${i + 1 < 10 ? '0' : ''}${i + 1}: ${nextLine.toDebugString(oldCols)}');
  59. continue;
  60. }
  61. }
  62. final newLine = BufferLine(length: newCols, isWrapped: true);
  63. newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  64. // clean the cells that we moved
  65. line.removeN(moveIndexStart, cellsToCopy);
  66. linesAfterReflow.add(newLine);
  67. //TODO: scrolling is a bit weird afterwards
  68. //print('S: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  69. } else {
  70. //print('N: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  71. }
  72. }
  73. buffer.lines.replaceWith(linesAfterReflow);
  74. }
  75. }