reflow_strategy_narrower.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:xterm/buffer/buffer.dart';
  4. import 'package:xterm/buffer/buffer_line.dart';
  5. import 'package:xterm/buffer/reflow_strategy.dart';
  6. import 'package:xterm/utli/circular_list.dart';
  7. class ReflowStrategyNarrower extends ReflowStrategy {
  8. ReflowStrategyNarrower(Buffer buffer) : super(buffer);
  9. @override
  10. void reflow(int newCols, int newRows, int oldCols, int oldRows) {
  11. for (var i = 0; i < buffer.lines.length; i++) {
  12. final line = buffer.lines[i];
  13. final lineLength = line.getTrimmedLength(oldCols);
  14. if (lineLength > newCols) {
  15. final moveIndexStart = newCols;
  16. final cellsToCopy = oldCols - newCols;
  17. // we need to move cut cells to the next line
  18. // if the next line is wrapped anyway, we can push them onto the beginning of that line
  19. // otherwise, we need add a new wrapped line
  20. if (i + 1 < buffer.lines.length) {
  21. final nextLine = buffer.lines[i + 1];
  22. if (nextLine.isWrapped) {
  23. nextLine.ensure(oldCols + cellsToCopy);
  24. nextLine.insertN(0, cellsToCopy);
  25. nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  26. line.erase(buffer.terminal.cursor, moveIndexStart, oldCols);
  27. continue;
  28. }
  29. }
  30. final newLine = BufferLine(isWrapped: true);
  31. newLine.ensure(newCols);
  32. newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  33. line.erase(buffer.terminal.cursor, moveIndexStart, oldCols);
  34. //TODO: aggregate and do at the end?
  35. buffer.lines.insert(i + 1, newLine);
  36. if (i + 1 <= buffer.cursorY) {
  37. buffer.moveCursorY(1);
  38. }
  39. }
  40. }
  41. }
  42. }