buffer_line.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import 'dart:math';
  2. import 'dart:typed_data';
  3. import 'package:xterm/terminal/cursor.dart';
  4. /// Line layout:
  5. /// | cell | cell | cell | cell | ...
  6. /// (16 bytes per cell)
  7. ///
  8. /// Cell layout:
  9. /// | code point | fg color | bg color | attributes |
  10. /// 4bytes 4bytes 4bytes 4bytes
  11. ///
  12. /// Attributes layout:
  13. /// | width | flags | reserved | reserved |
  14. /// 1byte 1byte 1byte 1byte
  15. const _cellSize = 16;
  16. const _cellContent = 0;
  17. const _cellFgColor = 4;
  18. const _cellBgColor = 8;
  19. // const _cellAttributes = 12;
  20. const _cellWidth = 12;
  21. const _cellFlags = 13;
  22. class BufferLine {
  23. BufferLine({bool isWrapped = false}) : _isWrapped = isWrapped {
  24. _cells = ByteData(_maxCols * _cellSize);
  25. }
  26. late ByteData _cells;
  27. bool get isWrapped => _isWrapped;
  28. bool _isWrapped = false;
  29. int _maxCols = 64;
  30. void ensure(int length) {
  31. final expectedLengthInBytes = length * _cellSize;
  32. if (expectedLengthInBytes < _cells.lengthInBytes) {
  33. return;
  34. }
  35. var newLengthInBytes = _cells.lengthInBytes;
  36. while (newLengthInBytes < expectedLengthInBytes) {
  37. newLengthInBytes *= 2;
  38. }
  39. final newCells = ByteData(newLengthInBytes);
  40. newCells.buffer.asInt64List().setAll(0, _cells.buffer.asInt64List());
  41. _cells = newCells;
  42. _maxCols = (newLengthInBytes / _cellSize).floor();
  43. }
  44. void insert(int index) {
  45. insertN(index, 1);
  46. }
  47. void insertN(int index, int count) {
  48. // start
  49. // +--------------------------|-----------------------------------+
  50. // | | |
  51. // +--------------------------\--\--------------------------------+
  52. // \ \
  53. // \ \
  54. // v v
  55. // +--------------------------|--|--------------------------------+
  56. // | | | |
  57. // +--------------------------|--|--------------------------------+
  58. // start start+offset
  59. final start = (index * _cellSize).clamp(0, _cells.lengthInBytes);
  60. final offset = (count * _cellSize).clamp(0, _cells.lengthInBytes - start);
  61. // move data forward
  62. final cells = _cells.buffer.asInt8List();
  63. for (var i = _cells.lengthInBytes - offset - 1; i >= start; i++) {
  64. cells[i + offset] = cells[i];
  65. }
  66. // set inserted cells to 0
  67. for (var i = start; i < start + offset; i++) {
  68. cells[i] = 0x00;
  69. }
  70. }
  71. void clear() {
  72. _cells.buffer.asInt64List().clear();
  73. }
  74. void erase(Cursor cursor, int start, int end, [bool resetIsWrapped = false]) {
  75. ensure(end);
  76. for (var i = start; i < end; i++) {
  77. cellErase(i, cursor);
  78. }
  79. if (resetIsWrapped) {
  80. _isWrapped = false;
  81. }
  82. }
  83. void cellInitialize(
  84. int index, {
  85. required int content,
  86. required int width,
  87. required Cursor cursor,
  88. }) {
  89. final cell = index * _cellSize;
  90. _cells.setInt32(cell + _cellContent, content);
  91. _cells.setInt32(cell + _cellFgColor, cursor.fg);
  92. _cells.setInt32(cell + _cellBgColor, cursor.bg);
  93. _cells.setInt8(cell + _cellWidth, width);
  94. _cells.setInt8(cell + _cellFlags, cursor.flags);
  95. }
  96. bool cellHasContent(int index) {
  97. return cellGetContent(index) != 0;
  98. }
  99. int cellGetContent(int index) {
  100. if (index >= _maxCols) {
  101. return 0;
  102. }
  103. return _cells.getInt32(index * _cellSize + _cellContent);
  104. }
  105. void cellSetContent(int index, int content) {
  106. return _cells.setInt32(index * _cellSize + _cellContent, content);
  107. }
  108. int cellGetFgColor(int index) {
  109. if (index >= _maxCols) {
  110. return 0;
  111. }
  112. return _cells.getInt32(index * _cellSize + _cellFgColor);
  113. }
  114. void cellSetFgColor(int index, int color) {
  115. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  116. }
  117. int cellGetBgColor(int index) {
  118. if (index >= _maxCols) {
  119. return 0;
  120. }
  121. return _cells.getInt32(index * _cellSize + _cellBgColor);
  122. }
  123. void cellSetBgColor(int index, int color) {
  124. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  125. }
  126. int cellGetFlags(int index) {
  127. if (index >= _maxCols) {
  128. return 0;
  129. }
  130. return _cells.getInt8(index * _cellSize + _cellFlags);
  131. }
  132. void cellSetFlags(int index, int flags) {
  133. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  134. }
  135. int cellGetWidth(int index) {
  136. if (index >= _maxCols) {
  137. return 1;
  138. }
  139. return _cells.getInt8(index * _cellSize + _cellWidth);
  140. }
  141. void cellSetWidth(int index, int width) {
  142. _cells.setInt8(index * _cellSize + _cellWidth, width);
  143. }
  144. void cellClearFlags(int index) {
  145. cellSetFlags(index, 0);
  146. }
  147. bool cellHasFlag(int index, int flag) {
  148. if (index >= _maxCols) {
  149. return false;
  150. }
  151. return cellGetFlags(index) & flag != 0;
  152. }
  153. void cellSetFlag(int index, int flag) {
  154. cellSetFlags(index, cellGetFlags(index) | flag);
  155. }
  156. void cellErase(int index, Cursor cursor) {
  157. cellSetContent(index, 0x00);
  158. cellSetFgColor(index, cursor.fg);
  159. cellSetBgColor(index, cursor.bg);
  160. cellSetFlags(index, cursor.flags);
  161. }
  162. int getTrimmedLength(int cols) {
  163. for (int i = cols; i >= 0; i--) {
  164. if (cellGetContent(i) != 0) {
  165. int length = 0;
  166. for (int j = 0; j <= i; j++) {
  167. length += cellGetWidth(j);
  168. }
  169. return length;
  170. }
  171. }
  172. return 0;
  173. }
  174. copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  175. final dstOffset = dstCol * _cellSize;
  176. final srcOffset = srcCol * _cellSize;
  177. final byteLen = len * _cellSize;
  178. final srcCopyView = src._cells.buffer.asUint8List(srcOffset, byteLen);
  179. _cells.buffer.asUint8List().setAll(dstOffset, srcCopyView);
  180. }
  181. // int cellGetHash(int index) {
  182. // final cell = index * _cellSize;
  183. // final a = _cells.getInt64(cell);
  184. // final b = _cells.getInt64(cell + 8);
  185. // return a ^ b;
  186. // }
  187. void removeRange(int start, int end) {
  188. end = min(end, _maxCols);
  189. // start = start.clamp(0, _cells.length);
  190. // end ??= _cells.length;
  191. // end = end.clamp(start, _cells.length);
  192. // _cells.removeRange(start, end);
  193. for (var index = start; index < end; index++) {
  194. cellSetContent(index, 0x00);
  195. }
  196. }
  197. }