buffer.dart 13 KB

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