line_list.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import 'dart:math';
  2. import 'package:xterm/buffer/line/line.dart';
  3. import 'package:xterm/terminal/cursor.dart';
  4. /// Line layout:
  5. /// | cell | cell | cell | cell | ...
  6. /// (4 ints per cell)
  7. ///
  8. /// Cell layout:
  9. /// | code point | fg color | bg color | attributes |
  10. /// 1 int 1 int 1 int 1 int
  11. ///
  12. /// Attributes layout:
  13. /// | width | flags | reserved | reserved |
  14. /// 1byte 1byte 1byte 1byte
  15. const _cellSize = 4;
  16. const _cellContent = 0;
  17. const _cellFgColor = 1;
  18. const _cellBgColor = 2;
  19. const _cellAttributes = 3;
  20. const _cellWidth = 0;
  21. const _cellFlags = 8;
  22. int _nextLength(int lengthRequirement) {
  23. var nextLength = 2;
  24. while (nextLength < lengthRequirement) {
  25. nextLength *= 2;
  26. }
  27. return nextLength;
  28. }
  29. /// [List] based [BufferLine], used in browser where ByteData is not avaliable.
  30. class ListBufferLine with BufferLine {
  31. ListBufferLine(int length, this.isWrapped) {
  32. _maxCols = _nextLength(length);
  33. _cells = List.filled(_maxCols * _cellSize, 0);
  34. }
  35. late List<int> _cells;
  36. bool isWrapped;
  37. int _maxCols = 64;
  38. void ensure(int length) {
  39. if (length <= _maxCols) {
  40. return;
  41. }
  42. final nextLength = _nextLength(length);
  43. final newCells = List.filled(nextLength * _cellSize, 0);
  44. newCells.setAll(0, _cells);
  45. _cells = newCells;
  46. _maxCols = nextLength;
  47. }
  48. void insert(int index) {
  49. insertN(index, 1);
  50. }
  51. void removeN(int index, int count) {
  52. final moveStart = index * _cellSize;
  53. final moveOffset = count * _cellSize;
  54. final moveEnd = (_maxCols - count) * _cellSize;
  55. final bufferEnd = _maxCols * _cellSize;
  56. // move data backward
  57. for (var i = moveStart; i < moveEnd; i++) {
  58. _cells[i] = _cells[i + moveOffset];
  59. }
  60. // set empty cells to 0
  61. for (var i = moveEnd; i < bufferEnd; i++) {
  62. _cells[i] = 0x00;
  63. }
  64. }
  65. void insertN(int index, int count) {
  66. final moveStart = index * _cellSize;
  67. final moveOffset = count * _cellSize;
  68. final bufferEnd = _maxCols * _cellSize;
  69. // move data forward
  70. for (var i = bufferEnd - moveOffset - 1; i >= moveStart; i--) {
  71. _cells[i + moveOffset] = _cells[i];
  72. }
  73. // set inserted cells to 0
  74. for (var i = moveStart; i < moveStart + moveOffset; i++) {
  75. _cells[i] = 0x00;
  76. }
  77. }
  78. void clear() {
  79. clearRange(0, _cells.length ~/ _cellSize);
  80. }
  81. void erase(Cursor cursor, int start, int end, [bool resetIsWrapped = false]) {
  82. ensure(end);
  83. for (var i = start; i < end; i++) {
  84. cellErase(i, cursor);
  85. }
  86. if (resetIsWrapped) {
  87. isWrapped = false;
  88. }
  89. }
  90. void cellClear(int index) {
  91. _cells.fillRange(index * _cellSize, index * _cellSize + _cellSize, 0);
  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[cell + _cellContent] = content;
  101. _cells[cell + _cellFgColor] = cursor.fg;
  102. _cells[cell + _cellBgColor] = cursor.bg;
  103. _cells[cell + _cellAttributes] =
  104. (width << _cellWidth) + (cursor.flags << _cellFlags);
  105. }
  106. bool cellHasContent(int index) {
  107. return cellGetContent(index) != 0;
  108. }
  109. int cellGetContent(int index) {
  110. if (index >= _maxCols) return 0;
  111. return _cells[index * _cellSize + _cellContent];
  112. }
  113. void cellSetContent(int index, int content) {
  114. _cells[index * _cellSize + _cellContent] = content;
  115. }
  116. int cellGetFgColor(int index) {
  117. if (index >= _maxCols) return 0;
  118. return _cells[index * _cellSize + _cellFgColor];
  119. }
  120. void cellSetFgColor(int index, int color) {
  121. _cells[index * _cellSize + _cellFgColor] = color;
  122. }
  123. int cellGetBgColor(int index) {
  124. if (index >= _maxCols) return 0;
  125. return _cells[index * _cellSize + _cellBgColor];
  126. }
  127. void cellSetBgColor(int index, int color) {
  128. _cells[index * _cellSize + _cellBgColor] = color;
  129. }
  130. int cellGetFlags(int index) {
  131. if (index >= _maxCols) return 0;
  132. final offset = index * _cellSize + _cellAttributes;
  133. return (_cells[offset] >> _cellFlags) & 0xFF;
  134. }
  135. void cellSetFlags(int index, int flags) {
  136. final offset = index * _cellSize + _cellAttributes;
  137. var result = _cells[offset];
  138. result |= 0xFF << _cellFlags;
  139. result &= flags << _cellFlags;
  140. _cells[offset] = result;
  141. }
  142. int cellGetWidth(int index) {
  143. if (index >= _maxCols) return 0;
  144. final offset = index * _cellSize + _cellAttributes;
  145. return (_cells[offset] >> _cellWidth) & 0xFF;
  146. }
  147. void cellSetWidth(int index, int width) {
  148. final offset = index * _cellSize + _cellAttributes;
  149. var result = _cells[offset];
  150. result |= 0xFF << _cellWidth;
  151. result &= width << _cellWidth;
  152. _cells[offset] = result;
  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 (var i = cols - 1; i >= 0; i--) {
  178. if (cellGetContent(i) != 0) {
  179. // we are at the last cell in this line that has content.
  180. // the length of this line is the index of this cell + 1
  181. // the only exception is that if that last cell is wider
  182. // than 1 then we have to add the diff
  183. final lastCellWidth = cellGetWidth(i);
  184. return i + lastCellWidth;
  185. }
  186. }
  187. return 0;
  188. }
  189. void copyCellsFrom(ListBufferLine src, int srcCol, int dstCol, int len) {
  190. ensure(dstCol + len);
  191. final intsToCopy = len * _cellSize;
  192. final srcStart = srcCol * _cellSize;
  193. final dstStart = dstCol * _cellSize;
  194. final cells = _cells;
  195. final srcCells = src._cells;
  196. for (var i = 0; i < intsToCopy; i++) {
  197. cells[dstStart + i] = srcCells[srcStart + i];
  198. }
  199. }
  200. void removeRange(int start, int end) {
  201. end = min(end, _maxCols);
  202. this.removeN(start, end - start);
  203. }
  204. void clearRange(int start, int end) {
  205. end = min(end, _maxCols);
  206. for (var index = start; index < end; index++) {
  207. cellClear(index);
  208. }
  209. }
  210. @override
  211. String toString() {
  212. final result = StringBuffer();
  213. for (int i = 0; i < _maxCols; i++) {
  214. final code = cellGetContent(i);
  215. if (code == 0) {
  216. continue;
  217. }
  218. result.writeCharCode(code);
  219. }
  220. return result.toString();
  221. }
  222. }