buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. terminal.refresh();
  227. }
  228. void setCursorX(int cursorX) {
  229. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  230. terminal.refresh();
  231. }
  232. void setCursorY(int cursorY) {
  233. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  234. terminal.refresh();
  235. }
  236. void moveCursorX(int offset) {
  237. setCursorX(_cursorX + offset);
  238. }
  239. void moveCursorY(int offset) {
  240. setCursorY(_cursorY + offset);
  241. }
  242. void setPosition(int cursorX, int cursorY) {
  243. var maxLine = terminal.viewHeight - 1;
  244. if (terminal.originMode) {
  245. cursorY += _marginTop;
  246. maxLine = _marginBottom;
  247. }
  248. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  249. _cursorY = cursorY.clamp(0, maxLine);
  250. }
  251. void movePosition(int offsetX, int offsetY) {
  252. final cursorX = _cursorX + offsetX;
  253. final cursorY = _cursorY + offsetY;
  254. setPosition(cursorX, cursorY);
  255. }
  256. int get scrollOffsetFromBottom {
  257. return _scrollLinesFromBottom;
  258. }
  259. int get scrollOffsetFromTop {
  260. return terminal.invisibleHeight - scrollOffsetFromBottom;
  261. }
  262. void setScrollOffsetFromBottom(int offset) {
  263. if (height < terminal.viewHeight) return;
  264. final maxOffset = height - terminal.viewHeight;
  265. _scrollLinesFromBottom = offset.clamp(0, maxOffset);
  266. terminal.refresh();
  267. }
  268. void setScrollOffsetFromTop(int offset) {
  269. final bottomOffset = terminal.invisibleHeight - offset;
  270. setScrollOffsetFromBottom(bottomOffset);
  271. }
  272. void screenScrollUp(int lines) {
  273. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  274. }
  275. void screenScrollDown(int lines) {
  276. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  277. }
  278. void saveCursor() {
  279. _savedCellAttr = terminal.cellAttr.value;
  280. _savedCursorX = _cursorX;
  281. _savedCursorY = _cursorY;
  282. charset.save();
  283. }
  284. void adjustSavedCursor(int diffX, int diffY) {
  285. if (_savedCursorX != null) {
  286. _savedCursorX = _savedCursorX! + diffX;
  287. }
  288. if (_savedCursorY != null) {
  289. _savedCursorY = _savedCursorY! + diffY;
  290. }
  291. }
  292. void restoreCursor() {
  293. if (_savedCellAttr != null) {
  294. terminal.cellAttr.use(_savedCellAttr!);
  295. }
  296. if (_savedCursorX != null) {
  297. _cursorX = _savedCursorX!;
  298. }
  299. if (_savedCursorY != null) {
  300. _cursorY = _savedCursorY!;
  301. }
  302. charset.restore();
  303. }
  304. void setVerticalMargins(int top, int bottom) {
  305. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  306. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  307. _marginTop = min(_marginTop, _marginBottom);
  308. _marginBottom = max(_marginTop, _marginBottom);
  309. }
  310. bool get hasScrollableRegion {
  311. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  312. }
  313. bool get isInScrollableRegion {
  314. return hasScrollableRegion &&
  315. _cursorY >= _marginTop &&
  316. _cursorY <= _marginBottom;
  317. }
  318. void resetVerticalMargins() {
  319. setVerticalMargins(0, terminal.viewHeight - 1);
  320. }
  321. void deleteChars(int count) {
  322. final start = _cursorX.clamp(0, currentLine.length);
  323. final end = min(_cursorX + count, currentLine.length);
  324. currentLine.removeRange(start, end);
  325. }
  326. void clearScrollback() {
  327. if (lines.length <= terminal.viewHeight) {
  328. return;
  329. }
  330. lines.removeRange(0, lines.length - terminal.viewHeight);
  331. }
  332. void clear() {
  333. lines.clear();
  334. }
  335. void insertBlankCharacters(int count) {
  336. for (var i = 0; i < count; i++) {
  337. final cell = Cell(attr: terminal.cellAttr.value);
  338. currentLine.insert(_cursorX + i, cell);
  339. }
  340. }
  341. void insertLines(int count) {
  342. if (hasScrollableRegion && !isInScrollableRegion) {
  343. return;
  344. }
  345. setCursorX(0);
  346. for (var i = 0; i < count; i++) {
  347. insertLine();
  348. }
  349. }
  350. void insertLine() {
  351. if (!isInScrollableRegion) {
  352. final index = convertViewLineToRawLine(_cursorX);
  353. final newLine = BufferLine();
  354. lines.insert(index, newLine);
  355. final maxLines = terminal.maxLines;
  356. if (maxLines != null && lines.length > maxLines) {
  357. lines.removeRange(0, lines.length - maxLines);
  358. }
  359. } else {
  360. final bottom = convertViewLineToRawLine(marginBottom);
  361. final movedLines = lines.getRange(_cursorY, bottom - 1);
  362. lines.setRange(_cursorY + 1, bottom, movedLines);
  363. final newLine = BufferLine();
  364. lines[_cursorY] = newLine;
  365. }
  366. }
  367. void deleteLines(int count) {
  368. if (hasScrollableRegion && !isInScrollableRegion) {
  369. return;
  370. }
  371. setCursorX(0);
  372. for (var i = 0; i < count; i++) {
  373. deleteLine();
  374. }
  375. }
  376. void deleteLine() {
  377. final index = convertViewLineToRawLine(_cursorX);
  378. if (index >= height) {
  379. return;
  380. }
  381. lines.removeAt(index);
  382. }
  383. void resize(int width, int height, int oldWidth, int oldHeight) {
  384. if (this.lines.length > 0) {
  385. // Deal with columns increasing (reducing needs to happen after reflow)
  386. if (oldWidth < width) {
  387. lines.forEach((l) {
  388. l.resize(width);
  389. });
  390. }
  391. if (oldHeight < height) {
  392. for (int y = oldHeight; y < height; y++) {
  393. //as long as we can we will adjust the scrolling
  394. if (scrollOffsetFromBottom > 0) {
  395. _scrollLinesFromBottom--;
  396. }
  397. }
  398. } else {
  399. // (this._rows >= newRows)
  400. //TODO: check if correct: I think we don't have anything to do here... things will correct automatically
  401. // for (int y = rows; y > newRows; y--) {
  402. // if (lines.Length > newRows + YBase) {
  403. // if (lines.Length > YBase + this.y + 1) {
  404. // // The line is a blank line below the cursor, remove it
  405. // lines.Pop();
  406. // } else {
  407. // // The line is the cursor, scroll down
  408. // YBase++;
  409. // YDisp++;
  410. // }
  411. // }
  412. // }
  413. }
  414. // // Reduce max length if needed after adjustments, this is done after as it
  415. // // would otherwise cut data from the bottom of the buffer.
  416. // if (newMaxLength < lines.MaxLength) {
  417. // // Trim from the top of the buffer and adjust ybase and ydisp.
  418. // int amountToTrim = lines.Length - newMaxLength;
  419. // if (amountToTrim > 0) {
  420. // lines.TrimStart(amountToTrim);
  421. // YBase = Math.Max(YBase - amountToTrim, 0);
  422. // YDisp = Math.Max(YDisp - amountToTrim, 0);
  423. // SavedY = Math.Max(SavedY - amountToTrim, 0);
  424. // }
  425. //
  426. // lines.MaxLength = newMaxLength;
  427. // }
  428. //
  429. // // Make sure that the cursor stays on screen
  430. // X = Math.Min(X, newCols - 1);
  431. // Y = Math.Min(Y, newRows - 1);
  432. // if (addToY != 0) {
  433. // Y += addToY;
  434. // }
  435. //
  436. // SavedX = Math.Min(SavedX, newCols - 1);
  437. // ScrollTop = 0;
  438. }
  439. // ScrollBottom = newRows - 1;
  440. if (/*IsReflowEnabled*/ true) {
  441. final rf = BufferReflow(
  442. this,
  443. CellAttr(
  444. fgColor: TerminalColor.empty(), bgColor: TerminalColor.empty()));
  445. rf.doReflow(oldWidth, width);
  446. // Trim the end of the line off if cols shrunk
  447. if (oldWidth > width) {
  448. lines.forEach((l) {
  449. l.resize(width, erase: true);
  450. });
  451. }
  452. }
  453. }
  454. }