buffer_line.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. for(var i=0; i<len; i++) {
  58. _cells[dstCol + i] = src._cells[srcCol + i].clone();
  59. }
  60. }
  61. int getWidthAt(int col) {
  62. if(col >= _cells.length) {
  63. return 1;
  64. }
  65. return _cells[col].width;
  66. }
  67. bool hasContentAt(int col) {
  68. if(col >= _cells.length) {
  69. return false;
  70. }
  71. return _cells[col].codePoint != 0;
  72. }
  73. }