reflow_strategy_wider.dart 1.8 KB

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