buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. if (terminal.autoWrapMode) {
  73. currentLine.isWrapped = true;
  74. }
  75. }
  76. final line = currentLine;
  77. line.ensure(_cursorX + 1);
  78. line.cellInitialize(
  79. _cursorX,
  80. content: codePoint,
  81. width: cellWidth,
  82. cursor: terminal.cursor,
  83. );
  84. if (_cursorX < terminal.viewWidth) {
  85. _cursorX++;
  86. }
  87. if (cellWidth == 2) {
  88. writeChar(0);
  89. }
  90. }
  91. /// get line in the viewport. [index] starts from 0, must be smaller than
  92. /// [Terminal.viewHeight].
  93. BufferLine getViewLine(int index) {
  94. index = index.clamp(0, terminal.viewHeight - 1);
  95. return lines[convertViewLineToRawLine(index)];
  96. }
  97. BufferLine get currentLine {
  98. return getViewLine(_cursorY);
  99. }
  100. int get height {
  101. return lines.length;
  102. }
  103. int convertViewLineToRawLine(int viewLine) {
  104. if (terminal.viewHeight > height) {
  105. return viewLine;
  106. }
  107. return viewLine + (height - terminal.viewHeight);
  108. }
  109. int convertRawLineToViewLine(int rawLine) {
  110. if (terminal.viewHeight > height) {
  111. return rawLine;
  112. }
  113. return rawLine - (height - terminal.viewHeight);
  114. }
  115. void newLine() {
  116. if (terminal.newLineMode) {
  117. setCursorX(0);
  118. }
  119. index();
  120. }
  121. void carriageReturn() {
  122. setCursorX(0);
  123. }
  124. void backspace() {
  125. if (_cursorX == 0 && currentLine.isWrapped) {
  126. movePosition(terminal.viewWidth - 1, -1);
  127. } else if (_cursorX == terminal.viewWidth) {
  128. movePosition(-2, 0);
  129. } else {
  130. movePosition(-1, 0);
  131. }
  132. }
  133. List<BufferLine> getVisibleLines() {
  134. if (height < terminal.viewHeight) {
  135. return lines.toList();
  136. }
  137. final result = <BufferLine>[];
  138. for (var i = height - terminal.viewHeight; i < height; i++) {
  139. final y = i - scrollOffsetFromBottom;
  140. if (y >= 0 && y < height) {
  141. result.add(lines[y]);
  142. }
  143. }
  144. return result;
  145. }
  146. void eraseDisplayFromCursor() {
  147. eraseLineFromCursor();
  148. for (var i = _cursorY + 1; i < terminal.viewHeight; i++) {
  149. getViewLine(i).erase(terminal.cursor, 0, terminal.viewWidth);
  150. }
  151. }
  152. void eraseDisplayToCursor() {
  153. eraseLineToCursor();
  154. for (var i = 0; i < _cursorY; i++) {
  155. getViewLine(i).erase(terminal.cursor, 0, terminal.viewWidth);
  156. }
  157. }
  158. void eraseDisplay() {
  159. for (var i = 0; i < terminal.viewHeight; i++) {
  160. final line = getViewLine(i);
  161. line.erase(terminal.cursor, 0, terminal.viewWidth);
  162. }
  163. }
  164. void eraseLineFromCursor() {
  165. currentLine.erase(terminal.cursor, _cursorX, terminal.viewWidth);
  166. }
  167. void eraseLineToCursor() {
  168. currentLine.erase(terminal.cursor, 0, _cursorX);
  169. }
  170. void eraseLine() {
  171. currentLine.erase(terminal.cursor, 0, terminal.viewWidth);
  172. }
  173. void eraseCharacters(int count) {
  174. final start = _cursorX;
  175. currentLine.erase(terminal.cursor, start, start + count);
  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] = _newEmptyLine();
  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] = _newEmptyLine();
  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(_newEmptyLine());
  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. void cursorGoForward() {
  249. setCursorX(_cursorX + 1);
  250. }
  251. void setCursorX(int cursorX) {
  252. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  253. }
  254. void setCursorY(int cursorY) {
  255. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  256. }
  257. void moveCursorX(int offset) {
  258. setCursorX(_cursorX + offset);
  259. }
  260. void moveCursorY(int offset) {
  261. setCursorY(_cursorY + offset);
  262. }
  263. void setPosition(int cursorX, int cursorY) {
  264. var maxLine = terminal.viewHeight - 1;
  265. if (terminal.originMode) {
  266. cursorY += _marginTop;
  267. maxLine = _marginBottom;
  268. }
  269. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  270. _cursorY = cursorY.clamp(0, maxLine);
  271. }
  272. void movePosition(int offsetX, int offsetY) {
  273. final cursorX = _cursorX + offsetX;
  274. final cursorY = _cursorY + offsetY;
  275. setPosition(cursorX, cursorY);
  276. }
  277. void setScrollOffsetFromBottom(int offsetFromBottom) {
  278. if (height < terminal.viewHeight) return;
  279. final maxOffsetFromBottom = height - terminal.viewHeight;
  280. _scrollOffsetFromBottom = offsetFromBottom.clamp(0, maxOffsetFromBottom);
  281. }
  282. void setScrollOffsetFromTop(int offsetFromTop) {
  283. final bottomOffset = terminal.invisibleHeight - offsetFromTop;
  284. setScrollOffsetFromBottom(bottomOffset);
  285. }
  286. void screenScrollUp(int lines) {
  287. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  288. }
  289. void screenScrollDown(int lines) {
  290. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  291. }
  292. void saveCursor() {
  293. _savedCellFlags = terminal.cursor.flags;
  294. _savedCellFgColor = terminal.cursor.fg;
  295. _savedCellBgColor = terminal.cursor.bg;
  296. _savedCursorX = _cursorX;
  297. _savedCursorY = _cursorY;
  298. charset.save();
  299. }
  300. void restoreCursor() {
  301. if (_savedCellFlags != null) {
  302. terminal.cursor.flags = _savedCellFlags!;
  303. }
  304. if (_savedCellFgColor != null) {
  305. terminal.cursor.fg = _savedCellFgColor!;
  306. }
  307. if (_savedCellBgColor != null) {
  308. terminal.cursor.bg = _savedCellBgColor!;
  309. }
  310. if (_savedCursorX != null) {
  311. _cursorX = _savedCursorX!;
  312. }
  313. if (_savedCursorY != null) {
  314. _cursorY = _savedCursorY!;
  315. }
  316. charset.restore();
  317. }
  318. void setVerticalMargins(int top, int bottom) {
  319. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  320. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  321. _marginTop = min(_marginTop, _marginBottom);
  322. _marginBottom = max(_marginTop, _marginBottom);
  323. }
  324. bool get hasScrollableRegion {
  325. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  326. }
  327. bool get isInScrollableRegion {
  328. return hasScrollableRegion &&
  329. _cursorY >= _marginTop &&
  330. _cursorY <= _marginBottom;
  331. }
  332. void resetVerticalMargins() {
  333. setVerticalMargins(0, terminal.viewHeight - 1);
  334. }
  335. void deleteChars(int count) {
  336. final start = _cursorX.clamp(0, terminal.viewWidth);
  337. final end = min(_cursorX + count, terminal.viewWidth);
  338. currentLine.removeRange(start, end);
  339. }
  340. void clearScrollback() {
  341. if (lines.length <= terminal.viewHeight) {
  342. return;
  343. }
  344. lines.removeRange(0, lines.length - terminal.viewHeight);
  345. }
  346. void clear() {
  347. lines.clear();
  348. lines.addAll(List.generate(
  349. terminal.viewHeight,
  350. (_) => _newEmptyLine(),
  351. ));
  352. }
  353. void insertBlankCharacters(int count) {
  354. for (var i = 0; i < count; i++) {
  355. currentLine.insert(_cursorX + i);
  356. currentLine.cellSetFlags(_cursorX + i, terminal.cursor.flags);
  357. }
  358. }
  359. void insertLines(int count) {
  360. if (hasScrollableRegion && !isInScrollableRegion) {
  361. return;
  362. }
  363. setCursorX(0);
  364. for (var i = 0; i < count; i++) {
  365. insertLine();
  366. }
  367. }
  368. void insertLine() {
  369. if (!isInScrollableRegion) {
  370. final index = convertViewLineToRawLine(_cursorX);
  371. final newLine = _newEmptyLine();
  372. lines.insert(index, newLine);
  373. final maxLines = terminal.maxLines;
  374. if (maxLines != null && lines.length > maxLines) {
  375. lines.removeRange(0, lines.length - maxLines);
  376. }
  377. } else {
  378. final bottom = convertViewLineToRawLine(marginBottom);
  379. final movedLines = lines.getRange(_cursorY, bottom - 1);
  380. lines.setRange(_cursorY + 1, bottom, movedLines);
  381. final newLine = _newEmptyLine();
  382. lines[_cursorY] = newLine;
  383. }
  384. }
  385. void deleteLines(int count) {
  386. if (hasScrollableRegion && !isInScrollableRegion) {
  387. return;
  388. }
  389. setCursorX(0);
  390. for (var i = 0; i < count; i++) {
  391. deleteLine();
  392. }
  393. }
  394. void deleteLine() {
  395. final index = convertViewLineToRawLine(_cursorX);
  396. if (index >= height) {
  397. return;
  398. }
  399. lines.removeAt(index);
  400. }
  401. void resize(int oldWidth, int oldHeight, int newWidth, int newHeight) {
  402. if (newWidth > oldWidth) {
  403. for (var line in lines) {
  404. line.ensure(newWidth);
  405. }
  406. }
  407. if (newHeight > oldHeight) {
  408. // Grow larger
  409. for (var i = 0; i < newHeight - oldHeight; i++) {
  410. if (_cursorY < oldHeight - 1) {
  411. lines.add(_newEmptyLine());
  412. } else {
  413. _cursorY++;
  414. }
  415. }
  416. } else {
  417. // Shrink smaller
  418. for (var i = 0; i < oldHeight - newHeight; i++) {
  419. if (_cursorY < oldHeight - 1) {
  420. lines.removeLast();
  421. } else {
  422. _cursorY++;
  423. }
  424. }
  425. }
  426. // Ensure cursor is within the screen.
  427. _cursorX = _cursorX.clamp(0, newWidth - 1);
  428. _cursorY = _cursorY.clamp(0, newHeight - 1);
  429. if (!terminal.isUsingAltBuffer()) {
  430. final rf = BufferReflow(this);
  431. rf.doReflow(oldWidth, newWidth);
  432. }
  433. }
  434. BufferLine _newEmptyLine() {
  435. final line = BufferLine();
  436. line.ensure(terminal.viewWidth);
  437. return line;
  438. }
  439. adjustSavedCursor(int dx, int dy) {
  440. if (_savedCursorX != null) {
  441. _savedCursorX = _savedCursorX! + dx;
  442. }
  443. if (_savedCursorY != null) {
  444. _savedCursorY = _savedCursorY! + dy;
  445. }
  446. }
  447. }