buffer.dart 14 KB

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