buffer_line.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import 'dart:typed_data';
  2. import 'package:xterm/terminal/cursor.dart';
  3. /// Line layout:
  4. /// | cell | cell | cell | cell | ...
  5. /// (16 bytes per cell)
  6. ///
  7. /// Cell layout:
  8. /// | code point | fg color | bg color | attributes |
  9. /// 4bytes 4bytes 4bytes 4bytes
  10. ///
  11. /// Attributes layout:
  12. /// | width | flags | reserved | reserved |
  13. /// 1byte 1byte 1byte 1byte
  14. const _cellSize = 16;
  15. const _cellContent = 0;
  16. const _cellFgColor = 4;
  17. const _cellBgColor = 8;
  18. // const _cellAttributes = 12;
  19. const _cellWidth = 12;
  20. const _cellFlags = 13;
  21. class BufferLine {
  22. BufferLine() {
  23. const initLength = 64;
  24. _cells = ByteData(initLength * _cellSize);
  25. }
  26. late ByteData _cells;
  27. bool get isWrapped => _isWrapped;
  28. bool _isWrapped = false;
  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. }
  42. void insert(int index) {
  43. insertN(index, 1);
  44. }
  45. void insertN(int index, int count) {
  46. // start
  47. // +--------------------------|-----------------------------------+
  48. // | | |
  49. // +--------------------------\--\--------------------------------+
  50. // \ \
  51. // \ \
  52. // v v
  53. // +--------------------------|--|--------------------------------+
  54. // | | | |
  55. // +--------------------------|--|--------------------------------+
  56. // start start+offset
  57. final start = (index * _cellSize).clamp(0, _cells.lengthInBytes);
  58. final offset = (count * _cellSize).clamp(0, _cells.lengthInBytes - start);
  59. // move data forward
  60. final cells = _cells.buffer.asInt8List();
  61. for (var i = _cells.lengthInBytes - offset - 1; i >= start; i++) {
  62. cells[i + offset] = cells[i];
  63. }
  64. // set inserted cells to 0
  65. for (var i = start; i < start + offset; i++) {
  66. cells[i] = 0x00;
  67. }
  68. }
  69. void clear() {
  70. removeRange(0, _cells.lengthInBytes ~/ _cellSize);
  71. }
  72. void erase(Cursor cursor, int start, int end) {
  73. ensure(end);
  74. for (var i = start; i < end; i++) {
  75. cellErase(i, cursor);
  76. }
  77. }
  78. void cellInitialize(
  79. int index, {
  80. required int content,
  81. required int width,
  82. required Cursor cursor,
  83. }) {
  84. final cell = index * _cellSize;
  85. _cells.setInt32(cell + _cellContent, content);
  86. _cells.setInt32(cell + _cellFgColor, cursor.fg);
  87. _cells.setInt32(cell + _cellBgColor, cursor.bg);
  88. _cells.setInt8(cell + _cellWidth, width);
  89. _cells.setInt8(cell + _cellFlags, cursor.flags);
  90. }
  91. int cellGetContent(int index) {
  92. return _cells.getInt32(index * _cellSize + _cellContent);
  93. }
  94. void cellSetContent(int index, int content) {
  95. return _cells.setInt32(index * _cellSize + _cellContent, content);
  96. }
  97. int cellGetFgColor(int index) {
  98. return _cells.getInt32(index * _cellSize + _cellFgColor);
  99. }
  100. void cellSetFgColor(int index, int color) {
  101. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  102. }
  103. int cellGetBgColor(int index) {
  104. return _cells.getInt32(index * _cellSize + _cellBgColor);
  105. }
  106. void cellSetBgColor(int index, int color) {
  107. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  108. }
  109. int cellGetFlags(int index) {
  110. return _cells.getInt8(index * _cellSize + _cellFlags);
  111. }
  112. void cellSetFlags(int index, int flags) {
  113. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  114. }
  115. int cellGetWidth(int index) {
  116. return _cells.getInt8(index * _cellSize + _cellWidth);
  117. }
  118. void cellSetWidth(int index, int width) {
  119. _cells.setInt8(index * _cellSize + _cellWidth, width);
  120. }
  121. void cellClearFlags(int index) {
  122. cellSetFlags(index, 0);
  123. }
  124. bool cellHasFlag(int index, int flag) {
  125. return cellGetFlags(index) & flag != 0;
  126. }
  127. void cellSetFlag(int index, int flag) {
  128. cellSetFlags(index, cellGetFlags(index) | flag);
  129. }
  130. void cellErase(int index, Cursor cursor) {
  131. cellSetContent(index, 0x00);
  132. cellSetFgColor(index, cursor.fg);
  133. cellSetBgColor(index, cursor.bg);
  134. cellSetFlags(index, cursor.flags);
  135. }
  136. // int cellGetHash(int index) {
  137. // final cell = index * _cellSize;
  138. // final a = _cells.getInt64(cell);
  139. // final b = _cells.getInt64(cell + 8);
  140. // return a ^ b;
  141. // }
  142. void removeRange(int start, int end) {
  143. // start = start.clamp(0, _cells.length);
  144. // end ??= _cells.length;
  145. // end = end.clamp(start, _cells.length);
  146. // _cells.removeRange(start, end);
  147. for (var index = start; index < end; index++) {
  148. cellSetContent(index, 0x00);
  149. }
  150. }
  151. }