| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import 'package:xterm/buffer/cell.dart';
- import 'package:xterm/buffer/cell_attr.dart';
- class BufferLine {
- final _cells = <Cell>[];
- bool _isWrapped = false;
- BufferLine({isWrapped = false}) {
- _isWrapped = isWrapped;
- }
- bool get isWrapped {
- return _isWrapped;
- }
- int get length {
- return _cells.length;
- }
- void add(Cell cell) {
- _cells.add(cell);
- }
- void insert(int index, Cell cell) {
- _cells.insert(index, cell);
- }
- void clear() {
- _cells.clear();
- }
- int getTrimmedLength() {
- int width = 0;
- for (int i = 0; i < _cells.length; i++)
- if (_cells[i].codePoint != null && _cells[i].codePoint != 0) {
- width += _cells[i].width;
- } else {
- return width;
- }
- return width;
- }
- void erase(CellAttr attr, int start, int end, bool clearWrap) {
- for (var i = start; i < end; i++) {
- if (i >= length) {
- add(Cell(attr: attr));
- } else {
- getCell(i).erase(attr);
- }
- }
- if (_isWrapped && clearWrap) {
- _isWrapped = false;
- }
- }
- Cell getCell(int index) {
- return _cells[index];
- }
- void removeRange(int start, [int? end]) {
- start = start.clamp(0, _cells.length);
- end ??= _cells.length;
- end = end.clamp(start, _cells.length);
- _cells.removeRange(start, end);
- }
- void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
- final requiredCells = dstCol + len;
- if (_cells.length < requiredCells) {
- _cells.addAll(List<Cell>.generate(
- requiredCells - _cells.length, (index) => Cell()));
- }
- //we have to make a copy first as src and dst might be the same line
- List<Cell> sourceCells =
- List<Cell>.generate(len, (index) => src._cells[srcCol + index].clone());
- for (var i = 0; i < len; i++) {
- _cells[dstCol + i] = sourceCells[i];
- }
- }
- int getWidthAt(int col) {
- if (col >= _cells.length) {
- return 1;
- }
- return _cells[col].width;
- }
- bool hasContentAt(int col) {
- if (col >= _cells.length) {
- return false;
- }
- return _cells[col].codePoint != 0;
- }
- }
|