buffer_line.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // _cells.insert(index, cell);
  44. }
  45. void insertN(int index, int count) {
  46. // _cells.insert(index, cell);
  47. }
  48. void clear() {
  49. // _cells.clear();
  50. }
  51. void erase(Cursor cursor, int start, int end) {
  52. ensure(end);
  53. for (var i = start; i < end; i++) {
  54. cellErase(i, cursor);
  55. }
  56. }
  57. void cellInitialize(
  58. int index, {
  59. required int content,
  60. required int width,
  61. required Cursor cursor,
  62. }) {
  63. final cell = index * _cellSize;
  64. _cells.setInt32(cell + _cellContent, content);
  65. _cells.setInt32(cell + _cellFgColor, cursor.fg);
  66. _cells.setInt32(cell + _cellBgColor, cursor.bg);
  67. _cells.setInt8(cell + _cellWidth, width);
  68. _cells.setInt8(cell + _cellFlags, cursor.flags);
  69. }
  70. int cellGetContent(int index) {
  71. return _cells.getInt32(index * _cellSize + _cellContent);
  72. }
  73. void cellSetContent(int index, int content) {
  74. return _cells.setInt32(index * _cellSize + _cellContent, content);
  75. }
  76. int cellGetFgColor(int index) {
  77. return _cells.getInt32(index * _cellSize + _cellFgColor);
  78. }
  79. void cellSetFgColor(int index, int color) {
  80. _cells.setInt32(index * _cellSize + _cellFgColor, color);
  81. }
  82. int cellGetBgColor(int index) {
  83. return _cells.getInt32(index * _cellSize + _cellBgColor);
  84. }
  85. void cellSetBgColor(int index, int color) {
  86. _cells.setInt32(index * _cellSize + _cellBgColor, color);
  87. }
  88. int cellGetFlags(int index) {
  89. return _cells.getInt8(index * _cellSize + _cellFlags);
  90. }
  91. void cellSetFlags(int index, int flags) {
  92. _cells.setInt8(index * _cellSize + _cellFlags, flags);
  93. }
  94. int cellGetWidth(int index) {
  95. return _cells.getInt8(index * _cellSize + _cellWidth);
  96. }
  97. void cellSetWidth(int index, int width) {
  98. _cells.setInt8(index * _cellSize + _cellWidth, width);
  99. }
  100. void cellClearFlags(int index) {
  101. cellSetFlags(index, 0);
  102. }
  103. bool cellHasFlag(int index, int flag) {
  104. return cellGetFlags(index) & flag != 0;
  105. }
  106. void cellSetFlag(int index, int flag) {
  107. cellSetFlags(index, cellGetFlags(index) | flag);
  108. }
  109. void cellErase(int index, Cursor cursor) {
  110. cellSetContent(index, 0x00);
  111. cellSetFgColor(index, cursor.fg);
  112. cellSetBgColor(index, cursor.bg);
  113. cellSetFlags(index, cursor.flags);
  114. }
  115. // int cellGetHash(int index) {
  116. // final cell = index * _cellSize;
  117. // final a = _cells.getInt64(cell);
  118. // final b = _cells.getInt64(cell + 8);
  119. // return a ^ b;
  120. // }
  121. void removeRange(int start, int end) {
  122. // start = start.clamp(0, _cells.length);
  123. // end ??= _cells.length;
  124. // end = end.clamp(start, _cells.length);
  125. // _cells.removeRange(start, end);
  126. for (var index = start; index < end; index++) {
  127. cellSetContent(index, 0x00);
  128. }
  129. }
  130. }