reflow_strategy_wider.dart 2.5 KB

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