reflow_strategy_narrower.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. //print('Reflow narrower $oldCols -> $newCols');
  12. for (var i = 0; i < buffer.lines.length; i++) {
  13. final line = buffer.lines[i];
  14. final lineLength = line.getTrimmedLength();
  15. if (lineLength > newCols) {
  16. var moveIndexStart = newCols;
  17. var cellsToCopy = lineLength - newCols;
  18. // when we have a double width character and are about to move the "0" placeholder,
  19. // then we have to move the double width character as well
  20. if (line.cellGetContent(moveIndexStart) == 0 &&
  21. line.cellGetWidth(moveIndexStart - 1) == 2) {
  22. moveIndexStart -= 1;
  23. cellsToCopy += 1;
  24. }
  25. var addZero = false;
  26. //when the last cell to copy is a double width cell, then add a "0"
  27. if (line.cellGetWidth(moveIndexStart + cellsToCopy - 1) == 2) {
  28. addZero = true;
  29. }
  30. // we need to move cut cells to the next line
  31. // if the next line is wrapped anyway, we can push them onto the beginning of that line
  32. // otherwise, we need add a new wrapped line
  33. if (i + 1 < buffer.lines.length) {
  34. final nextLine = buffer.lines[i + 1];
  35. if (nextLine.isWrapped) {
  36. final nextLineLength = nextLine.getTrimmedLength();
  37. nextLine.ensure(nextLineLength + cellsToCopy + (addZero ? 1 : 0));
  38. nextLine.insertN(0, cellsToCopy + (addZero ? 1 : 0));
  39. nextLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  40. // clean the cells that we moved
  41. line.erase(buffer.terminal.cursor, moveIndexStart,
  42. moveIndexStart + cellsToCopy);
  43. //print('M: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  44. //print(
  45. // 'N: ${i + 1 < 10 ? '0' : ''}${i + 1}: ${nextLine.toDebugString(oldCols)}');
  46. continue;
  47. }
  48. }
  49. final newLine = BufferLine(isWrapped: true);
  50. newLine.ensure(max(newCols, cellsToCopy));
  51. newLine.copyCellsFrom(line, moveIndexStart, 0, cellsToCopy);
  52. // clean the cells that we moved
  53. line.erase(buffer.terminal.cursor, moveIndexStart, lineLength);
  54. buffer.lines.insert(i + 1, newLine);
  55. //TODO: scrolling is a bit weird afterwards
  56. //print('S: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  57. } else {
  58. //print('N: ${i < 10 ? '0' : ''}$i: ${line.toDebugString(oldCols)}');
  59. }
  60. }
  61. }
  62. }