buffer_line.dart 7.3 KB

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