buffer_line.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:xterm/buffer/cell.dart';
  2. import 'package:xterm/buffer/cell_attr.dart';
  3. class BufferLine {
  4. final _cells = <Cell>[];
  5. bool _isWrapped = false;
  6. BufferLine({isWrapped = false}) {
  7. _isWrapped = isWrapped;
  8. }
  9. bool get isWrapped {
  10. return _isWrapped;
  11. }
  12. int get length {
  13. return _cells.length;
  14. }
  15. void add(Cell cell) {
  16. _cells.add(cell);
  17. }
  18. void insert(int index, Cell cell) {
  19. _cells.insert(index, cell);
  20. }
  21. void clear() {
  22. _cells.clear();
  23. }
  24. int getTrimmedLength() {
  25. int width = 0;
  26. for (int i = 0; i < _cells.length; i++)
  27. if (_cells[i].codePoint != null && _cells[i].codePoint != 0) {
  28. width += _cells[i].width;
  29. } else {
  30. return width;
  31. }
  32. return width;
  33. }
  34. void erase(CellAttr attr, int start, int end, bool clearWrap) {
  35. for (var i = start; i < end; i++) {
  36. if (i >= length) {
  37. add(Cell(attr: attr));
  38. } else {
  39. getCell(i).erase(attr);
  40. }
  41. }
  42. if (_isWrapped && clearWrap) {
  43. _isWrapped = false;
  44. }
  45. }
  46. Cell getCell(int index) {
  47. return _cells[index];
  48. }
  49. void removeRange(int start, [int? end]) {
  50. start = start.clamp(0, _cells.length);
  51. end ??= _cells.length;
  52. end = end.clamp(start, _cells.length);
  53. _cells.removeRange(start, end);
  54. }
  55. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  56. final requiredCells = dstCol + len;
  57. if (_cells.length < requiredCells) {
  58. _cells.addAll(List<Cell>.generate(
  59. requiredCells - _cells.length, (index) => Cell()));
  60. }
  61. //we have to make a copy first as src and dst might be the same line
  62. List<Cell> sourceCells =
  63. List<Cell>.generate(len, (index) => src._cells[srcCol + index].clone());
  64. for (var i = 0; i < len; i++) {
  65. _cells[dstCol + i] = sourceCells[i];
  66. }
  67. }
  68. int getWidthAt(int col) {
  69. if (col >= _cells.length) {
  70. return 1;
  71. }
  72. return _cells[col].width;
  73. }
  74. bool hasContentAt(int col) {
  75. if (col >= _cells.length) {
  76. return false;
  77. }
  78. return _cells[col].codePoint != 0;
  79. }
  80. }