reflow_strategy_wider.dart 2.2 KB

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