line_bytedata.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import 'dart:math';
  2. import 'dart:typed_data';
  3. import 'package:xterm/buffer/line/line.dart';
  4. import 'package:xterm/terminal/cursor.dart';
  5. /// Line layout:
  6. /// | cell | cell | cell | cell | ...
  7. /// (16 bytes per cell)
  8. ///
  9. /// Cell layout:
  10. /// | code point | fg color | bg color | attributes |
  11. /// 4bytes 4bytes 4bytes 4bytes
  12. ///
  13. /// Attributes layout:
  14. /// | width | flags | reserved | reserved |
  15. /// 1byte 1byte 1byte 1byte
  16. const _cellSize = 16;
  17. const _cellSize64Bit = _cellSize >> 3;
  18. const _cellContent = 0;
  19. const _cellFgColor = 4;
  20. const _cellBgColor = 8;
  21. // const _cellAttributes = 12;
  22. const _cellWidth = 12;
  23. const _cellFlags = 13;
  24. int _nextLength(int lengthRequirement) {
  25. var nextLength = 2;
  26. while (nextLength < lengthRequirement) {
  27. nextLength *= 2;
  28. }
  29. return nextLength;
  30. }
  31. /// [ByteData] based [BufferLine], used in non-web platforms to minimize memory
  32. /// footprint,
  33. class ByteDataBufferLine with BufferLine {
  34. ByteDataBufferLine(int length, this.isWrapped) {
  35. _maxCols = _nextLength(length);
  36. _cells = ByteData(_maxCols * _cellSize);
  37. }
  38. late ByteData _cells;
  39. bool isWrapped;
  40. int _maxCols = 64;
  41. void ensure(int length) {
  42. if (length <= _maxCols) {
  43. return;
  44. }
  45. final nextLength = _nextLength(length);
  46. final newCells = ByteData(nextLength * _cellSize);
  47. newCells.buffer.asInt64List().setAll(0, _cells.buffer.asInt64List());
  48. _cells = newCells;
  49. _maxCols = nextLength;
  50. }
  51. void insert(int index) {
  52. insertN(index, 1);
  53. }
  54. void removeN(int index, int count) {
  55. final moveStart = index * _cellSize64Bit;
  56. final moveOffset = count * _cellSize64Bit;
  57. final moveEnd = (_maxCols - count) * _cellSize64Bit;
  58. final bufferEnd = _maxCols * _cellSize64Bit;
  59. // move data backward
  60. final cells = _cells.buffer.asInt64List();
  61. for (var i = moveStart; i < moveEnd; i++) {
  62. cells[i] = cells[i + moveOffset];
  63. }
  64. // set empty cells to 0
  65. for (var i = moveEnd; i < bufferEnd; i++) {
  66. cells[i] = 0x00;
  67. }
  68. }
  69. void insertN(int index, int count) {
  70. // start
  71. // +--------------------------|-----------------------------------+
  72. // | | |
  73. // +--------------------------\--\--------------------------------+ end
  74. // \ \
  75. // \ \
  76. // v v
  77. // +--------------------------|--|--------------------------------+
  78. // | | | |
  79. // +--------------------------|--|--------------------------------+ end
  80. // start start+offset
  81. final moveStart = index * _cellSize64Bit;
  82. final moveOffset = count * _cellSize64Bit;
  83. final bufferEnd = _maxCols * _cellSize64Bit;
  84. // move data forward
  85. final cells = _cells.buffer.asInt64List();
  86. for (var i = bufferEnd - moveOffset - 1; i >= moveStart; i--) {
  87. cells[i + moveOffset] = cells[i];
  88. }
  89. // set inserted cells to 0
  90. for (var i = moveStart; i < moveStart + moveOffset; i++) {
  91. cells[i] = 0x00;
  92. }
  93. }
  94. void clear() {
  95. clearRange(0, _cells.lengthInBytes ~/ _cellSize);
  96. }
  97. void erase(Cursor cursor, int start, int end, [bool resetIsWrapped = false]) {
  98. ensure(end);
  99. for (var i = start; i < end; i++) {
  100. cellErase(i, cursor);
  101. }
  102. if (resetIsWrapped) {
  103. isWrapped = false;
  104. }
  105. }
  106. void cellClear(int index) {
  107. _cells.setUint64(index * _cellSize, 0x00);
  108. _cells.setUint64(index * _cellSize + 8, 0x00);
  109. }
  110. void cellInitialize(
  111. int index, {
  112. required int content,
  113. required int width,
  114. required Cursor cursor,
  115. }) {
  116. final cell = index * _cellSize;
  117. _cells.setUint32(cell + _cellContent, content);
  118. _cells.setUint32(cell + _cellFgColor, cursor.fg);
  119. _cells.setUint32(cell + _cellBgColor, cursor.bg);
  120. _cells.setUint8(cell + _cellWidth, width);
  121. _cells.setUint8(cell + _cellFlags, cursor.flags);
  122. }
  123. bool cellHasContent(int index) {
  124. return cellGetContent(index) != 0;
  125. }
  126. int cellGetContent(int index) {
  127. if (index > _maxCols) {
  128. return 0;
  129. }
  130. return _cells.getUint32(index * _cellSize + _cellContent);
  131. }
  132. void cellSetContent(int index, int content) {
  133. _cells.setInt32(index * _cellSize + _cellContent, content);
  134. }
  135. int cellGetFgColor(int index) {
  136. if (index >= _maxCols) {
  137. return 0;
  138. }
  139. return _cells.getUint32(index * _cellSize + _cellFgColor);
  140. }
  141. void cellSetFgColor(int index, int color) {
  142. _cells.setUint32(index * _cellSize + _cellFgColor, color);
  143. }
  144. int cellGetBgColor(int index) {
  145. if (index >= _maxCols) {
  146. return 0;
  147. }
  148. return _cells.getUint32(index * _cellSize + _cellBgColor);
  149. }
  150. void cellSetBgColor(int index, int color) {
  151. _cells.setUint32(index * _cellSize + _cellBgColor, color);
  152. }
  153. int cellGetFlags(int index) {
  154. if (index >= _maxCols) {
  155. return 0;
  156. }
  157. return _cells.getUint8(index * _cellSize + _cellFlags);
  158. }
  159. void cellSetFlags(int index, int flags) {
  160. _cells.setUint8(index * _cellSize + _cellFlags, flags);
  161. }
  162. int cellGetWidth(int index) {
  163. if (index >= _maxCols) {
  164. return 1;
  165. }
  166. return _cells.getUint8(index * _cellSize + _cellWidth);
  167. }
  168. void cellSetWidth(int index, int width) {
  169. _cells.setUint8(index * _cellSize + _cellWidth, width);
  170. }
  171. void cellClearFlags(int index) {
  172. cellSetFlags(index, 0);
  173. }
  174. bool cellHasFlag(int index, int flag) {
  175. if (index >= _maxCols) {
  176. return false;
  177. }
  178. return cellGetFlags(index) & flag != 0;
  179. }
  180. void cellSetFlag(int index, int flag) {
  181. cellSetFlags(index, cellGetFlags(index) | flag);
  182. }
  183. void cellErase(int index, Cursor cursor) {
  184. cellSetContent(index, 0x00);
  185. cellSetFgColor(index, cursor.fg);
  186. cellSetBgColor(index, cursor.bg);
  187. cellSetFlags(index, cursor.flags);
  188. cellSetWidth(index, 0);
  189. }
  190. int getTrimmedLength([int? cols]) {
  191. if (cols == null) {
  192. cols = _maxCols;
  193. }
  194. for (var i = cols - 1; i >= 0; i--) {
  195. if (cellGetContent(i) != 0) {
  196. // we are at the last cell in this line that has content.
  197. // the length of this line is the index of this cell + 1
  198. // the only exception is that if that last cell is wider
  199. // than 1 then we have to add the diff
  200. final lastCellWidth = cellGetWidth(i);
  201. return i + lastCellWidth;
  202. }
  203. }
  204. return 0;
  205. }
  206. void copyCellsFrom(ByteDataBufferLine src, int srcCol, int dstCol, int len) {
  207. ensure(dstCol + len);
  208. final intsToCopy = len * _cellSize64Bit;
  209. final srcStart = srcCol * _cellSize64Bit;
  210. final dstStart = dstCol * _cellSize64Bit;
  211. final cells = _cells.buffer.asInt64List();
  212. final srcCells = src._cells.buffer.asInt64List();
  213. for (var i = 0; i < intsToCopy; i++) {
  214. cells[dstStart + i] = srcCells[srcStart + i];
  215. }
  216. }
  217. // int cellGetHash(int index) {
  218. // final cell = index * _cellSize;
  219. // final a = _cells.getInt64(cell);
  220. // final b = _cells.getInt64(cell + 8);
  221. // return a ^ b;
  222. // }
  223. void removeRange(int start, int end) {
  224. end = min(end, _maxCols);
  225. this.removeN(start, end - start);
  226. }
  227. void clearRange(int start, int end) {
  228. end = min(end, _maxCols);
  229. for (var index = start; index < end; index++) {
  230. cellClear(index);
  231. }
  232. }
  233. @override
  234. String toString() {
  235. final result = StringBuffer();
  236. for (int i = 0; i < _maxCols; i++) {
  237. final code = cellGetContent(i);
  238. if (code == 0) {
  239. continue;
  240. }
  241. result.writeCharCode(code);
  242. }
  243. return result.toString();
  244. }
  245. }