reflow_strategy_narrower.dart 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. //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. if (lineLength > newCols) {
  14. var moveIndexStart = newCols;
  15. var cellsToCopy = lineLength - newCols;
  16. // when we have a double width character and are about to move the "0" placeholder,
  17. // then we have to move the double width character as well
  18. if (line.cellGetContent(moveIndexStart) == 0 &&
  19. line.cellGetWidth(moveIndexStart - 1) == 2) {
  20. moveIndexStart -= 1;
  21. cellsToCopy += 1;
  22. }
  23. var addZero = false;
  24. //when the last cell to copy is a double width cell, then add a "0"
  25. if (line.cellGetWidth(moveIndexStart + cellsToCopy - 1) == 2) {
  26. addZero = true;
  27. }
  28. var alreadyInserted = 0;
  29. //when we have aggregated a whole new line then insert it now
  30. while (cellsToCopy > newCols) {
  31. final newLine = BufferLine(isWrapped: true);
  32. newLine.ensure(newCols);
  33. newLine.copyCellsFrom(line, moveIndexStart, 0, newCols);
  34. // line.clearRange(moveIndexStart, moveIndexStart + newCols);
  35. line.removeN(moveIndexStart, newCols);
  36. buffer.lines.insert(i + 1, 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. if (nextLineIndex < buffer.lines.length) {
  45. final nextLine = buffer.lines[nextLineIndex];
  46. if (nextLine.isWrapped) {
  47. final nextLineLength = nextLine.getTrimmedLength();
  48. nextLine.ensure(nextLineLength + cellsToCopy + (addZero ? 1 : 0));
  49. nextLine.insertN(0, cellsToCopy + (addZero ? 1 : 0));
  50. nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  51. // clean the cells that we moved
  52. line.removeN(moveIndexStart, cellsToCopy);
  53. // line.erase(buffer.terminal.cursor, moveIndexStart,
  54. // moveIndexStart + cellsToCopy);
  55. //print('M: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  56. //print(
  57. // 'N: ${i + 1 < 10 ? '0' : ''}${i + 1}: ${nextLine.toDebugString(oldCols)}');
  58. continue;
  59. }
  60. }
  61. final newLine = BufferLine(isWrapped: true);
  62. newLine.ensure(newCols);
  63. newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  64. // clean the cells that we moved
  65. line.removeN(moveIndexStart, cellsToCopy);
  66. buffer.lines.insert(nextLineIndex, 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. }
  74. }