buffer_line.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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;
  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. clearRange(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 cellClear(int index) {
  90. _cells.setInt64(index * _cellSize, 0x00);
  91. _cells.setInt64(index * _cellSize + 8, 0x00);
  92. }
  93. void cellInitialize(
  94. int index, {
  95. required int content,
  96. required int width,
  97. required Cursor cursor,
  98. }) {
  99. final cell = index * _cellSize;
  100. _cells.setInt32(cell + _cellContent, content);
  101. _cells.setInt32(cell + _cellFgColor, cursor.fg);
  102. _cells.setInt32(cell + _cellBgColor, cursor.bg);
  103. _cells.setInt8(cell + _cellWidth, width);
  104. _cells.setInt8(cell + _cellFlags, cursor.flags);
  105. }
  106. bool cellHasContent(int index) {
  107. return cellGetContent(index) != 0;
  108. }
  109. int cellGetContent(int index) {
  110. if (index >= _maxCols) {
  111. return 0;
  112. }
  113. return _cells.getInt32(index * _cellSize + _cellContent);
  114. }
  115. void cellSetContent(int index, int content) {
  116. _cells.setInt32(index * _cellSize + _cellContent, content);
  117. }
  118. int cellGetFgColor(int index) {
  119. if (index >= _maxCols) {
  120. return 0;
  121. }
  122. return _cells.getInt32(index * _cellSize + _cellFgColor);
  123. }
  124. void cellSetFgColor(int index, int color) {
  125. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  126. }
  127. int cellGetBgColor(int index) {
  128. if (index >= _maxCols) {
  129. return 0;
  130. }
  131. return _cells.getInt32(index * _cellSize + _cellBgColor);
  132. }
  133. void cellSetBgColor(int index, int color) {
  134. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  135. }
  136. int cellGetFlags(int index) {
  137. if (index >= _maxCols) {
  138. return 0;
  139. }
  140. return _cells.getInt8(index * _cellSize + _cellFlags);
  141. }
  142. void cellSetFlags(int index, int flags) {
  143. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  144. }
  145. int cellGetWidth(int index) {
  146. if (index >= _maxCols) {
  147. return 1;
  148. }
  149. return _cells.getInt8(index * _cellSize + _cellWidth);
  150. }
  151. void cellSetWidth(int index, int width) {
  152. _cells.setInt8(index * _cellSize + _cellWidth, width);
  153. }
  154. void cellClearFlags(int index) {
  155. cellSetFlags(index, 0);
  156. }
  157. bool cellHasFlag(int index, int flag) {
  158. if (index >= _maxCols) {
  159. return false;
  160. }
  161. return cellGetFlags(index) & flag != 0;
  162. }
  163. void cellSetFlag(int index, int flag) {
  164. cellSetFlags(index, cellGetFlags(index) | flag);
  165. }
  166. void cellErase(int index, Cursor cursor) {
  167. cellSetContent(index, 0x00);
  168. cellSetFgColor(index, cursor.fg);
  169. cellSetBgColor(index, cursor.bg);
  170. cellSetFlags(index, cursor.flags);
  171. cellSetWidth(index, 0);
  172. }
  173. int getTrimmedLength([int? cols]) {
  174. if (cols == null) {
  175. cols = _maxCols;
  176. }
  177. for (int i = cols; i >= 0; i--) {
  178. if (cellGetContent(i) != 0) {
  179. int length = 0;
  180. for (int j = 0; j <= i; j++) {
  181. length += cellGetWidth(j);
  182. }
  183. return length;
  184. }
  185. }
  186. return 0;
  187. }
  188. void copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  189. final dstOffset = dstCol * _cellSize;
  190. final srcOffset = srcCol * _cellSize;
  191. final byteLen = len * _cellSize;
  192. ensure(dstCol + len);
  193. final srcCopyView = src._cells.buffer.asUint8List(srcOffset, byteLen);
  194. _cells.buffer.asUint8List().setAll(dstOffset, srcCopyView);
  195. }
  196. // int cellGetHash(int index) {
  197. // final cell = index * _cellSize;
  198. // final a = _cells.getInt64(cell);
  199. // final b = _cells.getInt64(cell + 8);
  200. // return a ^ b;
  201. // }
  202. void clearRange(int start, int end) {
  203. end = min(end, _maxCols);
  204. // start = start.clamp(0, _cells.length);
  205. // end ??= _cells.length;
  206. // end = end.clamp(start, _cells.length);
  207. // _cells.removeRange(start, end);
  208. for (var index = start; index < end; index++) {
  209. cellClear(index);
  210. }
  211. }
  212. @override
  213. String toString() {
  214. final result = StringBuffer();
  215. for (int i = 0; i < _maxCols; i++) {
  216. final code = cellGetContent(i);
  217. if (code == 0) {
  218. continue;
  219. }
  220. result.writeCharCode(code);
  221. }
  222. return result.toString();
  223. }
  224. String toDebugString(int cols) {
  225. final result = StringBuffer();
  226. final length = getTrimmedLength();
  227. for (int i = 0; i < max(cols, length); i++) {
  228. var code = cellGetContent(i);
  229. if (code == 0) {
  230. if (cellGetWidth(i) == 0) {
  231. code = '_'.runes.first;
  232. } else {
  233. code = cellGetWidth(i).toString().runes.first;
  234. }
  235. }
  236. result.writeCharCode(code);
  237. }
  238. return result.toString();
  239. }
  240. }