reflow_strategy_wider.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ReflowStrategyWider extends ReflowStrategy {
  6. ReflowStrategyWider(Buffer buffer) : super(buffer);
  7. @override
  8. void reflow(int newCols, int newRows, int oldCols, int oldRows) {
  9. for (var i = 0; i < buffer.lines.length; i++) {
  10. final line = buffer.lines[i];
  11. for (var offset = 1; i + offset < buffer.lines.length; offset++) {
  12. final nextLine = buffer.lines[i + offset];
  13. if (!nextLine.isWrapped) {
  14. break;
  15. }
  16. final lineLength = line.getTrimmedLength();
  17. var copyDestIndex = lineLength;
  18. if (copyDestIndex >= 1 &&
  19. line.cellGetWidth(copyDestIndex - 1) == 2 &&
  20. line.cellGetContent(copyDestIndex) == 0) {
  21. //we would override a wide char placeholder => move index one to the right
  22. copyDestIndex += 1;
  23. }
  24. final spaceOnLine = newCols - copyDestIndex;
  25. if (spaceOnLine <= 0) {
  26. // no more space to unwrap
  27. break;
  28. }
  29. final nextLineLength = nextLine.getTrimmedLength();
  30. var moveCount = min(spaceOnLine, nextLineLength);
  31. if (moveCount <= 0) {
  32. break;
  33. }
  34. // when we are about to copy a double width character
  35. // to the end of the line then we can include the 0 width placeholder
  36. // after it
  37. if (nextLine.cellGetWidth(moveCount - 1) == 2 &&
  38. nextLine.cellGetContent(moveCount) == 0) {
  39. moveCount += 1;
  40. }
  41. line.copyCellsFrom(nextLine, 0, copyDestIndex, moveCount);
  42. if (moveCount >= nextLineLength) {
  43. // if we unwrapped all cells off the next line, delete it
  44. buffer.lines.remove(i + offset);
  45. offset--;
  46. } else {
  47. // otherwise just remove the characters we moved up a line
  48. nextLine.removeN(0, moveCount);
  49. }
  50. }
  51. }
  52. //buffer doesn't have enough lines
  53. while (buffer.lines.length < buffer.terminal.viewHeight) {
  54. buffer.lines.push(BufferLine());
  55. }
  56. }
  57. }