buffer_line.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 insertN(int index, int count) {
  47. // start
  48. // +--------------------------|-----------------------------------+
  49. // | | |
  50. // +--------------------------\--\--------------------------------+
  51. // \ \
  52. // \ \
  53. // v v
  54. // +--------------------------|--|--------------------------------+
  55. // | | | |
  56. // +--------------------------|--|--------------------------------+
  57. // start start+offset
  58. final start = (index * _cellSize).clamp(0, _cells.lengthInBytes);
  59. final offset = (count * _cellSize).clamp(0, _cells.lengthInBytes - start);
  60. // move data forward
  61. final cells = _cells.buffer.asInt8List();
  62. for (var i = _cells.lengthInBytes - offset - 1; i >= start; i++) {
  63. cells[i + offset] = cells[i];
  64. }
  65. // set inserted cells to 0
  66. for (var i = start; i < start + offset; i++) {
  67. cells[i] = 0x00;
  68. }
  69. }
  70. void clear() {
  71. removeRange(0, (_cells.lengthInBytes / _cellSize).floor());
  72. }
  73. void erase(Cursor cursor, int start, int end, [bool resetIsWrapped = false]) {
  74. ensure(end);
  75. for (var i = start; i < end; i++) {
  76. cellErase(i, cursor);
  77. }
  78. if (resetIsWrapped) {
  79. isWrapped = false;
  80. }
  81. }
  82. void cellInitialize(
  83. int index, {
  84. required int content,
  85. required int width,
  86. required Cursor cursor,
  87. }) {
  88. final cell = index * _cellSize;
  89. _cells.setInt32(cell + _cellContent, content);
  90. _cells.setInt32(cell + _cellFgColor, cursor.fg);
  91. _cells.setInt32(cell + _cellBgColor, cursor.bg);
  92. _cells.setInt8(cell + _cellWidth, width);
  93. _cells.setInt8(cell + _cellFlags, cursor.flags);
  94. }
  95. bool cellHasContent(int index) {
  96. return cellGetContent(index) != 0;
  97. }
  98. int cellGetContent(int index) {
  99. if (index >= _maxCols) {
  100. return 0;
  101. }
  102. return _cells.getInt32(index * _cellSize + _cellContent);
  103. }
  104. void cellSetContent(int index, int content) {
  105. return _cells.setInt32(index * _cellSize + _cellContent, content);
  106. }
  107. int cellGetFgColor(int index) {
  108. if (index >= _maxCols) {
  109. return 0;
  110. }
  111. return _cells.getInt32(index * _cellSize + _cellFgColor);
  112. }
  113. void cellSetFgColor(int index, int color) {
  114. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  115. }
  116. int cellGetBgColor(int index) {
  117. if (index >= _maxCols) {
  118. return 0;
  119. }
  120. return _cells.getInt32(index * _cellSize + _cellBgColor);
  121. }
  122. void cellSetBgColor(int index, int color) {
  123. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  124. }
  125. int cellGetFlags(int index) {
  126. if (index >= _maxCols) {
  127. return 0;
  128. }
  129. return _cells.getInt8(index * _cellSize + _cellFlags);
  130. }
  131. void cellSetFlags(int index, int flags) {
  132. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  133. }
  134. int cellGetWidth(int index) {
  135. if (index >= _maxCols) {
  136. return 1;
  137. }
  138. return _cells.getInt8(index * _cellSize + _cellWidth);
  139. }
  140. void cellSetWidth(int index, int width) {
  141. _cells.setInt8(index * _cellSize + _cellWidth, width);
  142. }
  143. void cellClearFlags(int index) {
  144. cellSetFlags(index, 0);
  145. }
  146. bool cellHasFlag(int index, int flag) {
  147. if (index >= _maxCols) {
  148. return false;
  149. }
  150. return cellGetFlags(index) & flag != 0;
  151. }
  152. void cellSetFlag(int index, int flag) {
  153. cellSetFlags(index, cellGetFlags(index) | flag);
  154. }
  155. void cellErase(int index, Cursor cursor) {
  156. cellSetContent(index, 0x00);
  157. cellSetFgColor(index, cursor.fg);
  158. cellSetBgColor(index, cursor.bg);
  159. cellSetFlags(index, cursor.flags);
  160. }
  161. int getTrimmedLength(int cols) {
  162. for (int i = cols; i >= 0; i--) {
  163. if (cellGetContent(i) != 0) {
  164. int length = 0;
  165. for (int j = 0; j <= i; j++) {
  166. length += cellGetWidth(j);
  167. }
  168. return length;
  169. }
  170. }
  171. return 0;
  172. }
  173. copyCellsFrom(BufferLine src, int srcCol, int dstCol, int len) {
  174. final dstOffset = dstCol * _cellSize;
  175. final srcOffset = srcCol * _cellSize;
  176. final byteLen = len * _cellSize;
  177. final srcCopyView = src._cells.buffer.asUint8List(srcOffset, byteLen);
  178. _cells.buffer.asUint8List().setAll(dstOffset, srcCopyView);
  179. }
  180. // int cellGetHash(int index) {
  181. // final cell = index * _cellSize;
  182. // final a = _cells.getInt64(cell);
  183. // final b = _cells.getInt64(cell + 8);
  184. // return a ^ b;
  185. // }
  186. void removeRange(int start, int end) {
  187. end = min(end, _maxCols);
  188. // start = start.clamp(0, _cells.length);
  189. // end ??= _cells.length;
  190. // end = end.clamp(start, _cells.length);
  191. // _cells.removeRange(start, end);
  192. for (var index = start; index < end; index++) {
  193. cellSetContent(index, 0x00);
  194. }
  195. }
  196. }