reflow_strategy_narrower.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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(length: newCols, isWrapped: true);
  34. newLine.copyCellsFrom(line, moveIndexStart, 0, newCols);
  35. // line.clearRange(moveIndexStart, moveIndexStart + newCols);
  36. line.removeN(moveIndexStart, newCols);
  37. linesAfterReflow.add(newLine);
  38. cellsToCopy -= newCols;
  39. // alreadyInserted++;
  40. }
  41. // we need to move cut cells to the next line
  42. // if the next line is wrapped anyway, we can push them onto the beginning of that line
  43. // otherwise, we need add a new wrapped line
  44. // final nextLineIndex = i + alreadyInserted + 1;
  45. final nextLineIndex = i + 1;
  46. if (nextLineIndex < buffer.lines.length) {
  47. final nextLine = buffer.lines[nextLineIndex];
  48. if (nextLine.isWrapped) {
  49. final nextLineLength = nextLine.getTrimmedLength();
  50. nextLine.ensure(nextLineLength + cellsToCopy + (addZero ? 1 : 0));
  51. nextLine.insertN(0, cellsToCopy + (addZero ? 1 : 0));
  52. nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  53. // clean the cells that we moved
  54. line.removeN(moveIndexStart, cellsToCopy);
  55. // line.erase(buffer.terminal.cursor, moveIndexStart,
  56. // moveIndexStart + cellsToCopy);
  57. //print('M: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  58. //print(
  59. // 'N: ${i + 1 < 10 ? '0' : ''}${i + 1}: ${nextLine.toDebugString(oldCols)}');
  60. continue;
  61. }
  62. }
  63. final newLine = BufferLine(length: newCols, isWrapped: true);
  64. newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  65. // clean the cells that we moved
  66. line.removeN(moveIndexStart, cellsToCopy);
  67. linesAfterReflow.add(newLine);
  68. //TODO: scrolling is a bit weird afterwards
  69. //print('S: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  70. } else {
  71. //print('N: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  72. }
  73. }
  74. buffer.lines.replaceWith(linesAfterReflow);
  75. }
  76. }