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