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. 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 (nextLine.cellGetWidth(moveCount - 1) == 2) {
  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. }