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