buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. final rawIndex = convertViewLineToRawLine(index);
  90. if (rawIndex >= lines.length) {
  91. return BufferLine();
  92. }
  93. return lines[rawIndex];
  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)
  148. .erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  149. }
  150. }
  151. void eraseDisplayToCursor() {
  152. eraseLineToCursor();
  153. for (var i = 0; i < _cursorY; i++) {
  154. getViewLine(i)
  155. .erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  156. }
  157. }
  158. void eraseDisplay() {
  159. for (var i = 0; i < terminal.viewHeight; i++) {
  160. final line = getViewLine(i);
  161. line.erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  162. }
  163. }
  164. void eraseLineFromCursor() {
  165. currentLine.erase(
  166. terminal.cellAttr.value, _cursorX, terminal.viewWidth, _cursorX == 0);
  167. }
  168. void eraseLineToCursor() {
  169. currentLine.erase(terminal.cellAttr.value, 0, _cursorX, _cursorX == 0);
  170. }
  171. void eraseLine() {
  172. currentLine.erase(terminal.cellAttr.value, 0, terminal.viewWidth, true);
  173. }
  174. void eraseCharacters(int count) {
  175. final start = _cursorX;
  176. for (var i = start; i < start + count; i++) {
  177. if (i >= currentLine.length) {
  178. currentLine.add(Cell(attr: terminal.cellAttr.value));
  179. } else {
  180. currentLine.getCell(i).erase(terminal.cellAttr.value);
  181. }
  182. }
  183. }
  184. ScrollRange getAreaScrollRange() {
  185. var top = convertViewLineToRawLine(_marginTop);
  186. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  187. if (bottom > lines.length) {
  188. bottom = lines.length;
  189. }
  190. return ScrollRange(top, bottom);
  191. }
  192. void areaScrollDown(int lines) {
  193. final scrollRange = getAreaScrollRange();
  194. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  195. i--;
  196. if (i >= scrollRange.top + lines) {
  197. this.lines[i] = this.lines[i - lines];
  198. } else {
  199. this.lines[i] = BufferLine();
  200. }
  201. }
  202. }
  203. void areaScrollUp(int lines) {
  204. final scrollRange = getAreaScrollRange();
  205. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  206. if (i + lines < scrollRange.bottom) {
  207. this.lines[i] = this.lines[i + lines];
  208. } else {
  209. this.lines[i] = BufferLine();
  210. }
  211. }
  212. }
  213. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  214. ///
  215. /// ESC D
  216. ///
  217. /// [index] causes the active position to move downward one line without
  218. /// changing the column position. If the active position is at the bottom
  219. /// margin, a scroll up is performed.
  220. void index() {
  221. if (isInScrollableRegion) {
  222. if (_cursorY < _marginBottom) {
  223. moveCursorY(1);
  224. } else {
  225. areaScrollUp(1);
  226. }
  227. return;
  228. }
  229. // the cursor is not in the scrollable region
  230. if (_cursorY >= terminal.viewHeight - 1) {
  231. // we are at the bottom so a new line is created.
  232. lines.add(BufferLine());
  233. // keep viewport from moving if user is scrolling.
  234. if (isUserScrolling) {
  235. _scrollOffsetFromBottom++;
  236. }
  237. // clean extra lines if needed.
  238. final maxLines = terminal.maxLines;
  239. if (maxLines != null && lines.length > maxLines) {
  240. lines.removeRange(0, lines.length - maxLines);
  241. }
  242. } else {
  243. // there're still lines so we simply move cursor down.
  244. moveCursorY(1);
  245. }
  246. }
  247. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  248. void reverseIndex() {
  249. if (_cursorY == _marginTop) {
  250. areaScrollDown(1);
  251. } else if (_cursorY > 0) {
  252. moveCursorY(-1);
  253. }
  254. }
  255. Cell? getCell(int col, int row) {
  256. final rawRow = convertViewLineToRawLine(row);
  257. return getRawCell(col, rawRow);
  258. }
  259. Cell? getRawCell(int col, int rawRow) {
  260. if (col < 0 || rawRow < 0 || rawRow >= lines.length) {
  261. return null;
  262. }
  263. final line = lines[rawRow];
  264. if (col >= line.length) {
  265. return null;
  266. }
  267. return line.getCell(col);
  268. }
  269. Cell? getCellUnderCursor() {
  270. return getCell(cursorX, cursorY);
  271. }
  272. void cursorGoForward() {
  273. setCursorX(_cursorX + 1);
  274. }
  275. void setCursorX(int cursorX) {
  276. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  277. }
  278. void setCursorY(int cursorY) {
  279. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  280. }
  281. void moveCursorX(int offset) {
  282. setCursorX(_cursorX + offset);
  283. }
  284. void moveCursorY(int offset) {
  285. setCursorY(_cursorY + offset);
  286. }
  287. void setPosition(int cursorX, int cursorY) {
  288. var maxLine = terminal.viewHeight - 1;
  289. if (terminal.originMode) {
  290. cursorY += _marginTop;
  291. maxLine = _marginBottom;
  292. }
  293. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  294. _cursorY = cursorY.clamp(0, maxLine);
  295. }
  296. void movePosition(int offsetX, int offsetY) {
  297. final cursorX = _cursorX + offsetX;
  298. final cursorY = _cursorY + offsetY;
  299. setPosition(cursorX, cursorY);
  300. }
  301. void setScrollOffsetFromBottom(int offsetFromBottom) {
  302. if (height < terminal.viewHeight) return;
  303. final maxOffsetFromBottom = height - terminal.viewHeight;
  304. _scrollOffsetFromBottom = offsetFromBottom.clamp(0, maxOffsetFromBottom);
  305. }
  306. void setScrollOffsetFromTop(int offsetFromTop) {
  307. final bottomOffset = terminal.invisibleHeight - offsetFromTop;
  308. setScrollOffsetFromBottom(bottomOffset);
  309. }
  310. void screenScrollUp(int lines) {
  311. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  312. }
  313. void screenScrollDown(int lines) {
  314. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  315. }
  316. void saveCursor() {
  317. _savedCellAttr = terminal.cellAttr.value;
  318. _savedCursorX = _cursorX;
  319. _savedCursorY = _cursorY;
  320. charset.save();
  321. }
  322. void adjustSavedCursor(int diffX, int diffY) {
  323. if (_savedCursorX != null) {
  324. _savedCursorX = _savedCursorX! + diffX;
  325. }
  326. if (_savedCursorY != null) {
  327. _savedCursorY = _savedCursorY! + diffY;
  328. }
  329. }
  330. void restoreCursor() {
  331. if (_savedCellAttr != null) {
  332. terminal.cellAttr.use(_savedCellAttr!);
  333. }
  334. if (_savedCursorX != null) {
  335. _cursorX = _savedCursorX!;
  336. }
  337. if (_savedCursorY != null) {
  338. _cursorY = _savedCursorY!;
  339. }
  340. charset.restore();
  341. }
  342. void setVerticalMargins(int top, int bottom) {
  343. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  344. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  345. _marginTop = min(_marginTop, _marginBottom);
  346. _marginBottom = max(_marginTop, _marginBottom);
  347. }
  348. bool get hasScrollableRegion {
  349. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  350. }
  351. bool get isInScrollableRegion {
  352. return hasScrollableRegion &&
  353. _cursorY >= _marginTop &&
  354. _cursorY <= _marginBottom;
  355. }
  356. void resetVerticalMargins() {
  357. setVerticalMargins(0, terminal.viewHeight - 1);
  358. }
  359. void deleteChars(int count) {
  360. final start = _cursorX.clamp(0, currentLine.length);
  361. final end = min(_cursorX + count, currentLine.length);
  362. currentLine.removeRange(start, end);
  363. }
  364. void clearScrollback() {
  365. if (lines.length <= terminal.viewHeight) {
  366. return;
  367. }
  368. lines.removeRange(0, lines.length - terminal.viewHeight);
  369. }
  370. void clear() {
  371. lines.clear();
  372. }
  373. void insertBlankCharacters(int count) {
  374. for (var i = 0; i < count; i++) {
  375. final cell = Cell(attr: terminal.cellAttr.value);
  376. currentLine.insert(_cursorX + i, cell);
  377. }
  378. }
  379. void insertLines(int count) {
  380. if (hasScrollableRegion && !isInScrollableRegion) {
  381. return;
  382. }
  383. setCursorX(0);
  384. for (var i = 0; i < count; i++) {
  385. insertLine();
  386. }
  387. }
  388. void insertLine() {
  389. if (!isInScrollableRegion) {
  390. final index = convertViewLineToRawLine(_cursorX);
  391. final newLine = BufferLine();
  392. lines.insert(index, newLine);
  393. final maxLines = terminal.maxLines;
  394. if (maxLines != null && lines.length > maxLines) {
  395. lines.removeRange(0, lines.length - maxLines);
  396. }
  397. } else {
  398. final bottom = convertViewLineToRawLine(marginBottom);
  399. final movedLines = lines.getRange(_cursorY, bottom - 1);
  400. lines.setRange(_cursorY + 1, bottom, movedLines);
  401. final newLine = BufferLine();
  402. lines[_cursorY] = newLine;
  403. }
  404. }
  405. void deleteLines(int count) {
  406. if (hasScrollableRegion && !isInScrollableRegion) {
  407. return;
  408. }
  409. setCursorX(0);
  410. for (var i = 0; i < count; i++) {
  411. deleteLine();
  412. }
  413. }
  414. void deleteLine() {
  415. final index = convertViewLineToRawLine(_cursorX);
  416. if (index >= height) {
  417. return;
  418. }
  419. lines.removeAt(index);
  420. }
  421. void resize(
  422. int width, int height, int oldWidth, int oldHeight, bool doReflow) {
  423. if (this.lines.length > 0) {
  424. if (oldHeight < height) {
  425. for (int y = oldHeight; y < height; y++) {
  426. if (_cursorY < terminal.viewHeight - 1) {
  427. lines.add(BufferLine());
  428. } else {
  429. _cursorY++;
  430. }
  431. }
  432. } else {
  433. for (var i = 0; i < oldHeight - height; i++) {
  434. if (_cursorY < terminal.viewHeight - 1) {
  435. lines.removeLast();
  436. } else {
  437. _cursorY++;
  438. }
  439. }
  440. }
  441. }
  442. // ScrollBottom = newRows - 1;
  443. if (doReflow) {
  444. final rf = BufferReflow(this);
  445. rf.doReflow(oldWidth, width);
  446. }
  447. }
  448. }