buffer.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 at the bottom so a new line is created.
  225. lines.add(BufferLine());
  226. // keep viewport from moving if user is scrolling.
  227. if (isUserScrolling) {
  228. _scrollOffsetFromBottom++;
  229. }
  230. // clean extra lines if needed.
  231. final maxLines = terminal.maxLines;
  232. if (maxLines != null && lines.length > maxLines) {
  233. lines.removeRange(0, lines.length - maxLines);
  234. }
  235. } else {
  236. // there're still lines so we simply move cursor down.
  237. moveCursorY(1);
  238. }
  239. }
  240. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  241. void reverseIndex() {
  242. if (_cursorY == _marginTop) {
  243. areaScrollDown(1);
  244. } else if (_cursorY > 0) {
  245. moveCursorY(-1);
  246. }
  247. }
  248. Cell? getCell(int col, int row) {
  249. final rawRow = convertViewLineToRawLine(row);
  250. return getRawCell(col, rawRow);
  251. }
  252. Cell? getRawCell(int col, int rawRow) {
  253. if (col < 0 || rawRow < 0 || rawRow >= lines.length) {
  254. return null;
  255. }
  256. final line = lines[rawRow];
  257. if (col >= line.length) {
  258. return null;
  259. }
  260. return line.getCell(col);
  261. }
  262. Cell? getCellUnderCursor() {
  263. return getCell(cursorX, cursorY);
  264. }
  265. void cursorGoForward() {
  266. setCursorX(_cursorX + 1);
  267. }
  268. void setCursorX(int cursorX) {
  269. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  270. }
  271. void setCursorY(int cursorY) {
  272. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  273. }
  274. void moveCursorX(int offset) {
  275. setCursorX(_cursorX + offset);
  276. }
  277. void moveCursorY(int offset) {
  278. setCursorY(_cursorY + offset);
  279. }
  280. void setPosition(int cursorX, int cursorY) {
  281. var maxLine = terminal.viewHeight - 1;
  282. if (terminal.originMode) {
  283. cursorY += _marginTop;
  284. maxLine = _marginBottom;
  285. }
  286. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  287. _cursorY = cursorY.clamp(0, maxLine);
  288. }
  289. void movePosition(int offsetX, int offsetY) {
  290. final cursorX = _cursorX + offsetX;
  291. final cursorY = _cursorY + offsetY;
  292. setPosition(cursorX, cursorY);
  293. }
  294. void setScrollOffsetFromBottom(int offsetFromBottom) {
  295. if (height < terminal.viewHeight) return;
  296. final maxOffsetFromBottom = height - terminal.viewHeight;
  297. _scrollOffsetFromBottom = offsetFromBottom.clamp(0, maxOffsetFromBottom);
  298. }
  299. void setScrollOffsetFromTop(int offsetFromTop) {
  300. final bottomOffset = terminal.invisibleHeight - offsetFromTop;
  301. setScrollOffsetFromBottom(bottomOffset);
  302. }
  303. void screenScrollUp(int lines) {
  304. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  305. }
  306. void screenScrollDown(int lines) {
  307. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  308. }
  309. void saveCursor() {
  310. _savedCellAttr = terminal.cellAttr.value;
  311. _savedCursorX = _cursorX;
  312. _savedCursorY = _cursorY;
  313. charset.save();
  314. }
  315. void restoreCursor() {
  316. if (_savedCellAttr != null) {
  317. terminal.cellAttr.use(_savedCellAttr!);
  318. }
  319. if (_savedCursorX != null) {
  320. _cursorX = _savedCursorX!;
  321. }
  322. if (_savedCursorY != null) {
  323. _cursorY = _savedCursorY!;
  324. }
  325. charset.restore();
  326. }
  327. void setVerticalMargins(int top, int bottom) {
  328. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  329. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  330. _marginTop = min(_marginTop, _marginBottom);
  331. _marginBottom = max(_marginTop, _marginBottom);
  332. }
  333. bool get hasScrollableRegion {
  334. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  335. }
  336. bool get isInScrollableRegion {
  337. return hasScrollableRegion &&
  338. _cursorY >= _marginTop &&
  339. _cursorY <= _marginBottom;
  340. }
  341. void resetVerticalMargins() {
  342. setVerticalMargins(0, terminal.viewHeight - 1);
  343. }
  344. void deleteChars(int count) {
  345. final start = _cursorX.clamp(0, currentLine.length);
  346. final end = min(_cursorX + count, currentLine.length);
  347. currentLine.removeRange(start, end);
  348. }
  349. void clearScrollback() {
  350. if (lines.length <= terminal.viewHeight) {
  351. return;
  352. }
  353. lines.removeRange(0, lines.length - terminal.viewHeight);
  354. }
  355. void clear() {
  356. lines.clear();
  357. }
  358. void insertBlankCharacters(int count) {
  359. for (var i = 0; i < count; i++) {
  360. final cell = Cell(attr: terminal.cellAttr.value);
  361. currentLine.insert(_cursorX + i, cell);
  362. }
  363. }
  364. void insertLines(int count) {
  365. if (hasScrollableRegion && !isInScrollableRegion) {
  366. return;
  367. }
  368. setCursorX(0);
  369. for (var i = 0; i < count; i++) {
  370. insertLine();
  371. }
  372. }
  373. void insertLine() {
  374. if (!isInScrollableRegion) {
  375. final index = convertViewLineToRawLine(_cursorX);
  376. final newLine = BufferLine();
  377. lines.insert(index, newLine);
  378. final maxLines = terminal.maxLines;
  379. if (maxLines != null && lines.length > maxLines) {
  380. lines.removeRange(0, lines.length - maxLines);
  381. }
  382. } else {
  383. final bottom = convertViewLineToRawLine(marginBottom);
  384. final movedLines = lines.getRange(_cursorY, bottom - 1);
  385. lines.setRange(_cursorY + 1, bottom, movedLines);
  386. final newLine = BufferLine();
  387. lines[_cursorY] = newLine;
  388. }
  389. }
  390. void deleteLines(int count) {
  391. if (hasScrollableRegion && !isInScrollableRegion) {
  392. return;
  393. }
  394. setCursorX(0);
  395. for (var i = 0; i < count; i++) {
  396. deleteLine();
  397. }
  398. }
  399. void deleteLine() {
  400. final index = convertViewLineToRawLine(_cursorX);
  401. if (index >= height) {
  402. return;
  403. }
  404. lines.removeAt(index);
  405. }
  406. }