buffer_line.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. if (index >= _maxCols) {
  126. return 0;
  127. }
  128. return _cells.getInt32(index * _cellSize + _cellContent);
  129. }
  130. void cellSetContent(int index, int content) {
  131. _cells.setInt32(index * _cellSize + _cellContent, content);
  132. _currentTrimmedLength = null;
  133. }
  134. int cellGetFgColor(int index) {
  135. if (index >= _maxCols) {
  136. return 0;
  137. }
  138. return _cells.getInt32(index * _cellSize + _cellFgColor);
  139. }
  140. void cellSetFgColor(int index, int color) {
  141. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  142. }
  143. int cellGetBgColor(int index) {
  144. if (index >= _maxCols) {
  145. return 0;
  146. }
  147. return _cells.getInt32(index * _cellSize + _cellBgColor);
  148. }
  149. void cellSetBgColor(int index, int color) {
  150. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  151. }
  152. int cellGetFlags(int index) {
  153. if (index >= _maxCols) {
  154. return 0;
  155. }
  156. return _cells.getInt8(index * _cellSize + _cellFlags);
  157. }
  158. void cellSetFlags(int index, int flags) {
  159. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  160. }
  161. int cellGetWidth(int index) {
  162. if (index >= _maxCols) {
  163. return 1;
  164. }
  165. return _cells.getInt8(index * _cellSize + _cellWidth);
  166. }
  167. void cellSetWidth(int index, int width) {
  168. _cells.setInt8(index * _cellSize + _cellWidth, width);
  169. _currentTrimmedLength = null;
  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. _currentTrimmedLength = null;
  190. }
  191. int getTrimmedLength([int? cols]) {
  192. if (_currentTrimmedLength != null) {
  193. return _currentTrimmedLength!;
  194. }
  195. if (cols == null) {
  196. cols = _maxCols;
  197. }
  198. for (int i = cols; i >= 0; i--) {
  199. if (cellGetContent(i) != 0) {
  200. int length = 0;
  201. for (int j = 0; j <= i; j++) {
  202. length += cellGetWidth(j);
  203. }
  204. _currentTrimmedLength = length;
  205. return length;
  206. }
  207. }
  208. _currentTrimmedLength = 0;
  209. return 0;
  210. }
  211. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  212. ensure(dstCol + len);
  213. final intsToCopy = len * _cellSize64Bit;
  214. final srcStart = srcCol * _cellSize64Bit;
  215. final dstStart = dstCol * _cellSize64Bit;
  216. final cells = _cells.buffer.asInt64List();
  217. final srcCells = src._cells.buffer.asInt64List();
  218. for (var i = 0; i < intsToCopy; i++) {
  219. cells[dstStart + i] = srcCells[srcStart + i];
  220. }
  221. _currentTrimmedLength = null;
  222. }
  223. // int cellGetHash(int index) {
  224. // final cell = index * _cellSize;
  225. // final a = _cells.getInt64(cell);
  226. // final b = _cells.getInt64(cell + 8);
  227. // return a ^ b;
  228. // }
  229. void clearRange(int start, int end) {
  230. end = min(end, _maxCols);
  231. // start = start.clamp(0, _cells.length);
  232. // end ??= _cells.length;
  233. // end = end.clamp(start, _cells.length);
  234. // _cells.removeRange(start, end);
  235. for (var index = start; index < end; index++) {
  236. cellClear(index);
  237. }
  238. }
  239. @override
  240. String toString() {
  241. final result = StringBuffer();
  242. for (int i = 0; i < _maxCols; i++) {
  243. final code = cellGetContent(i);
  244. if (code == 0) {
  245. continue;
  246. }
  247. result.writeCharCode(code);
  248. }
  249. return result.toString();
  250. }
  251. String toDebugString(int cols) {
  252. final result = StringBuffer();
  253. final length = getTrimmedLength();
  254. for (int i = 0; i < max(cols, length); i++) {
  255. var code = cellGetContent(i);
  256. if (code == 0) {
  257. if (cellGetWidth(i) == 0) {
  258. code = '_'.runes.first;
  259. } else {
  260. code = cellGetWidth(i).toString().runes.first;
  261. }
  262. }
  263. result.writeCharCode(code);
  264. }
  265. return result.toString();
  266. }
  267. }