buffer_line.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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({int numOfCells = 0, attr}) {
  7. _cells.addAll(List<Cell>.generate(
  8. numOfCells, (index) => Cell(codePoint: 0, attr: attr)));
  9. }
  10. bool get isWrapped {
  11. return _isWrapped;
  12. }
  13. int get length {
  14. return _cells.length;
  15. }
  16. void add(Cell cell) {
  17. _cells.add(cell);
  18. }
  19. void insert(int index, Cell cell) {
  20. _cells.insert(index, cell);
  21. }
  22. void clear() {
  23. _cells.clear();
  24. }
  25. int getTrimmedLength() {
  26. for (int i = _cells.length - 1; i >= 0; --i)
  27. if (_cells[i].codePoint != 0) {
  28. int width = 0;
  29. for (int j = 0; j <= i; j++) width += _cells[i].width;
  30. return width;
  31. }
  32. return 0;
  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. List.copyRange(_cells, dstCol, src._cells, srcCol, srcCol + len);
  54. }
  55. int getWidthAt(int col) {
  56. return _cells[col].width;
  57. }
  58. bool hasContentAt(int col) {
  59. return _cells[col].codePoint != 0;
  60. }
  61. void resize(int width, {bool erase = false}) {
  62. var missing = width - _cells.length;
  63. if (missing > 0) {
  64. _cells.addAll(List<Cell>.generate(missing, (index) => Cell()));
  65. }
  66. if (missing < 0 && erase) {
  67. _cells.removeRange(width, _cells.length - 1);
  68. }
  69. }
  70. }