buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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/utli/scroll_range.dart';
  8. import 'package:xterm/utli/unicode_v11.dart';
  9. import 'buffer_reflow.dart';
  10. class Buffer {
  11. Buffer(this.terminal) {
  12. resetVerticalMargins();
  13. lines = List.generate(terminal.viewHeight, (_) => BufferLine());
  14. }
  15. final Terminal terminal;
  16. final charset = Charset();
  17. /// lines of the buffer. the length of [lines] should always be equal or
  18. /// greater than [Terminal.viewHeight].
  19. late final List<BufferLine> lines;
  20. int? _savedCursorX;
  21. int? _savedCursorY;
  22. CellAttr? _savedCellAttr;
  23. // Indicates how far the bottom of the viewport is from the bottom of the
  24. // entire buffer. 0 if the viewport overlaps the terminal screen.
  25. int get scrollOffsetFromBottom => _scrollOffsetFromBottom;
  26. int _scrollOffsetFromBottom = 0;
  27. // Indicates how far the top of the viewport is from the top of the entire
  28. // buffer. 0 if the viewport is scrolled to the top.
  29. int get scrollOffsetFromTop {
  30. return terminal.invisibleHeight - scrollOffsetFromBottom;
  31. }
  32. /// Indicated whether the terminal should automatically scroll to bottom when
  33. /// new lines are added. When user is scrolling, [isUserScrolling] is true and
  34. /// the automatical scroll-to-bottom behavior is disabled.
  35. bool get isUserScrolling {
  36. return _scrollOffsetFromBottom != 0;
  37. }
  38. /// Horizontal position of the cursor relative to the top-left cornor of the
  39. /// screen, starting from 0.
  40. int get cursorX => _cursorX.clamp(0, terminal.viewWidth - 1);
  41. int _cursorX = 0;
  42. /// Vertical position of the cursor relative to the top-left cornor of the
  43. /// screen, starting from 0.
  44. int get cursorY => _cursorY;
  45. int _cursorY = 0;
  46. int get marginTop => _marginTop;
  47. late int _marginTop;
  48. int get marginBottom => _marginBottom;
  49. late int _marginBottom;
  50. /// Writes data to the terminal. Terminal sequences or special characters are
  51. /// not interpreted and directly added to the buffer.
  52. ///
  53. /// See also: [Terminal.write]
  54. void write(String text) {
  55. for (var char in text.runes) {
  56. writeChar(char);
  57. }
  58. }
  59. /// Writes a single character to the terminal. Special chatacters are not
  60. /// interpreted and directly added to the buffer.
  61. ///
  62. /// See also: [Terminal.writeChar]
  63. void writeChar(int codePoint) {
  64. codePoint = charset.translate(codePoint);
  65. final cellWidth = unicodeV11.wcwidth(codePoint);
  66. if (_cursorX >= terminal.viewWidth) {
  67. newLine();
  68. setCursorX(0);
  69. }
  70. final line = currentLine;
  71. while (line.length <= _cursorX) {
  72. line.add(Cell());
  73. }
  74. final cell = line.getCell(_cursorX);
  75. cell.setCodePoint(codePoint);
  76. cell.setWidth(cellWidth);
  77. cell.setAttr(terminal.cellAttr.value);
  78. if (_cursorX < terminal.viewWidth) {
  79. _cursorX++;
  80. }
  81. if (cellWidth == 2) {
  82. writeChar(0);
  83. }
  84. }
  85. BufferLine getViewLine(int index) {
  86. if (index > terminal.viewHeight) {
  87. return lines.last;
  88. }
  89. // while (index >= lines.length) {
  90. // final newLine = BufferLine();
  91. // lines.add(newLine);
  92. // }
  93. return lines[convertViewLineToRawLine(index)];
  94. }
  95. BufferLine get currentLine {
  96. return getViewLine(_cursorY);
  97. }
  98. int get height {
  99. return lines.length;
  100. }
  101. int convertViewLineToRawLine(int viewLine) {
  102. if (terminal.viewHeight > height) {
  103. return viewLine;
  104. }
  105. return viewLine + (height - terminal.viewHeight);
  106. }
  107. int convertRawLineToViewLine(int rawLine) {
  108. if (terminal.viewHeight > height) {
  109. return rawLine;
  110. }
  111. return rawLine - (height - terminal.viewHeight);
  112. }
  113. void newLine() {
  114. if (terminal.newLineMode) {
  115. setCursorX(0);
  116. }
  117. index();
  118. }
  119. void carriageReturn() {
  120. setCursorX(0);
  121. }
  122. void backspace() {
  123. if (_cursorX == 0 && currentLine.isWrapped) {
  124. movePosition(terminal.viewWidth - 1, -1);
  125. } else if (_cursorX == terminal.viewWidth) {
  126. movePosition(-2, 0);
  127. } else {
  128. movePosition(-1, 0);
  129. }
  130. }
  131. List<BufferLine> getVisibleLines() {
  132. if (height < terminal.viewHeight) {
  133. return lines.toList();
  134. }
  135. final result = <BufferLine>[];
  136. for (var i = height - terminal.viewHeight; i < height; i++) {
  137. final y = i - scrollOffsetFromBottom;
  138. if (y >= 0 && y < height) {
  139. result.add(lines[y]);
  140. }
  141. }
  142. return result;
  143. }
  144. void eraseDisplayFromCursor() {
  145. eraseLineFromCursor();
  146. for (var i = _cursorY + 1; i < terminal.viewHeight; i++) {
  147. getViewLine(i).erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  148. }
  149. }
  150. void eraseDisplayToCursor() {
  151. eraseLineToCursor();
  152. for (var i = 0; i < _cursorY; i++) {
  153. getViewLine(i).erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  154. }
  155. }
  156. void eraseDisplay() {
  157. for (var i = 0; i < terminal.viewHeight; i++) {
  158. final line = getViewLine(i);
  159. line.erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  160. }
  161. }
  162. void eraseLineFromCursor() {
  163. currentLine.erase(terminal.cellAttr.value, _cursorX, terminal.viewWidth, _cursorX == 0);
  164. }
  165. void eraseLineToCursor() {
  166. currentLine.erase(terminal.cellAttr.value, 0, _cursorX, _cursorX == 0);
  167. }
  168. void eraseLine() {
  169. currentLine.erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  170. }
  171. void eraseCharacters(int count) {
  172. final start = _cursorX;
  173. for (var i = start; i < start + count; i++) {
  174. if (i >= currentLine.length) {
  175. currentLine.add(Cell(attr: terminal.cellAttr.value));
  176. } else {
  177. currentLine.getCell(i).erase(terminal.cellAttr.value);
  178. }
  179. }
  180. }
  181. ScrollRange getAreaScrollRange() {
  182. var top = convertViewLineToRawLine(_marginTop);
  183. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  184. if (bottom > lines.length) {
  185. bottom = lines.length;
  186. }
  187. return ScrollRange(top, bottom);
  188. }
  189. void areaScrollDown(int lines) {
  190. final scrollRange = getAreaScrollRange();
  191. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  192. i--;
  193. if (i >= scrollRange.top + lines) {
  194. this.lines[i] = this.lines[i - lines];
  195. } else {
  196. this.lines[i] = BufferLine();
  197. }
  198. }
  199. }
  200. void areaScrollUp(int lines) {
  201. final scrollRange = getAreaScrollRange();
  202. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  203. if (i + lines < scrollRange.bottom) {
  204. this.lines[i] = this.lines[i + lines];
  205. } else {
  206. this.lines[i] = BufferLine();
  207. }
  208. }
  209. }
  210. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  211. ///
  212. /// ESC D
  213. ///
  214. /// [index] causes the active position to move downward one line without
  215. /// changing the column position. If the active position is at the bottom
  216. /// margin, a scroll up is performed.
  217. void index() {
  218. if (isInScrollableRegion) {
  219. if (_cursorY < _marginBottom) {
  220. moveCursorY(1);
  221. } else {
  222. areaScrollUp(1);
  223. }
  224. return;
  225. }
  226. // the cursor is not in the scrollable region
  227. if (_cursorY >= terminal.viewHeight - 1) {
  228. // we are at the bottom so a new line is created.
  229. lines.add(BufferLine());
  230. // keep viewport from moving if user is scrolling.
  231. if (isUserScrolling) {
  232. _scrollOffsetFromBottom++;
  233. }
  234. // clean extra lines if needed.
  235. final maxLines = terminal.maxLines;
  236. if (maxLines != null && lines.length > maxLines) {
  237. lines.removeRange(0, lines.length - maxLines);
  238. }
  239. } else {
  240. // there're still lines so we simply move cursor down.
  241. moveCursorY(1);
  242. }
  243. }
  244. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  245. void reverseIndex() {
  246. if (_cursorY == _marginTop) {
  247. areaScrollDown(1);
  248. } else if (_cursorY > 0) {
  249. moveCursorY(-1);
  250. }
  251. }
  252. Cell? getCell(int col, int row) {
  253. final rawRow = convertViewLineToRawLine(row);
  254. return getRawCell(col, rawRow);
  255. }
  256. Cell? getRawCell(int col, int rawRow) {
  257. if (col < 0 || rawRow < 0 || rawRow >= lines.length) {
  258. return null;
  259. }
  260. final line = lines[rawRow];
  261. if (col >= line.length) {
  262. return null;
  263. }
  264. return line.getCell(col);
  265. }
  266. Cell? getCellUnderCursor() {
  267. return getCell(cursorX, cursorY);
  268. }
  269. void cursorGoForward() {
  270. setCursorX(_cursorX + 1);
  271. }
  272. void setCursorX(int cursorX) {
  273. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  274. }
  275. void setCursorY(int cursorY) {
  276. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  277. }
  278. void moveCursorX(int offset) {
  279. setCursorX(_cursorX + offset);
  280. }
  281. void moveCursorY(int offset) {
  282. setCursorY(_cursorY + offset);
  283. }
  284. void setPosition(int cursorX, int cursorY) {
  285. var maxLine = terminal.viewHeight - 1;
  286. if (terminal.originMode) {
  287. cursorY += _marginTop;
  288. maxLine = _marginBottom;
  289. }
  290. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  291. _cursorY = cursorY.clamp(0, maxLine);
  292. }
  293. void movePosition(int offsetX, int offsetY) {
  294. final cursorX = _cursorX + offsetX;
  295. final cursorY = _cursorY + offsetY;
  296. setPosition(cursorX, cursorY);
  297. }
  298. void setScrollOffsetFromBottom(int offsetFromBottom) {
  299. if (height < terminal.viewHeight) return;
  300. final maxOffsetFromBottom = height - terminal.viewHeight;
  301. _scrollOffsetFromBottom = offsetFromBottom.clamp(0, maxOffsetFromBottom);
  302. }
  303. void setScrollOffsetFromTop(int offsetFromTop) {
  304. final bottomOffset = terminal.invisibleHeight - offsetFromTop;
  305. setScrollOffsetFromBottom(bottomOffset);
  306. }
  307. void screenScrollUp(int lines) {
  308. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  309. }
  310. void screenScrollDown(int lines) {
  311. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  312. }
  313. void saveCursor() {
  314. _savedCellAttr = terminal.cellAttr.value;
  315. _savedCursorX = _cursorX;
  316. _savedCursorY = _cursorY;
  317. charset.save();
  318. }
  319. void adjustSavedCursor(int diffX, int diffY) {
  320. if (_savedCursorX != null) {
  321. _savedCursorX = _savedCursorX! + diffX;
  322. }
  323. if (_savedCursorY != null) {
  324. _savedCursorY = _savedCursorY! + diffY;
  325. }
  326. }
  327. void restoreCursor() {
  328. if (_savedCellAttr != null) {
  329. terminal.cellAttr.use(_savedCellAttr!);
  330. }
  331. if (_savedCursorX != null) {
  332. _cursorX = _savedCursorX!;
  333. }
  334. if (_savedCursorY != null) {
  335. _cursorY = _savedCursorY!;
  336. }
  337. charset.restore();
  338. }
  339. void setVerticalMargins(int top, int bottom) {
  340. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  341. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  342. _marginTop = min(_marginTop, _marginBottom);
  343. _marginBottom = max(_marginTop, _marginBottom);
  344. }
  345. bool get hasScrollableRegion {
  346. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  347. }
  348. bool get isInScrollableRegion {
  349. return hasScrollableRegion &&
  350. _cursorY >= _marginTop &&
  351. _cursorY <= _marginBottom;
  352. }
  353. void resetVerticalMargins() {
  354. setVerticalMargins(0, terminal.viewHeight - 1);
  355. }
  356. void deleteChars(int count) {
  357. final start = _cursorX.clamp(0, currentLine.length);
  358. final end = min(_cursorX + count, currentLine.length);
  359. currentLine.removeRange(start, end);
  360. }
  361. void clearScrollback() {
  362. if (lines.length <= terminal.viewHeight) {
  363. return;
  364. }
  365. lines.removeRange(0, lines.length - terminal.viewHeight);
  366. }
  367. void clear() {
  368. lines.clear();
  369. }
  370. void insertBlankCharacters(int count) {
  371. for (var i = 0; i < count; i++) {
  372. final cell = Cell(attr: terminal.cellAttr.value);
  373. currentLine.insert(_cursorX + i, cell);
  374. }
  375. }
  376. void insertLines(int count) {
  377. if (hasScrollableRegion && !isInScrollableRegion) {
  378. return;
  379. }
  380. setCursorX(0);
  381. for (var i = 0; i < count; i++) {
  382. insertLine();
  383. }
  384. }
  385. void insertLine() {
  386. if (!isInScrollableRegion) {
  387. final index = convertViewLineToRawLine(_cursorX);
  388. final newLine = BufferLine();
  389. lines.insert(index, newLine);
  390. final maxLines = terminal.maxLines;
  391. if (maxLines != null && lines.length > maxLines) {
  392. lines.removeRange(0, lines.length - maxLines);
  393. }
  394. } else {
  395. final bottom = convertViewLineToRawLine(marginBottom);
  396. final movedLines = lines.getRange(_cursorY, bottom - 1);
  397. lines.setRange(_cursorY + 1, bottom, movedLines);
  398. final newLine = BufferLine();
  399. lines[_cursorY] = newLine;
  400. }
  401. }
  402. void deleteLines(int count) {
  403. if (hasScrollableRegion && !isInScrollableRegion) {
  404. return;
  405. }
  406. setCursorX(0);
  407. for (var i = 0; i < count; i++) {
  408. deleteLine();
  409. }
  410. }
  411. void deleteLine() {
  412. final index = convertViewLineToRawLine(_cursorX);
  413. if (index >= height) {
  414. return;
  415. }
  416. lines.removeAt(index);
  417. }
  418. void resize(int width, int height, int oldWidth, int oldHeight, bool doReflow) {
  419. if (this.lines.length > 0) {
  420. if (oldHeight < height) {
  421. for (int y = oldHeight; y < height; y++) {
  422. if (_cursorY < terminal.viewHeight - 1) {
  423. lines.add(BufferLine());
  424. } else {
  425. _cursorY++;
  426. }
  427. }
  428. } else {
  429. for (var i = 0; i < oldHeight - height; i++) {
  430. if (_cursorY < terminal.viewHeight - 1) {
  431. lines.removeLast();
  432. } else {
  433. _cursorY++;
  434. }
  435. }
  436. }
  437. }
  438. // ScrollBottom = newRows - 1;
  439. if (doReflow) {
  440. final rf = BufferReflow(this);
  441. rf.doReflow(oldWidth, width);
  442. }
  443. }
  444. }