buffer.dart 13 KB

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