buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. final rawIndex = convertViewLineToRawLine(index);
  62. if(rawIndex >= lines.length) {
  63. lines.addAll(List<BufferLine>.generate(rawIndex - lines.length + 1, (index) => BufferLine()));
  64. }
  65. return lines[rawIndex];
  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.newLineMode) {
  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. if (height < terminal.viewHeight) {
  105. return lines.toList();
  106. }
  107. final result = <BufferLine>[];
  108. for (var i = height - terminal.viewHeight; i < height; i++) {
  109. final y = i - scrollOffsetFromBottom;
  110. if (y >= 0 && y < height) {
  111. result.add(lines[y]);
  112. }
  113. }
  114. return result;
  115. }
  116. void eraseDisplayFromCursor() {
  117. eraseLineFromCursor();
  118. for (var i = _cursorY + 1; i < terminal.viewHeight; i++) {
  119. getViewLine(i).erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  120. }
  121. }
  122. void eraseDisplayToCursor() {
  123. eraseLineToCursor();
  124. for (var i = 0; i < _cursorY; i++) {
  125. getViewLine(i).erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  126. }
  127. }
  128. void eraseDisplay() {
  129. for (var i = 0; i < terminal.viewHeight; i++) {
  130. final line = getViewLine(i);
  131. line.erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  132. }
  133. }
  134. void eraseLineFromCursor() {
  135. currentLine.erase(terminal.cellAttr.value, _cursorX, terminal.viewWidth, _cursorX == 0);
  136. }
  137. void eraseLineToCursor() {
  138. currentLine.erase(terminal.cellAttr.value, 0, _cursorX, _cursorX == 0);
  139. }
  140. void eraseLine() {
  141. currentLine.erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  142. }
  143. void eraseCharacters(int count) {
  144. final start = _cursorX;
  145. for (var i = start; i < start + count; i++) {
  146. if (i >= currentLine.length) {
  147. currentLine.add(Cell(attr: terminal.cellAttr.value));
  148. } else {
  149. currentLine.getCell(i).erase(terminal.cellAttr.value);
  150. }
  151. }
  152. }
  153. ScrollRange getAreaScrollRange() {
  154. var top = convertViewLineToRawLine(_marginTop);
  155. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  156. if (bottom > lines.length) {
  157. bottom = lines.length;
  158. }
  159. return ScrollRange(top, bottom);
  160. }
  161. void areaScrollDown(int lines) {
  162. final scrollRange = getAreaScrollRange();
  163. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  164. i--;
  165. if (i >= scrollRange.top + lines) {
  166. this.lines[i] = this.lines[i - lines];
  167. } else {
  168. this.lines[i] = BufferLine();
  169. }
  170. }
  171. }
  172. void areaScrollUp(int lines) {
  173. final scrollRange = getAreaScrollRange();
  174. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  175. if (i + lines < scrollRange.bottom) {
  176. this.lines[i] = this.lines[i + lines];
  177. } else {
  178. this.lines[i] = BufferLine();
  179. }
  180. }
  181. }
  182. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  183. ///
  184. /// ESC D
  185. ///
  186. /// [index] causes the active position to move downward one line without
  187. /// changing the column position. If the active position is at the bottom
  188. /// margin, a scroll up is performed.
  189. void index() {
  190. if (isInScrollableRegion) {
  191. if (_cursorY < _marginBottom) {
  192. moveCursorY(1);
  193. } else {
  194. areaScrollUp(1);
  195. }
  196. return;
  197. }
  198. // the cursor is not in the scrollable region
  199. if (_cursorY >= terminal.viewHeight - 1) {
  200. // we are ait the bottom so a new line is created.
  201. lines.add(BufferLine());
  202. // clean extra lines if needed.
  203. final maxLines = terminal.maxLines;
  204. if (maxLines != null && lines.length > maxLines) {
  205. lines.removeRange(0, lines.length - maxLines);
  206. }
  207. } else {
  208. // there're still lines so we simply move cursor down.
  209. moveCursorY(1);
  210. }
  211. }
  212. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  213. void reverseIndex() {
  214. if (_cursorY == _marginTop) {
  215. areaScrollDown(1);
  216. } else if (_cursorY > 0) {
  217. moveCursorY(-1);
  218. }
  219. }
  220. Cell? getCell(int col, int row) {
  221. final rawRow = convertViewLineToRawLine(row);
  222. return getRawCell(col, rawRow);
  223. }
  224. Cell? getRawCell(int col, int rawRow) {
  225. if (col < 0 || rawRow < 0 || rawRow >= lines.length) {
  226. return null;
  227. }
  228. final line = lines[rawRow];
  229. if (col >= line.length) {
  230. return null;
  231. }
  232. return line.getCell(col);
  233. }
  234. Cell? getCellUnderCursor() {
  235. return getCell(cursorX, cursorY);
  236. }
  237. void cursorGoForward() {
  238. setCursorX(_cursorX + 1);
  239. }
  240. void setCursorX(int cursorX) {
  241. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  242. }
  243. void setCursorY(int cursorY) {
  244. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  245. }
  246. void moveCursorX(int offset) {
  247. setCursorX(_cursorX + offset);
  248. }
  249. void moveCursorY(int offset) {
  250. setCursorY(_cursorY + offset);
  251. }
  252. void setPosition(int cursorX, int cursorY) {
  253. var maxLine = terminal.viewHeight - 1;
  254. if (terminal.originMode) {
  255. cursorY += _marginTop;
  256. maxLine = _marginBottom;
  257. }
  258. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  259. _cursorY = cursorY.clamp(0, maxLine);
  260. }
  261. void movePosition(int offsetX, int offsetY) {
  262. final cursorX = _cursorX + offsetX;
  263. final cursorY = _cursorY + offsetY;
  264. setPosition(cursorX, cursorY);
  265. }
  266. int get scrollOffsetFromBottom {
  267. return _scrollLinesFromBottom;
  268. }
  269. int get scrollOffsetFromTop {
  270. return terminal.invisibleHeight - scrollOffsetFromBottom;
  271. }
  272. void setScrollOffsetFromBottom(int offset) {
  273. if (height < terminal.viewHeight) return;
  274. final maxOffset = height - terminal.viewHeight;
  275. _scrollLinesFromBottom = offset.clamp(0, maxOffset);
  276. }
  277. void setScrollOffsetFromTop(int offset) {
  278. final bottomOffset = terminal.invisibleHeight - offset;
  279. setScrollOffsetFromBottom(bottomOffset);
  280. }
  281. void screenScrollUp(int lines) {
  282. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  283. }
  284. void screenScrollDown(int lines) {
  285. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  286. }
  287. void saveCursor() {
  288. _savedCellAttr = terminal.cellAttr.value;
  289. _savedCursorX = _cursorX;
  290. _savedCursorY = _cursorY;
  291. charset.save();
  292. }
  293. void adjustSavedCursor(int diffX, int diffY) {
  294. if (_savedCursorX != null) {
  295. _savedCursorX = _savedCursorX! + diffX;
  296. }
  297. if (_savedCursorY != null) {
  298. _savedCursorY = _savedCursorY! + diffY;
  299. }
  300. }
  301. void restoreCursor() {
  302. if (_savedCellAttr != null) {
  303. terminal.cellAttr.use(_savedCellAttr!);
  304. }
  305. if (_savedCursorX != null) {
  306. _cursorX = _savedCursorX!;
  307. }
  308. if (_savedCursorY != null) {
  309. _cursorY = _savedCursorY!;
  310. }
  311. charset.restore();
  312. }
  313. void setVerticalMargins(int top, int bottom) {
  314. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  315. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  316. _marginTop = min(_marginTop, _marginBottom);
  317. _marginBottom = max(_marginTop, _marginBottom);
  318. }
  319. bool get hasScrollableRegion {
  320. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  321. }
  322. bool get isInScrollableRegion {
  323. return hasScrollableRegion &&
  324. _cursorY >= _marginTop &&
  325. _cursorY <= _marginBottom;
  326. }
  327. void resetVerticalMargins() {
  328. setVerticalMargins(0, terminal.viewHeight - 1);
  329. }
  330. void deleteChars(int count) {
  331. final start = _cursorX.clamp(0, currentLine.length);
  332. final end = min(_cursorX + count, currentLine.length);
  333. currentLine.removeRange(start, end);
  334. }
  335. void clearScrollback() {
  336. if (lines.length <= terminal.viewHeight) {
  337. return;
  338. }
  339. lines.removeRange(0, lines.length - terminal.viewHeight);
  340. }
  341. void clear() {
  342. lines.clear();
  343. }
  344. void insertBlankCharacters(int count) {
  345. for (var i = 0; i < count; i++) {
  346. final cell = Cell(attr: terminal.cellAttr.value);
  347. currentLine.insert(_cursorX + i, cell);
  348. }
  349. }
  350. void insertLines(int count) {
  351. if (hasScrollableRegion && !isInScrollableRegion) {
  352. return;
  353. }
  354. setCursorX(0);
  355. for (var i = 0; i < count; i++) {
  356. insertLine();
  357. }
  358. }
  359. void insertLine() {
  360. if (!isInScrollableRegion) {
  361. final index = convertViewLineToRawLine(_cursorX);
  362. final newLine = BufferLine();
  363. lines.insert(index, newLine);
  364. final maxLines = terminal.maxLines;
  365. if (maxLines != null && lines.length > maxLines) {
  366. lines.removeRange(0, lines.length - maxLines);
  367. }
  368. } else {
  369. final bottom = convertViewLineToRawLine(marginBottom);
  370. final movedLines = lines.getRange(_cursorY, bottom - 1);
  371. lines.setRange(_cursorY + 1, bottom, movedLines);
  372. final newLine = BufferLine();
  373. lines[_cursorY] = newLine;
  374. }
  375. }
  376. void deleteLines(int count) {
  377. if (hasScrollableRegion && !isInScrollableRegion) {
  378. return;
  379. }
  380. setCursorX(0);
  381. for (var i = 0; i < count; i++) {
  382. deleteLine();
  383. }
  384. }
  385. void deleteLine() {
  386. final index = convertViewLineToRawLine(_cursorX);
  387. if (index >= height) {
  388. return;
  389. }
  390. lines.removeAt(index);
  391. }
  392. void resize(int width, int height, int oldWidth, int oldHeight) {
  393. if (this.lines.length > 0) {
  394. if (oldHeight < height) {
  395. for (int y = oldHeight; y < height; y++) {
  396. //as long as we can we will adjust the scrolling
  397. if (scrollOffsetFromBottom > 0) {
  398. _scrollLinesFromBottom--;
  399. }
  400. }
  401. } else {
  402. // (this._rows >= newRows)
  403. //TODO: check if correct: I think we don't have anything to do here... things will correct automatically
  404. // for (int y = rows; y > newRows; y--) {
  405. // if (lines.Length > newRows + YBase) {
  406. // if (lines.Length > YBase + this.y + 1) {
  407. // // The line is a blank line below the cursor, remove it
  408. // lines.Pop();
  409. // } else {
  410. // // The line is the cursor, scroll down
  411. // YBase++;
  412. // YDisp++;
  413. // }
  414. // }
  415. // }
  416. }
  417. // // Reduce max length if needed after adjustments, this is done after as it
  418. // // would otherwise cut data from the bottom of the buffer.
  419. // if (newMaxLength < lines.MaxLength) {
  420. // // Trim from the top of the buffer and adjust ybase and ydisp.
  421. // int amountToTrim = lines.Length - newMaxLength;
  422. // if (amountToTrim > 0) {
  423. // lines.TrimStart(amountToTrim);
  424. // YBase = Math.Max(YBase - amountToTrim, 0);
  425. // YDisp = Math.Max(YDisp - amountToTrim, 0);
  426. // SavedY = Math.Max(SavedY - amountToTrim, 0);
  427. // }
  428. //
  429. // lines.MaxLength = newMaxLength;
  430. // }
  431. //
  432. // // Make sure that the cursor stays on screen
  433. // X = Math.Min(X, newCols - 1);
  434. // Y = Math.Min(Y, newRows - 1);
  435. // if (addToY != 0) {
  436. // Y += addToY;
  437. // }
  438. //
  439. // SavedX = Math.Min(SavedX, newCols - 1);
  440. // ScrollTop = 0;
  441. }
  442. // ScrollBottom = newRows - 1;
  443. if (/*IsReflowEnabled*/ true) {
  444. final rf = BufferReflow(this);
  445. rf.doReflow(oldWidth, width);
  446. }
  447. }
  448. }