buffer.dart 13 KB

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