buffer_line.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. int? _currentTrimmedLength;
  39. void ensure(int length) {
  40. if (length <= _maxCols) {
  41. return;
  42. }
  43. final nextLength = _nextLength(length);
  44. final newCells = ByteData(nextLength * _cellSize);
  45. newCells.buffer.asInt64List().setAll(0, _cells.buffer.asInt64List());
  46. _cells = newCells;
  47. _maxCols = nextLength;
  48. }
  49. void insert(int index) {
  50. insertN(index, 1);
  51. }
  52. void removeN(int index, int count) {
  53. final moveStart = index * _cellSize64Bit;
  54. final moveOffset = count * _cellSize64Bit;
  55. final moveEnd = (_maxCols - count) * _cellSize64Bit;
  56. final bufferEnd = _maxCols * _cellSize64Bit;
  57. // move data backward
  58. final cells = _cells.buffer.asInt64List();
  59. for (var i = moveStart; i < moveEnd; i++) {
  60. cells[i] = cells[i + moveOffset];
  61. }
  62. // set empty cells to 0
  63. for (var i = moveEnd; i < bufferEnd; i++) {
  64. cells[i] = 0x00;
  65. }
  66. _currentTrimmedLength = null;
  67. }
  68. void insertN(int index, int count) {
  69. // start
  70. // +--------------------------|-----------------------------------+
  71. // | | |
  72. // +--------------------------\--\--------------------------------+ end
  73. // \ \
  74. // \ \
  75. // v v
  76. // +--------------------------|--|--------------------------------+
  77. // | | | |
  78. // +--------------------------|--|--------------------------------+ end
  79. // start start+offset
  80. final moveStart = index * _cellSize64Bit;
  81. final moveOffset = count * _cellSize64Bit;
  82. final bufferEnd = _maxCols * _cellSize64Bit;
  83. // move data forward
  84. final cells = _cells.buffer.asInt64List();
  85. for (var i = bufferEnd - moveOffset - 1; i >= moveStart; i--) {
  86. cells[i + moveOffset] = cells[i];
  87. }
  88. // set inserted cells to 0
  89. for (var i = moveStart; i < moveStart + moveOffset; i++) {
  90. cells[i] = 0x00;
  91. }
  92. _currentTrimmedLength = null;
  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.setInt64(index * _cellSize, 0x00);
  108. _cells.setInt64(index * _cellSize + 8, 0x00);
  109. _currentTrimmedLength = null;
  110. }
  111. void cellInitialize(
  112. int index, {
  113. required int content,
  114. required int width,
  115. required Cursor cursor,
  116. }) {
  117. final cell = index * _cellSize;
  118. _cells.setInt32(cell + _cellContent, content);
  119. _cells.setInt32(cell + _cellFgColor, cursor.fg);
  120. _cells.setInt32(cell + _cellBgColor, cursor.bg);
  121. _cells.setInt8(cell + _cellWidth, width);
  122. _cells.setInt8(cell + _cellFlags, cursor.flags);
  123. _currentTrimmedLength = null;
  124. }
  125. bool cellHasContent(int index) {
  126. return cellGetContent(index) != 0;
  127. }
  128. int cellGetContent(int index) {
  129. return _cells.getUint32(index * _cellSize + _cellContent);
  130. }
  131. void cellSetContent(int index, int content) {
  132. _cells.setInt32(index * _cellSize + _cellContent, content);
  133. _currentTrimmedLength = null;
  134. }
  135. int cellGetFgColor(int index) {
  136. if (index >= _maxCols) {
  137. return 0;
  138. }
  139. return _cells.getInt32(index * _cellSize + _cellFgColor);
  140. }
  141. void cellSetFgColor(int index, int color) {
  142. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  143. }
  144. int cellGetBgColor(int index) {
  145. if (index >= _maxCols) {
  146. return 0;
  147. }
  148. return _cells.getInt32(index * _cellSize + _cellBgColor);
  149. }
  150. void cellSetBgColor(int index, int color) {
  151. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  152. }
  153. int cellGetFlags(int index) {
  154. if (index >= _maxCols) {
  155. return 0;
  156. }
  157. return _cells.getInt8(index * _cellSize + _cellFlags);
  158. }
  159. void cellSetFlags(int index, int flags) {
  160. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  161. }
  162. int cellGetWidth(int index) {
  163. if (index >= _maxCols) {
  164. return 1;
  165. }
  166. return _cells.getInt8(index * _cellSize + _cellWidth);
  167. }
  168. void cellSetWidth(int index, int width) {
  169. _cells.setInt8(index * _cellSize + _cellWidth, width);
  170. _currentTrimmedLength = null;
  171. }
  172. void cellClearFlags(int index) {
  173. cellSetFlags(index, 0);
  174. }
  175. bool cellHasFlag(int index, int flag) {
  176. if (index >= _maxCols) {
  177. return false;
  178. }
  179. return cellGetFlags(index) & flag != 0;
  180. }
  181. void cellSetFlag(int index, int flag) {
  182. cellSetFlags(index, cellGetFlags(index) | flag);
  183. }
  184. void cellErase(int index, Cursor cursor) {
  185. cellSetContent(index, 0x00);
  186. cellSetFgColor(index, cursor.fg);
  187. cellSetBgColor(index, cursor.bg);
  188. cellSetFlags(index, cursor.flags);
  189. cellSetWidth(index, 0);
  190. _currentTrimmedLength = null;
  191. }
  192. int getTrimmedLength([int? cols]) {
  193. if (_currentTrimmedLength != null) {
  194. return _currentTrimmedLength!;
  195. }
  196. if (cols == null) {
  197. cols = _maxCols;
  198. }
  199. for (var i = cols - 1; i >= 0; i--) {
  200. if (cellGetContent(i) != 0) {
  201. var length = 0;
  202. for (var j = 0; j <= i; j++) {
  203. length += cellGetWidth(j);
  204. }
  205. _currentTrimmedLength = length;
  206. return length;
  207. }
  208. }
  209. _currentTrimmedLength = 0;
  210. return 0;
  211. }
  212. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  213. ensure(dstCol + len);
  214. final intsToCopy = len * _cellSize64Bit;
  215. final srcStart = srcCol * _cellSize64Bit;
  216. final dstStart = dstCol * _cellSize64Bit;
  217. final cells = _cells.buffer.asInt64List();
  218. final srcCells = src._cells.buffer.asInt64List();
  219. for (var i = 0; i < intsToCopy; i++) {
  220. cells[dstStart + i] = srcCells[srcStart + i];
  221. }
  222. _currentTrimmedLength = null;
  223. }
  224. // int cellGetHash(int index) {
  225. // final cell = index * _cellSize;
  226. // final a = _cells.getInt64(cell);
  227. // final b = _cells.getInt64(cell + 8);
  228. // return a ^ b;
  229. // }
  230. void clearRange(int start, int end) {
  231. end = min(end, _maxCols);
  232. // start = start.clamp(0, _cells.length);
  233. // end ??= _cells.length;
  234. // end = end.clamp(start, _cells.length);
  235. // _cells.removeRange(start, end);
  236. for (var index = start; index < end; index++) {
  237. cellClear(index);
  238. }
  239. }
  240. @override
  241. String toString() {
  242. final result = StringBuffer();
  243. for (int i = 0; i < _maxCols; i++) {
  244. final code = cellGetContent(i);
  245. if (code == 0) {
  246. continue;
  247. }
  248. result.writeCharCode(code);
  249. }
  250. return result.toString();
  251. }
  252. String toDebugString(int cols) {
  253. final result = StringBuffer();
  254. final length = getTrimmedLength();
  255. for (int i = 0; i < max(cols, length); i++) {
  256. var code = cellGetContent(i);
  257. if (code == 0) {
  258. if (cellGetWidth(i) == 0) {
  259. code = '_'.runes.first;
  260. } else {
  261. code = cellGetWidth(i).toString().runes.first;
  262. }
  263. }
  264. result.writeCharCode(code);
  265. }
  266. return result.toString();
  267. }
  268. }