buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import 'dart:math' show max, min;
  2. import 'package:xterm/buffer/buffer_line.dart';
  3. import 'package:xterm/buffer/cell.dart';
  4. import 'package:xterm/buffer/cell_attr.dart';
  5. import 'package:xterm/terminal/charset.dart';
  6. import 'package:xterm/terminal/terminal.dart';
  7. import 'package:xterm/theme/terminal_color.dart';
  8. import 'package:xterm/utli/scroll_range.dart';
  9. import 'package:xterm/utli/unicode_v11.dart';
  10. import 'buffer_reflow.dart';
  11. class Buffer {
  12. Buffer(this.terminal) {
  13. resetVerticalMargins();
  14. }
  15. final Terminal terminal;
  16. final lines = <BufferLine>[];
  17. final charset = Charset();
  18. int _cursorX = 0;
  19. int _cursorY = 0;
  20. int? _savedCursorX;
  21. int? _savedCursorY;
  22. int _scrollLinesFromBottom = 0;
  23. late int _marginTop;
  24. late int _marginBottom;
  25. CellAttr? _savedCellAttr;
  26. int get cursorX => _cursorX.clamp(0, terminal.viewWidth - 1);
  27. int get cursorY => _cursorY;
  28. int get marginTop => _marginTop;
  29. int get marginBottom => _marginBottom;
  30. void write(String text) {
  31. for (var char in text.runes) {
  32. writeChar(char);
  33. }
  34. }
  35. void writeChar(int codePoint) {
  36. codePoint = charset.translate(codePoint);
  37. final cellWidth = unicodeV11.wcwidth(codePoint);
  38. if (_cursorX >= terminal.viewWidth) {
  39. newLine();
  40. setCursorX(0);
  41. }
  42. final line = currentLine;
  43. while (line.length <= _cursorX) {
  44. line.add(Cell());
  45. }
  46. final cell = line.getCell(_cursorX);
  47. cell.setCodePoint(codePoint);
  48. cell.setWidth(cellWidth);
  49. cell.setAttr(terminal.cellAttr.value);
  50. if (_cursorX < terminal.viewWidth) {
  51. _cursorX++;
  52. }
  53. if (cellWidth == 2) {
  54. writeChar(0);
  55. }
  56. }
  57. BufferLine getViewLine(int index) {
  58. if (index > terminal.viewHeight) {
  59. return lines.last;
  60. }
  61. while (index >= lines.length) {
  62. final newLine = BufferLine();
  63. lines.add(newLine);
  64. }
  65. return lines[convertViewLineToRawLine(index)];
  66. }
  67. BufferLine get currentLine {
  68. return getViewLine(_cursorY);
  69. }
  70. int get height {
  71. return lines.length;
  72. }
  73. int convertViewLineToRawLine(int viewLine) {
  74. if (terminal.viewHeight > height) {
  75. return viewLine;
  76. }
  77. return viewLine + (height - terminal.viewHeight);
  78. }
  79. int convertRawLineToViewLine(int rawLine) {
  80. if (terminal.viewHeight > height) {
  81. return rawLine;
  82. }
  83. return rawLine - (height - terminal.viewHeight);
  84. }
  85. void newLine() {
  86. if (terminal.lineFeed == false) {
  87. setCursorX(0);
  88. }
  89. index();
  90. }
  91. void carriageReturn() {
  92. setCursorX(0);
  93. }
  94. void backspace() {
  95. if (_cursorX == 0 && currentLine.isWrapped) {
  96. movePosition(terminal.viewWidth - 1, -1);
  97. } else if (_cursorX == terminal.viewWidth) {
  98. movePosition(-2, 0);
  99. } else {
  100. movePosition(-1, 0);
  101. }
  102. }
  103. List<BufferLine> getVisibleLines() {
  104. final result = <BufferLine>[];
  105. for (var i = height - terminal.viewHeight; i < height; i++) {
  106. final y = i - scrollOffsetFromBottom;
  107. if (y >= 0 && y < height) {
  108. result.add(lines[y]);
  109. }
  110. }
  111. return result;
  112. }
  113. void eraseDisplayFromCursor() {
  114. eraseLineFromCursor();
  115. for (var i = _cursorY + 1; i < terminal.viewHeight; i++) {
  116. getViewLine(i).erase(terminal.cellAttr.value, 0, terminal.viewWidth);
  117. }
  118. }
  119. void eraseDisplayToCursor() {
  120. eraseLineToCursor();
  121. for (var i = 0; i < _cursorY; i++) {
  122. getViewLine(i).erase(terminal.cellAttr.value, 0, terminal.viewWidth);
  123. }
  124. }
  125. void eraseDisplay() {
  126. for (var i = 0; i < terminal.viewHeight; i++) {
  127. final line = getViewLine(i);
  128. line.erase(terminal.cellAttr.value, 0, terminal.viewWidth);
  129. }
  130. }
  131. void eraseLineFromCursor() {
  132. currentLine.erase(terminal.cellAttr.value, _cursorX, terminal.viewWidth);
  133. }
  134. void eraseLineToCursor() {
  135. currentLine.erase(terminal.cellAttr.value, 0, _cursorX);
  136. }
  137. void eraseLine() {
  138. currentLine.erase(terminal.cellAttr.value, 0, terminal.viewWidth);
  139. }
  140. void eraseCharacters(int count) {
  141. final start = _cursorX;
  142. for (var i = start; i < start + count; i++) {
  143. if (i >= currentLine.length) {
  144. currentLine.add(Cell(attr: terminal.cellAttr.value));
  145. } else {
  146. currentLine.getCell(i).erase(terminal.cellAttr.value);
  147. }
  148. }
  149. }
  150. ScrollRange getAreaScrollRange() {
  151. var top = convertViewLineToRawLine(_marginTop);
  152. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  153. if (bottom > lines.length) {
  154. bottom = lines.length;
  155. }
  156. return ScrollRange(top, bottom);
  157. }
  158. void areaScrollDown(int lines) {
  159. final scrollRange = getAreaScrollRange();
  160. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  161. i--;
  162. if (i >= scrollRange.top + lines) {
  163. this.lines[i] = this.lines[i - lines];
  164. } else {
  165. this.lines[i] = BufferLine();
  166. }
  167. }
  168. }
  169. void areaScrollUp(int lines) {
  170. final scrollRange = getAreaScrollRange();
  171. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  172. if (i + lines < scrollRange.bottom) {
  173. this.lines[i] = this.lines[i + lines];
  174. } else {
  175. this.lines[i] = BufferLine();
  176. }
  177. }
  178. }
  179. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND
  180. void index() {
  181. if (isInScrollableRegion) {
  182. if (_cursorY < _marginBottom) {
  183. moveCursorY(1);
  184. } else {
  185. areaScrollUp(1);
  186. }
  187. return;
  188. }
  189. if (_cursorY >= terminal.viewHeight - 1) {
  190. lines.add(BufferLine());
  191. final maxLines = terminal.maxLines;
  192. if (maxLines != null && lines.length > maxLines) {
  193. lines.removeRange(0, lines.length - maxLines);
  194. }
  195. } else {
  196. moveCursorY(1);
  197. }
  198. }
  199. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  200. void reverseIndex() {
  201. if (_cursorY == _marginTop) {
  202. areaScrollDown(1);
  203. } else if (_cursorY > 0) {
  204. moveCursorY(-1);
  205. }
  206. }
  207. Cell? getCell(int col, int row) {
  208. final rawRow = convertViewLineToRawLine(row);
  209. return getRawCell(col, rawRow);
  210. }
  211. Cell? getRawCell(int col, int rawRow) {
  212. if (col < 0 || rawRow < 0 || rawRow >= lines.length) {
  213. return null;
  214. }
  215. final line = lines[rawRow];
  216. if (col >= line.length) {
  217. return null;
  218. }
  219. return line.getCell(col);
  220. }
  221. Cell? getCellUnderCursor() {
  222. return getCell(cursorX, cursorY);
  223. }
  224. void cursorGoForward() {
  225. setCursorX(_cursorX + 1);
  226. }
  227. void setCursorX(int cursorX) {
  228. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  229. }
  230. void setCursorY(int cursorY) {
  231. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  232. }
  233. void moveCursorX(int offset) {
  234. setCursorX(_cursorX + offset);
  235. }
  236. void moveCursorY(int offset) {
  237. setCursorY(_cursorY + offset);
  238. }
  239. void setPosition(int cursorX, int cursorY) {
  240. var maxLine = terminal.viewHeight - 1;
  241. if (terminal.originMode) {
  242. cursorY += _marginTop;
  243. maxLine = _marginBottom;
  244. }
  245. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  246. _cursorY = cursorY.clamp(0, maxLine);
  247. }
  248. void movePosition(int offsetX, int offsetY) {
  249. final cursorX = _cursorX + offsetX;
  250. final cursorY = _cursorY + offsetY;
  251. setPosition(cursorX, cursorY);
  252. }
  253. int get scrollOffsetFromBottom {
  254. return _scrollLinesFromBottom;
  255. }
  256. int get scrollOffsetFromTop {
  257. return terminal.invisibleHeight - scrollOffsetFromBottom;
  258. }
  259. void setScrollOffsetFromBottom(int offset) {
  260. if (height < terminal.viewHeight) return;
  261. final maxOffset = height - terminal.viewHeight;
  262. _scrollLinesFromBottom = offset.clamp(0, maxOffset);
  263. }
  264. void setScrollOffsetFromTop(int offset) {
  265. final bottomOffset = terminal.invisibleHeight - offset;
  266. setScrollOffsetFromBottom(bottomOffset);
  267. }
  268. void screenScrollUp(int lines) {
  269. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  270. }
  271. void screenScrollDown(int lines) {
  272. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  273. }
  274. void saveCursor() {
  275. _savedCellAttr = terminal.cellAttr.value;
  276. _savedCursorX = _cursorX;
  277. _savedCursorY = _cursorY;
  278. charset.save();
  279. }
  280. void adjustSavedCursor(int diffX, int diffY) {
  281. if (_savedCursorX != null) {
  282. _savedCursorX = _savedCursorX! + diffX;
  283. }
  284. if (_savedCursorY != null) {
  285. _savedCursorY = _savedCursorY! + diffY;
  286. }
  287. }
  288. void restoreCursor() {
  289. if (_savedCellAttr != null) {
  290. terminal.cellAttr.use(_savedCellAttr!);
  291. }
  292. if (_savedCursorX != null) {
  293. _cursorX = _savedCursorX!;
  294. }
  295. if (_savedCursorY != null) {
  296. _cursorY = _savedCursorY!;
  297. }
  298. charset.restore();
  299. }
  300. void setVerticalMargins(int top, int bottom) {
  301. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  302. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  303. _marginTop = min(_marginTop, _marginBottom);
  304. _marginBottom = max(_marginTop, _marginBottom);
  305. }
  306. bool get hasScrollableRegion {
  307. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  308. }
  309. bool get isInScrollableRegion {
  310. return hasScrollableRegion &&
  311. _cursorY >= _marginTop &&
  312. _cursorY <= _marginBottom;
  313. }
  314. void resetVerticalMargins() {
  315. setVerticalMargins(0, terminal.viewHeight - 1);
  316. }
  317. void deleteChars(int count) {
  318. final start = _cursorX.clamp(0, currentLine.length);
  319. final end = min(_cursorX + count, currentLine.length);
  320. currentLine.removeRange(start, end);
  321. }
  322. void clearScrollback() {
  323. if (lines.length <= terminal.viewHeight) {
  324. return;
  325. }
  326. lines.removeRange(0, lines.length - terminal.viewHeight);
  327. }
  328. void clear() {
  329. lines.clear();
  330. }
  331. void insertBlankCharacters(int count) {
  332. for (var i = 0; i < count; i++) {
  333. final cell = Cell(attr: terminal.cellAttr.value);
  334. currentLine.insert(_cursorX + i, cell);
  335. }
  336. }
  337. void insertLines(int count) {
  338. if (hasScrollableRegion && !isInScrollableRegion) {
  339. return;
  340. }
  341. setCursorX(0);
  342. for (var i = 0; i < count; i++) {
  343. insertLine();
  344. }
  345. }
  346. void insertLine() {
  347. if (!isInScrollableRegion) {
  348. final index = convertViewLineToRawLine(_cursorX);
  349. final newLine = BufferLine();
  350. lines.insert(index, newLine);
  351. final maxLines = terminal.maxLines;
  352. if (maxLines != null && lines.length > maxLines) {
  353. lines.removeRange(0, lines.length - maxLines);
  354. }
  355. } else {
  356. final bottom = convertViewLineToRawLine(marginBottom);
  357. final movedLines = lines.getRange(_cursorY, bottom - 1);
  358. lines.setRange(_cursorY + 1, bottom, movedLines);
  359. final newLine = BufferLine();
  360. lines[_cursorY] = newLine;
  361. }
  362. }
  363. void deleteLines(int count) {
  364. if (hasScrollableRegion && !isInScrollableRegion) {
  365. return;
  366. }
  367. setCursorX(0);
  368. for (var i = 0; i < count; i++) {
  369. deleteLine();
  370. }
  371. }
  372. void deleteLine() {
  373. final index = convertViewLineToRawLine(_cursorX);
  374. if (index >= height) {
  375. return;
  376. }
  377. lines.removeAt(index);
  378. }
  379. void resize(int width, int height, int oldWidth, int oldHeight) {
  380. if (this.lines.length > 0) {
  381. if (oldHeight < height) {
  382. for (int y = oldHeight; y < height; y++) {
  383. //as long as we can we will adjust the scrolling
  384. if (scrollOffsetFromBottom > 0) {
  385. _scrollLinesFromBottom--;
  386. }
  387. }
  388. } else {
  389. // (this._rows >= newRows)
  390. //TODO: check if correct: I think we don't have anything to do here... things will correct automatically
  391. // for (int y = rows; y > newRows; y--) {
  392. // if (lines.Length > newRows + YBase) {
  393. // if (lines.Length > YBase + this.y + 1) {
  394. // // The line is a blank line below the cursor, remove it
  395. // lines.Pop();
  396. // } else {
  397. // // The line is the cursor, scroll down
  398. // YBase++;
  399. // YDisp++;
  400. // }
  401. // }
  402. // }
  403. }
  404. // // Reduce max length if needed after adjustments, this is done after as it
  405. // // would otherwise cut data from the bottom of the buffer.
  406. // if (newMaxLength < lines.MaxLength) {
  407. // // Trim from the top of the buffer and adjust ybase and ydisp.
  408. // int amountToTrim = lines.Length - newMaxLength;
  409. // if (amountToTrim > 0) {
  410. // lines.TrimStart(amountToTrim);
  411. // YBase = Math.Max(YBase - amountToTrim, 0);
  412. // YDisp = Math.Max(YDisp - amountToTrim, 0);
  413. // SavedY = Math.Max(SavedY - amountToTrim, 0);
  414. // }
  415. //
  416. // lines.MaxLength = newMaxLength;
  417. // }
  418. //
  419. // // Make sure that the cursor stays on screen
  420. // X = Math.Min(X, newCols - 1);
  421. // Y = Math.Min(Y, newRows - 1);
  422. // if (addToY != 0) {
  423. // Y += addToY;
  424. // }
  425. //
  426. // SavedX = Math.Min(SavedX, newCols - 1);
  427. // ScrollTop = 0;
  428. }
  429. // ScrollBottom = newRows - 1;
  430. if (/*IsReflowEnabled*/ true) {
  431. final rf = BufferReflow(this);
  432. rf.doReflow(oldWidth, width);
  433. }
  434. }
  435. }