line.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import 'dart:math' show min;
  2. import 'dart:typed_data';
  3. import 'package:xterm/core/cell.dart';
  4. import 'package:xterm/core/cursor.dart';
  5. import 'package:xterm/utils/unicode_v11.dart';
  6. const _cellSize = 4;
  7. const _cellForeground = 0;
  8. const _cellBackground = 1;
  9. const _cellAttributes = 2;
  10. const _cellContent = 3;
  11. class BufferLine {
  12. BufferLine(
  13. this._length, {
  14. this.isWrapped = false,
  15. }) : _data = Uint32List(_calcCapacity(_length) * _cellSize);
  16. int _length;
  17. Uint32List _data;
  18. Uint32List get data => _data;
  19. var isWrapped = false;
  20. int get length => _length;
  21. int getForeground(int index) {
  22. return _data[index * _cellSize + _cellForeground];
  23. }
  24. int getBackground(int index) {
  25. return _data[index * _cellSize + _cellBackground];
  26. }
  27. int getAttributes(int index) {
  28. return _data[index * _cellSize + _cellAttributes];
  29. }
  30. int getContent(int index) {
  31. return _data[index * _cellSize + _cellContent];
  32. }
  33. int getCodePoint(int index) {
  34. return _data[index * _cellSize + _cellContent] & CellContent.codepointMask;
  35. }
  36. int getWidth(int index) {
  37. return _data[index * _cellSize + _cellContent] >> CellContent.widthShift;
  38. }
  39. void getCellData(int index, CellData cellData) {
  40. final offset = index * _cellSize;
  41. cellData.foreground = _data[offset + _cellForeground];
  42. cellData.background = _data[offset + _cellBackground];
  43. cellData.flags = _data[offset + _cellAttributes];
  44. cellData.content = _data[offset + _cellContent];
  45. }
  46. CellData createCellData(int index) {
  47. final cellData = CellData.empty();
  48. final offset = index * _cellSize;
  49. _data[offset + _cellForeground] = cellData.foreground;
  50. _data[offset + _cellBackground] = cellData.background;
  51. _data[offset + _cellAttributes] = cellData.flags;
  52. _data[offset + _cellContent] = cellData.content;
  53. return cellData;
  54. }
  55. void setForeground(int index, int value) {
  56. _data[index * _cellSize + _cellForeground] = value;
  57. }
  58. void setBackground(int index, int value) {
  59. _data[index * _cellSize + _cellBackground] = value;
  60. }
  61. void setAttributes(int index, int value) {
  62. _data[index * _cellSize + _cellAttributes] = value;
  63. }
  64. void setContent(int index, int value) {
  65. _data[index * _cellSize + _cellContent] = value;
  66. }
  67. void setCodePoint(int index, int char) {
  68. final width = unicodeV11.wcwidth(char);
  69. setContent(index, char | (width << CellContent.widthShift));
  70. }
  71. void setCell(int index, int char, int witdh, CursorStyle style) {
  72. final offset = index * _cellSize;
  73. _data[offset + _cellForeground] = style.foreground;
  74. _data[offset + _cellBackground] = style.background;
  75. _data[offset + _cellAttributes] = style.attrs;
  76. _data[offset + _cellContent] = char | (witdh << CellContent.widthShift);
  77. }
  78. void setCellData(int index, CellData cellData) {
  79. final offset = index * _cellSize;
  80. _data[offset + _cellForeground] = cellData.foreground;
  81. _data[offset + _cellBackground] = cellData.background;
  82. _data[offset + _cellAttributes] = cellData.flags;
  83. _data[offset + _cellContent] = cellData.content;
  84. }
  85. void eraseCell(int index, CursorStyle style) {
  86. final offset = index * _cellSize;
  87. _data[offset + _cellForeground] = style.foreground;
  88. _data[offset + _cellBackground] = style.background;
  89. _data[offset + _cellAttributes] = style.attrs;
  90. _data[offset + _cellContent] = 0;
  91. }
  92. void resetCell(int index) {
  93. final offset = index * _cellSize;
  94. _data[offset + _cellForeground] = 0;
  95. _data[offset + _cellBackground] = 0;
  96. _data[offset + _cellAttributes] = 0;
  97. _data[offset + _cellContent] = 0;
  98. }
  99. void eraseRange(int start, int end, CursorStyle style) {
  100. // reset cell one to the left if start is second cell of a wide char
  101. if (start > 0 && getWidth(start - 1) == 2) {
  102. eraseCell(start - 1, style);
  103. }
  104. // reset cell one to the right if end is second cell of a wide char
  105. if (end < _length && getWidth(end - 1) == 2) {
  106. eraseCell(end - 1, style);
  107. }
  108. end = min(end, _length);
  109. for (var i = start; i < end; i++) {
  110. eraseCell(i, style);
  111. }
  112. }
  113. void removeCells(int start, int count, [CursorStyle? style]) {
  114. assert(start >= 0 && start < _length);
  115. assert(count >= 0 && start + count <= _length);
  116. style ??= CursorStyle.empty;
  117. if (start + count < _length) {
  118. final moveStart = start * _cellSize;
  119. final moveEnd = (_length - count) * _cellSize;
  120. final moveOffset = count * _cellSize;
  121. for (var i = moveStart; i < moveEnd; i++) {
  122. _data[i] = _data[i + moveOffset];
  123. }
  124. }
  125. for (var i = _length - count; i < _length; i++) {
  126. eraseCell(i, style);
  127. }
  128. if (start > 0 && getWidth(start - 1) == 2) {
  129. eraseCell(start - 1, style);
  130. }
  131. }
  132. void insertCells(int start, int count, [CursorStyle? style]) {
  133. style ??= CursorStyle.empty;
  134. if (start > 0 && getWidth(start - 1) == 2) {
  135. eraseCell(start - 1, style);
  136. }
  137. if (start + count < _length) {
  138. final moveStart = start * _cellSize;
  139. final moveEnd = (_length - count) * _cellSize;
  140. final moveOffset = count * _cellSize;
  141. for (var i = moveEnd - 1; i >= moveStart; i--) {
  142. _data[i + moveOffset] = _data[i];
  143. }
  144. }
  145. final end = min(start + count, _length);
  146. for (var i = start; i < end; i++) {
  147. eraseCell(i, style);
  148. }
  149. if (getWidth(_length - 1) == 2) {
  150. eraseCell(_length - 1, style);
  151. }
  152. }
  153. void resize(int length) {
  154. assert(length >= 0);
  155. if (length == _length) {
  156. return;
  157. }
  158. final newBufferSize = _calcCapacity(length) * _cellSize;
  159. if (newBufferSize > _data.length) {
  160. final newBuffer = Uint32List(newBufferSize);
  161. newBuffer.setRange(0, _data.length, _data);
  162. _data = newBuffer;
  163. }
  164. _length = length;
  165. }
  166. int getTrimmedLength([int? cols]) {
  167. if (cols == null) {
  168. cols = _data.length ~/ _cellSize;
  169. }
  170. for (var i = cols - 1; i >= 0; i--) {
  171. var codePoint = getCodePoint(i);
  172. if (codePoint != 0) {
  173. // we are at the last cell in this line that has content.
  174. // the length of this line is the index of this cell + 1
  175. // the only exception is that if that last cell is wider
  176. // than 1 then we have to add the diff
  177. final lastCellWidth = getWidth(i);
  178. return i + lastCellWidth;
  179. }
  180. }
  181. return 0;
  182. }
  183. void copyFrom(BufferLine src, int srcCol, int dstCol, int len) {
  184. resize(dstCol + len);
  185. // data.setRange(
  186. // dstCol * _cellSize,
  187. // (dstCol + len) * _cellSize,
  188. // Uint32List.sublistView(src.data, srcCol * _cellSize, len * _cellSize),
  189. // );
  190. var srcOffset = srcCol * _cellSize;
  191. var dstOffset = dstCol * _cellSize;
  192. for (var i = 0; i < len * _cellSize; i++) {
  193. _data[dstOffset++] = src._data[srcOffset++];
  194. }
  195. }
  196. static int _calcCapacity(int length) {
  197. assert(length >= 0);
  198. var capacity = 64;
  199. if (length < 256) {
  200. while (capacity < length) {
  201. capacity *= 2;
  202. }
  203. } else {
  204. capacity = 256;
  205. while (capacity < length) {
  206. capacity += 32;
  207. }
  208. }
  209. return capacity;
  210. }
  211. String getText([int? from, int? to]) {
  212. if (from == null) {
  213. from = 0;
  214. }
  215. if (to == null) {
  216. to = _length;
  217. }
  218. final builder = StringBuffer();
  219. for (var i = from; i < to; i++) {
  220. final codePoint = getCodePoint(i);
  221. final width = getWidth(i);
  222. if (codePoint != 0 && i + width <= to) {
  223. builder.writeCharCode(codePoint);
  224. }
  225. }
  226. return builder.toString();
  227. }
  228. @override
  229. String toString() {
  230. return getText();
  231. }
  232. }