buffer.dart 12 KB

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