buffer_line.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. }
  168. int getTrimmedLength(int cols) {
  169. for (int i = cols; i >= 0; i--) {
  170. if (cellGetContent(i) != 0) {
  171. int length = 0;
  172. for (int j = 0; j <= i; j++) {
  173. length += cellGetWidth(j);
  174. }
  175. return length;
  176. }
  177. }
  178. return 0;
  179. }
  180. copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  181. final dstOffset = dstCol * _cellSize;
  182. final srcOffset = srcCol * _cellSize;
  183. final byteLen = len * _cellSize;
  184. ensure(dstCol + len);
  185. final srcCopyView = src._cells.buffer.asUint8List(srcOffset, byteLen);
  186. _cells.buffer.asUint8List().setAll(dstOffset, srcCopyView);
  187. }
  188. // int cellGetHash(int index) {
  189. // final cell = index * _cellSize;
  190. // final a = _cells.getInt64(cell);
  191. // final b = _cells.getInt64(cell + 8);
  192. // return a ^ b;
  193. // }
  194. void removeRange(int start, int end) {
  195. end = min(end, _maxCols);
  196. // start = start.clamp(0, _cells.length);
  197. // end ??= _cells.length;
  198. // end = end.clamp(start, _cells.length);
  199. // _cells.removeRange(start, end);
  200. for (var index = start; index < end; index++) {
  201. cellSetContent(index, 0x00);
  202. }
  203. }
  204. }