buffer_line.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. if (index > _maxCols) {
  125. return 0;
  126. }
  127. return _cells.getUint32(index * _cellSize + _cellContent);
  128. }
  129. void cellSetContent(int index, int content) {
  130. _cells.setInt32(index * _cellSize + _cellContent, content);
  131. }
  132. int cellGetFgColor(int index) {
  133. if (index >= _maxCols) {
  134. return 0;
  135. }
  136. return _cells.getInt32(index * _cellSize + _cellFgColor);
  137. }
  138. void cellSetFgColor(int index, int color) {
  139. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  140. }
  141. int cellGetBgColor(int index) {
  142. if (index >= _maxCols) {
  143. return 0;
  144. }
  145. return _cells.getInt32(index * _cellSize + _cellBgColor);
  146. }
  147. void cellSetBgColor(int index, int color) {
  148. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  149. }
  150. int cellGetFlags(int index) {
  151. if (index >= _maxCols) {
  152. return 0;
  153. }
  154. return _cells.getInt8(index * _cellSize + _cellFlags);
  155. }
  156. void cellSetFlags(int index, int flags) {
  157. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  158. }
  159. int cellGetWidth(int index) {
  160. if (index >= _maxCols) {
  161. return 1;
  162. }
  163. return _cells.getInt8(index * _cellSize + _cellWidth);
  164. }
  165. void cellSetWidth(int index, int width) {
  166. _cells.setInt8(index * _cellSize + _cellWidth, width);
  167. }
  168. void cellClearFlags(int index) {
  169. cellSetFlags(index, 0);
  170. }
  171. bool cellHasFlag(int index, int flag) {
  172. if (index >= _maxCols) {
  173. return false;
  174. }
  175. return cellGetFlags(index) & flag != 0;
  176. }
  177. void cellSetFlag(int index, int flag) {
  178. cellSetFlags(index, cellGetFlags(index) | flag);
  179. }
  180. void cellErase(int index, Cursor cursor) {
  181. cellSetContent(index, 0x00);
  182. cellSetFgColor(index, cursor.fg);
  183. cellSetBgColor(index, cursor.bg);
  184. cellSetFlags(index, cursor.flags);
  185. cellSetWidth(index, 0);
  186. }
  187. int getTrimmedLength([int? cols]) {
  188. if (cols == null) {
  189. cols = _maxCols;
  190. }
  191. for (var i = cols - 1; i >= 0; i--) {
  192. if (cellGetContent(i) != 0) {
  193. // we are at the last cell in this line that has content.
  194. // the length of this line is the index of this cell + 1
  195. // the only exception is that if that last cell is wider
  196. // than 1 then we have to add the diff
  197. final lastCellWidth = cellGetWidth(i);
  198. return i + lastCellWidth;
  199. }
  200. }
  201. return 0;
  202. }
  203. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  204. ensure(dstCol + len);
  205. final intsToCopy = len * _cellSize64Bit;
  206. final srcStart = srcCol * _cellSize64Bit;
  207. final dstStart = dstCol * _cellSize64Bit;
  208. final cells = _cells.buffer.asInt64List();
  209. final srcCells = src._cells.buffer.asInt64List();
  210. for (var i = 0; i < intsToCopy; i++) {
  211. cells[dstStart + i] = srcCells[srcStart + i];
  212. }
  213. }
  214. // int cellGetHash(int index) {
  215. // final cell = index * _cellSize;
  216. // final a = _cells.getInt64(cell);
  217. // final b = _cells.getInt64(cell + 8);
  218. // return a ^ b;
  219. // }
  220. void removeRange(int start, int end) {
  221. end = min(end, _maxCols);
  222. this.removeN(start, end - start);
  223. }
  224. void clearRange(int start, int end) {
  225. end = min(end, _maxCols);
  226. for (var index = start; index < end; index++) {
  227. cellClear(index);
  228. }
  229. }
  230. @override
  231. String toString() {
  232. final result = StringBuffer();
  233. for (int i = 0; i < _maxCols; i++) {
  234. final code = cellGetContent(i);
  235. if (code == 0) {
  236. continue;
  237. }
  238. result.writeCharCode(code);
  239. }
  240. return result.toString();
  241. }
  242. String toDebugString(int cols) {
  243. final result = StringBuffer();
  244. final length = getTrimmedLength();
  245. for (int i = 0; i < max(cols, length); i++) {
  246. var code = cellGetContent(i);
  247. if (code == 0) {
  248. if (cellGetWidth(i) == 0) {
  249. code = '_'.runes.first;
  250. } else {
  251. code = cellGetWidth(i).toString().runes.first;
  252. }
  253. }
  254. result.writeCharCode(code);
  255. }
  256. return result.toString();
  257. }
  258. }