buffer_line.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. class BufferLine {
  24. BufferLine({this.isWrapped = false}) {
  25. _cells = ByteData(_maxCols * _cellSize);
  26. }
  27. late ByteData _cells;
  28. bool isWrapped;
  29. int _maxCols = 64;
  30. int? _currentTrimmedLength;
  31. void ensure(int length) {
  32. final expectedLengthInBytes = length * _cellSize;
  33. if (expectedLengthInBytes < _cells.lengthInBytes) {
  34. return;
  35. }
  36. var newLengthInBytes = _cells.lengthInBytes;
  37. while (newLengthInBytes < expectedLengthInBytes) {
  38. newLengthInBytes *= 2;
  39. }
  40. final newCells = ByteData(newLengthInBytes);
  41. newCells.buffer.asInt64List().setAll(0, _cells.buffer.asInt64List());
  42. _cells = newCells;
  43. _maxCols = newLengthInBytes ~/ _cellSize;
  44. }
  45. void insert(int index) {
  46. insertN(index, 1);
  47. }
  48. void removeN(int index, int count) {
  49. final moveStart = index * _cellSize64Bit;
  50. final moveOffset = count * _cellSize64Bit;
  51. final moveEnd = (_maxCols - count) * _cellSize64Bit;
  52. final bufferEnd = _maxCols * _cellSize64Bit;
  53. // move data backward
  54. final cells = _cells.buffer.asInt64List();
  55. for (var i = moveStart; i < moveEnd; i++) {
  56. cells[i] = cells[i + moveOffset];
  57. }
  58. // set empty cells to 0
  59. for (var i = moveEnd; i < bufferEnd; i++) {
  60. cells[i] = 0x00;
  61. }
  62. _currentTrimmedLength = null;
  63. }
  64. void insertN(int index, int count) {
  65. // start
  66. // +--------------------------|-----------------------------------+
  67. // | | |
  68. // +--------------------------\--\--------------------------------+ end
  69. // \ \
  70. // \ \
  71. // v v
  72. // +--------------------------|--|--------------------------------+
  73. // | | | |
  74. // +--------------------------|--|--------------------------------+ end
  75. // start start+offset
  76. final moveStart = index * _cellSize64Bit;
  77. final moveOffset = count * _cellSize64Bit;
  78. final bufferEnd = _maxCols * _cellSize64Bit;
  79. // move data forward
  80. final cells = _cells.buffer.asInt64List();
  81. for (var i = bufferEnd - moveOffset - 1; i >= moveStart; i--) {
  82. cells[i + moveOffset] = cells[i];
  83. }
  84. // set inserted cells to 0
  85. for (var i = moveStart; i < moveStart + moveOffset; i++) {
  86. cells[i] = 0x00;
  87. }
  88. _currentTrimmedLength = null;
  89. }
  90. void clear() {
  91. clearRange(0, _cells.lengthInBytes ~/ _cellSize);
  92. }
  93. void erase(Cursor cursor, int start, int end, [bool resetIsWrapped = false]) {
  94. ensure(end);
  95. for (var i = start; i < end; i++) {
  96. cellErase(i, cursor);
  97. }
  98. if (resetIsWrapped) {
  99. isWrapped = false;
  100. }
  101. }
  102. void cellClear(int index) {
  103. _cells.setInt64(index * _cellSize, 0x00);
  104. _cells.setInt64(index * _cellSize + 8, 0x00);
  105. _currentTrimmedLength = null;
  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. _currentTrimmedLength = null;
  120. }
  121. bool cellHasContent(int index) {
  122. return cellGetContent(index) != 0;
  123. }
  124. int cellGetContent(int index) {
  125. return _cells.getUint32(index * _cellSize + _cellContent);
  126. }
  127. void cellSetContent(int index, int content) {
  128. _cells.setInt32(index * _cellSize + _cellContent, content);
  129. _currentTrimmedLength = null;
  130. }
  131. int cellGetFgColor(int index) {
  132. if (index >= _maxCols) {
  133. return 0;
  134. }
  135. return _cells.getInt32(index * _cellSize + _cellFgColor);
  136. }
  137. void cellSetFgColor(int index, int color) {
  138. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  139. }
  140. int cellGetBgColor(int index) {
  141. if (index >= _maxCols) {
  142. return 0;
  143. }
  144. return _cells.getInt32(index * _cellSize + _cellBgColor);
  145. }
  146. void cellSetBgColor(int index, int color) {
  147. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  148. }
  149. int cellGetFlags(int index) {
  150. if (index >= _maxCols) {
  151. return 0;
  152. }
  153. return _cells.getInt8(index * _cellSize + _cellFlags);
  154. }
  155. void cellSetFlags(int index, int flags) {
  156. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  157. }
  158. int cellGetWidth(int index) {
  159. if (index >= _maxCols) {
  160. return 1;
  161. }
  162. return _cells.getInt8(index * _cellSize + _cellWidth);
  163. }
  164. void cellSetWidth(int index, int width) {
  165. _cells.setInt8(index * _cellSize + _cellWidth, width);
  166. _currentTrimmedLength = null;
  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. _currentTrimmedLength = null;
  187. }
  188. int getTrimmedLength([int? cols]) {
  189. if (_currentTrimmedLength != null) {
  190. return _currentTrimmedLength!;
  191. }
  192. if (cols == null) {
  193. cols = _maxCols;
  194. }
  195. for (var i = cols - 1; i >= 0; i--) {
  196. if (cellGetContent(i) != 0) {
  197. var length = 0;
  198. for (var j = 0; j <= i; j++) {
  199. length += cellGetWidth(j);
  200. }
  201. _currentTrimmedLength = length;
  202. return length;
  203. }
  204. }
  205. _currentTrimmedLength = 0;
  206. return 0;
  207. }
  208. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  209. ensure(dstCol + len);
  210. final intsToCopy = len * _cellSize64Bit;
  211. final srcStart = srcCol * _cellSize64Bit;
  212. final dstStart = dstCol * _cellSize64Bit;
  213. final cells = _cells.buffer.asInt64List();
  214. final srcCells = src._cells.buffer.asInt64List();
  215. for (var i = 0; i < intsToCopy; i++) {
  216. cells[dstStart + i] = srcCells[srcStart + i];
  217. }
  218. _currentTrimmedLength = null;
  219. }
  220. // int cellGetHash(int index) {
  221. // final cell = index * _cellSize;
  222. // final a = _cells.getInt64(cell);
  223. // final b = _cells.getInt64(cell + 8);
  224. // return a ^ b;
  225. // }
  226. void clearRange(int start, int end) {
  227. end = min(end, _maxCols);
  228. // start = start.clamp(0, _cells.length);
  229. // end ??= _cells.length;
  230. // end = end.clamp(start, _cells.length);
  231. // _cells.removeRange(start, end);
  232. for (var index = start; index < end; index++) {
  233. cellClear(index);
  234. }
  235. }
  236. @override
  237. String toString() {
  238. final result = StringBuffer();
  239. for (int i = 0; i < _maxCols; i++) {
  240. final code = cellGetContent(i);
  241. if (code == 0) {
  242. continue;
  243. }
  244. result.writeCharCode(code);
  245. }
  246. return result.toString();
  247. }
  248. String toDebugString(int cols) {
  249. final result = StringBuffer();
  250. final length = getTrimmedLength();
  251. for (int i = 0; i < max(cols, length); i++) {
  252. var code = cellGetContent(i);
  253. if (code == 0) {
  254. if (cellGetWidth(i) == 0) {
  255. code = '_'.runes.first;
  256. } else {
  257. code = cellGetWidth(i).toString().runes.first;
  258. }
  259. }
  260. result.writeCharCode(code);
  261. }
  262. return result.toString();
  263. }
  264. }