buffer_line.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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) {
  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. }
  43. Cell getCell(int index) {
  44. return _cells[index];
  45. }
  46. void removeRange(int start, [int? end]) {
  47. start = start.clamp(0, _cells.length);
  48. end ??= _cells.length;
  49. end = end.clamp(start, _cells.length);
  50. _cells.removeRange(start, end);
  51. }
  52. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  53. final requiredCells = dstCol + len;
  54. if(_cells.length < requiredCells) {
  55. _cells.addAll(List<Cell>.generate(requiredCells - _cells.length, (index) => Cell()));
  56. }
  57. //we have to make a copy first as src and dst might be the same line
  58. List<Cell> sourceCells = List<Cell>.generate(len, (index) => src._cells[srcCol + index].clone());
  59. for(var i=0; i<len; i++) {
  60. _cells[dstCol + i] = sourceCells[i];
  61. }
  62. }
  63. int getWidthAt(int col) {
  64. if(col >= _cells.length) {
  65. return 1;
  66. }
  67. return _cells[col].width;
  68. }
  69. bool hasContentAt(int col) {
  70. if(col >= _cells.length) {
  71. return false;
  72. }
  73. return _cells[col].codePoint != 0;
  74. }
  75. }