reflow_strategy_narrower.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. var moveIndexStart = newCols;
  16. var cellsToCopy = oldCols - newCols;
  17. // when we have a double width character and are about to move the "0" placeholder,
  18. // then we have to move the double width character as well
  19. if (line.cellGetContent(moveIndexStart) == 0 &&
  20. line.cellGetWidth(moveIndexStart - 1) == 2) {
  21. moveIndexStart -= 1;
  22. cellsToCopy += 1;
  23. }
  24. // we need to move cut cells to the next line
  25. // if the next line is wrapped anyway, we can push them onto the beginning of that line
  26. // otherwise, we need add a new wrapped line
  27. if (i + 1 < buffer.lines.length) {
  28. final nextLine = buffer.lines[i + 1];
  29. if (nextLine.isWrapped) {
  30. nextLine.ensure(oldCols + cellsToCopy); //to be safe
  31. nextLine.insertN(0, cellsToCopy);
  32. nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  33. // clean the cells that we moved
  34. line.erase(buffer.terminal.cursor, moveIndexStart, oldCols);
  35. continue;
  36. }
  37. }
  38. final newLine = BufferLine(isWrapped: true);
  39. newLine.ensure(max(newCols, cellsToCopy));
  40. newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  41. // clean the cells that we moved
  42. line.erase(buffer.terminal.cursor, moveIndexStart, oldCols);
  43. buffer.lines.insert(i + 1, newLine);
  44. //TODO: scrolling is a bit weird afterwards
  45. }
  46. }
  47. }
  48. }