buffer.dart 12 KB

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