buffer_line.dart 7.8 KB

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