buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. import 'dart:math' show max, min;
  2. import 'package:xterm/buffer/buffer_line.dart';
  3. import 'package:xterm/buffer/buffer_reflow.dart';
  4. import 'package:xterm/terminal/charset.dart';
  5. import 'package:xterm/terminal/terminal.dart';
  6. import 'package:xterm/utli/scroll_range.dart';
  7. import 'package:xterm/utli/unicode_v11.dart';
  8. class Buffer {
  9. Buffer(this.terminal) {
  10. resetVerticalMargins();
  11. lines = List.generate(
  12. terminal.viewHeight,
  13. (_) => _newEmptyLine(),
  14. );
  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. int? _savedCellFgColor;
  24. int? _savedCellBgColor;
  25. int? _savedCellFlags;
  26. // Indicates how far the bottom of the viewport is from the bottom of the
  27. // entire buffer. 0 if the viewport overlaps the terminal screen.
  28. int get scrollOffsetFromBottom => _scrollOffsetFromBottom;
  29. int _scrollOffsetFromBottom = 0;
  30. // Indicates how far the top of the viewport is from the top of the entire
  31. // buffer. 0 if the viewport is scrolled to the top.
  32. int get scrollOffsetFromTop {
  33. return terminal.invisibleHeight - scrollOffsetFromBottom;
  34. }
  35. /// Indicated whether the terminal should automatically scroll to bottom when
  36. /// new lines are added. When user is scrolling, [isUserScrolling] is true and
  37. /// the automatical scroll-to-bottom behavior is disabled.
  38. bool get isUserScrolling {
  39. return _scrollOffsetFromBottom != 0;
  40. }
  41. /// Horizontal position of the cursor relative to the top-left cornor of the
  42. /// screen, starting from 0.
  43. int get cursorX => _cursorX.clamp(0, terminal.viewWidth - 1);
  44. int _cursorX = 0;
  45. /// Vertical position of the cursor relative to the top-left cornor of the
  46. /// screen, starting from 0.
  47. int get cursorY => _cursorY;
  48. int _cursorY = 0;
  49. int get marginTop => _marginTop;
  50. late int _marginTop;
  51. int get marginBottom => _marginBottom;
  52. late int _marginBottom;
  53. /// Writes data to the terminal. Terminal sequences or special characters are
  54. /// not interpreted and directly added to the buffer.
  55. ///
  56. /// See also: [Terminal.write]
  57. void write(String text) {
  58. for (var char in text.runes) {
  59. writeChar(char);
  60. }
  61. }
  62. /// Writes a single character to the terminal. Special chatacters are not
  63. /// interpreted and directly added to the buffer.
  64. ///
  65. /// See also: [Terminal.writeChar]
  66. void writeChar(int codePoint) {
  67. codePoint = charset.translate(codePoint);
  68. final cellWidth = unicodeV11.wcwidth(codePoint);
  69. if (_cursorX >= terminal.viewWidth) {
  70. newLine();
  71. setCursorX(0);
  72. }
  73. final line = currentLine;
  74. line.ensure(_cursorX + 1);
  75. line.cellInitialize(
  76. _cursorX,
  77. content: codePoint,
  78. width: cellWidth,
  79. cursor: terminal.cursor,
  80. );
  81. if (_cursorX < terminal.viewWidth) {
  82. _cursorX++;
  83. }
  84. if (cellWidth == 2) {
  85. writeChar(0);
  86. }
  87. }
  88. /// get line in the viewport. [index] starts from 0, must be smaller than
  89. /// [Terminal.viewHeight].
  90. BufferLine getViewLine(int index) {
  91. index = index.clamp(0, terminal.viewHeight - 1);
  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.cursor, 0, terminal.viewWidth);
  147. }
  148. }
  149. void eraseDisplayToCursor() {
  150. eraseLineToCursor();
  151. for (var i = 0; i < _cursorY; i++) {
  152. getViewLine(i).erase(terminal.cursor, 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.cursor, 0, terminal.viewWidth);
  159. }
  160. }
  161. void eraseLineFromCursor() {
  162. currentLine.erase(terminal.cursor, _cursorX, terminal.viewWidth);
  163. }
  164. void eraseLineToCursor() {
  165. currentLine.erase(terminal.cursor, 0, _cursorX);
  166. }
  167. void eraseLine() {
  168. currentLine.erase(terminal.cursor, 0, terminal.viewWidth);
  169. }
  170. void eraseCharacters(int count) {
  171. final start = _cursorX;
  172. currentLine.erase(terminal.cursor, start, start + count);
  173. }
  174. ScrollRange getAreaScrollRange() {
  175. var top = convertViewLineToRawLine(_marginTop);
  176. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  177. if (bottom > lines.length) {
  178. bottom = lines.length;
  179. }
  180. return ScrollRange(top, bottom);
  181. }
  182. void areaScrollDown(int lines) {
  183. final scrollRange = getAreaScrollRange();
  184. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  185. i--;
  186. if (i >= scrollRange.top + lines) {
  187. this.lines[i] = this.lines[i - lines];
  188. } else {
  189. this.lines[i] = _newEmptyLine();
  190. }
  191. }
  192. }
  193. void areaScrollUp(int lines) {
  194. final scrollRange = getAreaScrollRange();
  195. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  196. if (i + lines < scrollRange.bottom) {
  197. this.lines[i] = this.lines[i + lines];
  198. } else {
  199. this.lines[i] = _newEmptyLine();
  200. }
  201. }
  202. }
  203. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  204. ///
  205. /// ESC D
  206. ///
  207. /// [index] causes the active position to move downward one line without
  208. /// changing the column position. If the active position is at the bottom
  209. /// margin, a scroll up is performed.
  210. void index() {
  211. if (isInScrollableRegion) {
  212. if (_cursorY < _marginBottom) {
  213. moveCursorY(1);
  214. } else {
  215. areaScrollUp(1);
  216. }
  217. return;
  218. }
  219. // the cursor is not in the scrollable region
  220. if (_cursorY >= terminal.viewHeight - 1) {
  221. // we are at the bottom so a new line is created.
  222. lines.add(_newEmptyLine());
  223. // keep viewport from moving if user is scrolling.
  224. if (isUserScrolling) {
  225. _scrollOffsetFromBottom++;
  226. }
  227. // clean extra lines if needed.
  228. final maxLines = terminal.maxLines;
  229. if (maxLines != null && lines.length > maxLines) {
  230. lines.removeRange(0, lines.length - maxLines);
  231. }
  232. } else {
  233. // there're still lines so we simply move cursor down.
  234. moveCursorY(1);
  235. }
  236. }
  237. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  238. void reverseIndex() {
  239. if (_cursorY == _marginTop) {
  240. areaScrollDown(1);
  241. } else if (_cursorY > 0) {
  242. moveCursorY(-1);
  243. }
  244. }
  245. void cursorGoForward() {
  246. setCursorX(_cursorX + 1);
  247. }
  248. void setCursorX(int cursorX) {
  249. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  250. }
  251. void setCursorY(int cursorY) {
  252. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  253. }
  254. void moveCursorX(int offset) {
  255. setCursorX(_cursorX + offset);
  256. }
  257. void moveCursorY(int offset) {
  258. setCursorY(_cursorY + offset);
  259. }
  260. void setPosition(int cursorX, int cursorY) {
  261. var maxLine = terminal.viewHeight - 1;
  262. if (terminal.originMode) {
  263. cursorY += _marginTop;
  264. maxLine = _marginBottom;
  265. }
  266. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  267. _cursorY = cursorY.clamp(0, maxLine);
  268. }
  269. void movePosition(int offsetX, int offsetY) {
  270. final cursorX = _cursorX + offsetX;
  271. final cursorY = _cursorY + offsetY;
  272. setPosition(cursorX, cursorY);
  273. }
  274. void setScrollOffsetFromBottom(int offsetFromBottom) {
  275. if (height < terminal.viewHeight) return;
  276. final maxOffsetFromBottom = height - terminal.viewHeight;
  277. _scrollOffsetFromBottom = offsetFromBottom.clamp(0, maxOffsetFromBottom);
  278. }
  279. void setScrollOffsetFromTop(int offsetFromTop) {
  280. final bottomOffset = terminal.invisibleHeight - offsetFromTop;
  281. setScrollOffsetFromBottom(bottomOffset);
  282. }
  283. void screenScrollUp(int lines) {
  284. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  285. }
  286. void screenScrollDown(int lines) {
  287. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  288. }
  289. void saveCursor() {
  290. _savedCellFlags = terminal.cursor.flags;
  291. _savedCellFgColor = terminal.cursor.fg;
  292. _savedCellBgColor = terminal.cursor.bg;
  293. _savedCursorX = _cursorX;
  294. _savedCursorY = _cursorY;
  295. charset.save();
  296. }
  297. void restoreCursor() {
  298. if (_savedCellFlags != null) {
  299. terminal.cursor.flags = _savedCellFlags!;
  300. }
  301. if (_savedCellFgColor != null) {
  302. terminal.cursor.fg = _savedCellFgColor!;
  303. }
  304. if (_savedCellBgColor != null) {
  305. terminal.cursor.bg = _savedCellBgColor!;
  306. }
  307. if (_savedCursorX != null) {
  308. _cursorX = _savedCursorX!;
  309. }
  310. if (_savedCursorY != null) {
  311. _cursorY = _savedCursorY!;
  312. }
  313. charset.restore();
  314. }
  315. void setVerticalMargins(int top, int bottom) {
  316. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  317. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  318. _marginTop = min(_marginTop, _marginBottom);
  319. _marginBottom = max(_marginTop, _marginBottom);
  320. }
  321. bool get hasScrollableRegion {
  322. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  323. }
  324. bool get isInScrollableRegion {
  325. return hasScrollableRegion &&
  326. _cursorY >= _marginTop &&
  327. _cursorY <= _marginBottom;
  328. }
  329. void resetVerticalMargins() {
  330. setVerticalMargins(0, terminal.viewHeight - 1);
  331. }
  332. void deleteChars(int count) {
  333. final start = _cursorX.clamp(0, terminal.viewWidth);
  334. final end = min(_cursorX + count, terminal.viewWidth);
  335. currentLine.removeRange(start, end);
  336. }
  337. void clearScrollback() {
  338. if (lines.length <= terminal.viewHeight) {
  339. return;
  340. }
  341. lines.removeRange(0, lines.length - terminal.viewHeight);
  342. }
  343. void clear() {
  344. lines.clear();
  345. lines.addAll(List.generate(
  346. terminal.viewHeight,
  347. (_) => _newEmptyLine(),
  348. ));
  349. }
  350. void insertBlankCharacters(int count) {
  351. for (var i = 0; i < count; i++) {
  352. currentLine.insert(_cursorX + i);
  353. currentLine.cellSetFlags(_cursorX + i, terminal.cursor.flags);
  354. }
  355. }
  356. void insertLines(int count) {
  357. if (hasScrollableRegion && !isInScrollableRegion) {
  358. return;
  359. }
  360. setCursorX(0);
  361. for (var i = 0; i < count; i++) {
  362. insertLine();
  363. }
  364. }
  365. void insertLine() {
  366. if (!isInScrollableRegion) {
  367. final index = convertViewLineToRawLine(_cursorX);
  368. final newLine = _newEmptyLine();
  369. lines.insert(index, newLine);
  370. final maxLines = terminal.maxLines;
  371. if (maxLines != null && lines.length > maxLines) {
  372. lines.removeRange(0, lines.length - maxLines);
  373. }
  374. } else {
  375. final bottom = convertViewLineToRawLine(marginBottom);
  376. final movedLines = lines.getRange(_cursorY, bottom - 1);
  377. lines.setRange(_cursorY + 1, bottom, movedLines);
  378. final newLine = _newEmptyLine();
  379. lines[_cursorY] = newLine;
  380. }
  381. }
  382. void deleteLines(int count) {
  383. if (hasScrollableRegion && !isInScrollableRegion) {
  384. return;
  385. }
  386. setCursorX(0);
  387. for (var i = 0; i < count; i++) {
  388. deleteLine();
  389. }
  390. }
  391. void deleteLine() {
  392. final index = convertViewLineToRawLine(_cursorX);
  393. if (index >= height) {
  394. return;
  395. }
  396. lines.removeAt(index);
  397. }
  398. void resize(int oldWidth, int oldHeight, int newWidth, int newHeight) {
  399. if (newWidth > oldWidth) {
  400. for (var line in lines) {
  401. line.ensure(newWidth);
  402. }
  403. }
  404. if (newHeight > oldHeight) {
  405. // Grow larger
  406. for (var i = 0; i < newHeight - oldHeight; i++) {
  407. if (_cursorY < oldHeight - 1) {
  408. lines.add(_newEmptyLine());
  409. } else {
  410. _cursorY++;
  411. }
  412. }
  413. } else {
  414. // Shrink smaller
  415. for (var i = 0; i < oldHeight - newHeight; i++) {
  416. if (_cursorY < oldHeight - 1) {
  417. lines.removeLast();
  418. } else {
  419. _cursorY++;
  420. }
  421. }
  422. }
  423. // Ensure cursor is within the screen.
  424. _cursorX = _cursorX.clamp(0, newWidth - 1);
  425. _cursorY = _cursorY.clamp(0, newHeight - 1);
  426. if (!terminal.isUsingAltBuffer()) {
  427. final rf = BufferReflow(this);
  428. rf.doReflow(oldWidth, newWidth);
  429. }
  430. }
  431. BufferLine _newEmptyLine() {
  432. final line = BufferLine();
  433. line.ensure(terminal.viewWidth);
  434. return line;
  435. }
  436. adjustSavedCursor(int dx, int dy) {
  437. if (_savedCursorX != null) {
  438. _savedCursorX = _savedCursorX! + dx;
  439. }
  440. if (_savedCursorY != null) {
  441. _savedCursorY = _savedCursorY! + dy;
  442. }
  443. }
  444. }