reflow_strategy_narrower.dart 3.5 KB

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