buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. movePosition(terminal.viewWidth - 1, -1);
  131. } else if (_cursorX == terminal.viewWidth) {
  132. movePosition(-2, 0);
  133. } else {
  134. movePosition(-1, 0);
  135. }
  136. }
  137. List<BufferLine> getVisibleLines() {
  138. if (height < terminal.viewHeight) {
  139. return lines.toList();
  140. }
  141. final result = <BufferLine>[];
  142. for (var i = height - terminal.viewHeight; i < height; i++) {
  143. final y = i - scrollOffsetFromBottom;
  144. if (y >= 0 && y < height) {
  145. result.add(lines[y]);
  146. }
  147. }
  148. return result;
  149. }
  150. void eraseDisplayFromCursor() {
  151. eraseLineFromCursor();
  152. for (var i = _cursorY + 1; i < terminal.viewHeight; i++) {
  153. getViewLine(i).erase(terminal.cursor, 0, terminal.viewWidth);
  154. }
  155. }
  156. void eraseDisplayToCursor() {
  157. eraseLineToCursor();
  158. for (var i = 0; i < _cursorY; i++) {
  159. getViewLine(i).erase(terminal.cursor, 0, terminal.viewWidth);
  160. }
  161. }
  162. void eraseDisplay() {
  163. for (var i = 0; i < terminal.viewHeight; i++) {
  164. final line = getViewLine(i);
  165. line.erase(terminal.cursor, 0, terminal.viewWidth);
  166. }
  167. }
  168. void eraseLineFromCursor() {
  169. currentLine.erase(terminal.cursor, _cursorX, terminal.viewWidth);
  170. }
  171. void eraseLineToCursor() {
  172. currentLine.erase(terminal.cursor, 0, _cursorX);
  173. }
  174. void eraseLine() {
  175. currentLine.erase(terminal.cursor, 0, terminal.viewWidth);
  176. }
  177. void eraseCharacters(int count) {
  178. final start = _cursorX;
  179. currentLine.erase(terminal.cursor, start, start + count);
  180. }
  181. ScrollRange getAreaScrollRange() {
  182. var top = convertViewLineToRawLine(_marginTop);
  183. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  184. if (bottom > lines.length) {
  185. bottom = lines.length;
  186. }
  187. return ScrollRange(top, bottom);
  188. }
  189. void areaScrollDown(int lines) {
  190. final scrollRange = getAreaScrollRange();
  191. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  192. i--;
  193. if (i >= scrollRange.top + lines) {
  194. this.lines[i] = this.lines[i - lines];
  195. } else {
  196. this.lines[i] = _newEmptyLine();
  197. }
  198. }
  199. }
  200. void areaScrollUp(int lines) {
  201. final scrollRange = getAreaScrollRange();
  202. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  203. if (i + lines < scrollRange.bottom) {
  204. this.lines[i] = this.lines[i + lines];
  205. } else {
  206. this.lines[i] = _newEmptyLine();
  207. }
  208. }
  209. }
  210. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  211. ///
  212. /// ESC D
  213. ///
  214. /// [index] causes the active position to move downward one line without
  215. /// changing the column position. If the active position is at the bottom
  216. /// margin, a scroll up is performed.
  217. void index() {
  218. if (isInScrollableRegion) {
  219. if (_cursorY < _marginBottom) {
  220. moveCursorY(1);
  221. } else {
  222. areaScrollUp(1);
  223. }
  224. return;
  225. }
  226. // the cursor is not in the scrollable region
  227. if (_cursorY >= terminal.viewHeight - 1) {
  228. // we are at the bottom so a new line is created.
  229. lines.push(_newEmptyLine());
  230. // keep viewport from moving if user is scrolling.
  231. if (isUserScrolling) {
  232. _scrollOffsetFromBottom++;
  233. }
  234. } else {
  235. // there're still lines so we simply move cursor down.
  236. moveCursorY(1);
  237. }
  238. }
  239. /// https://vt100.net/docs/vt100-ug/chapter3.html#RI
  240. void reverseIndex() {
  241. if (_cursorY == _marginTop) {
  242. areaScrollDown(1);
  243. } else if (_cursorY > 0) {
  244. moveCursorY(-1);
  245. }
  246. }
  247. void cursorGoForward() {
  248. setCursorX(_cursorX + 1);
  249. }
  250. void setCursorX(int cursorX) {
  251. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  252. }
  253. void setCursorY(int cursorY) {
  254. _cursorY = cursorY.clamp(0, terminal.viewHeight - 1);
  255. }
  256. void moveCursorX(int offset) {
  257. setCursorX(_cursorX + offset);
  258. }
  259. void moveCursorY(int offset) {
  260. setCursorY(_cursorY + offset);
  261. }
  262. void setPosition(int cursorX, int cursorY) {
  263. var maxLine = terminal.viewHeight - 1;
  264. if (terminal.originMode) {
  265. cursorY += _marginTop;
  266. maxLine = _marginBottom;
  267. }
  268. _cursorX = cursorX.clamp(0, terminal.viewWidth - 1);
  269. _cursorY = cursorY.clamp(0, maxLine);
  270. }
  271. void movePosition(int offsetX, int offsetY) {
  272. final cursorX = _cursorX + offsetX;
  273. final cursorY = _cursorY + offsetY;
  274. setPosition(cursorX, cursorY);
  275. }
  276. void setScrollOffsetFromBottom(int offsetFromBottom) {
  277. if (height < terminal.viewHeight) return;
  278. final maxOffsetFromBottom = height - terminal.viewHeight;
  279. _scrollOffsetFromBottom = offsetFromBottom.clamp(0, maxOffsetFromBottom);
  280. }
  281. void setScrollOffsetFromTop(int offsetFromTop) {
  282. final bottomOffset = terminal.invisibleHeight - offsetFromTop;
  283. setScrollOffsetFromBottom(bottomOffset);
  284. }
  285. void screenScrollUp(int lines) {
  286. setScrollOffsetFromBottom(scrollOffsetFromBottom + lines);
  287. }
  288. void screenScrollDown(int lines) {
  289. setScrollOffsetFromBottom(scrollOffsetFromBottom - lines);
  290. }
  291. void saveCursor() {
  292. _savedCellFlags = terminal.cursor.flags;
  293. _savedCellFgColor = terminal.cursor.fg;
  294. _savedCellBgColor = terminal.cursor.bg;
  295. _savedCursorX = _cursorX;
  296. _savedCursorY = _cursorY;
  297. charset.save();
  298. }
  299. void restoreCursor() {
  300. if (_savedCellFlags != null) {
  301. terminal.cursor.flags = _savedCellFlags!;
  302. }
  303. if (_savedCellFgColor != null) {
  304. terminal.cursor.fg = _savedCellFgColor!;
  305. }
  306. if (_savedCellBgColor != null) {
  307. terminal.cursor.bg = _savedCellBgColor!;
  308. }
  309. if (_savedCursorX != null) {
  310. _cursorX = _savedCursorX!;
  311. }
  312. if (_savedCursorY != null) {
  313. _cursorY = _savedCursorY!;
  314. }
  315. charset.restore();
  316. }
  317. void setVerticalMargins(int top, int bottom) {
  318. _marginTop = top.clamp(0, terminal.viewHeight - 1);
  319. _marginBottom = bottom.clamp(0, terminal.viewHeight - 1);
  320. _marginTop = min(_marginTop, _marginBottom);
  321. _marginBottom = max(_marginTop, _marginBottom);
  322. }
  323. bool get hasScrollableRegion {
  324. return _marginTop > 0 || _marginBottom < (terminal.viewHeight - 1);
  325. }
  326. bool get isInScrollableRegion {
  327. return hasScrollableRegion &&
  328. _cursorY >= _marginTop &&
  329. _cursorY <= _marginBottom;
  330. }
  331. void resetVerticalMargins() {
  332. setVerticalMargins(0, terminal.viewHeight - 1);
  333. }
  334. void deleteChars(int count) {
  335. final start = _cursorX.clamp(0, terminal.viewWidth);
  336. final end = min(_cursorX + count, terminal.viewWidth);
  337. currentLine.removeRange(start, end);
  338. }
  339. void clearScrollback() {
  340. if (lines.length <= terminal.viewHeight) {
  341. return;
  342. }
  343. lines.trimStart(lines.length - terminal.viewHeight);
  344. }
  345. void clear() {
  346. lines.clear();
  347. for (int i = 0; i < terminal.viewHeight; i++) {
  348. lines.push(_newEmptyLine());
  349. }
  350. }
  351. void insertBlankCharacters(int count) {
  352. for (var i = 0; i < count; i++) {
  353. currentLine.insert(_cursorX + i);
  354. currentLine.cellSetFlags(_cursorX + i, terminal.cursor.flags);
  355. }
  356. }
  357. void insertLines(int count) {
  358. if (hasScrollableRegion && !isInScrollableRegion) {
  359. return;
  360. }
  361. setCursorX(0);
  362. for (var i = 0; i < count; i++) {
  363. insertLine();
  364. }
  365. }
  366. void insertLine() {
  367. if (!isInScrollableRegion) {
  368. final index = convertViewLineToRawLine(_cursorX);
  369. final newLine = _newEmptyLine();
  370. lines.insert(index, newLine);
  371. } else {
  372. final newLine = _newEmptyLine();
  373. lines.insert(_cursorY, newLine);
  374. }
  375. }
  376. void deleteLines(int count) {
  377. if (hasScrollableRegion && !isInScrollableRegion) {
  378. return;
  379. }
  380. setCursorX(0);
  381. for (var i = 0; i < count; i++) {
  382. deleteLine();
  383. }
  384. }
  385. void deleteLine() {
  386. final index = convertViewLineToRawLine(_cursorX);
  387. if (index >= height) {
  388. return;
  389. }
  390. lines.remove(index);
  391. }
  392. void resize(int oldWidth, int oldHeight, int newWidth, int newHeight) {
  393. if (newWidth > oldWidth) {
  394. lines.forEach((item) => item.ensure(newWidth));
  395. }
  396. if (newHeight > oldHeight) {
  397. while (lines.length < newHeight) {
  398. lines.push(_newEmptyLine());
  399. }
  400. // Grow larger
  401. for (var i = 0; i < newHeight - oldHeight; i++) {
  402. if (_cursorY < oldHeight - 1) {
  403. lines.push(_newEmptyLine());
  404. } else {
  405. _cursorY++;
  406. }
  407. }
  408. } else {
  409. // Shrink smaller
  410. for (var i = 0; i < oldHeight - newHeight; i++) {
  411. if (_cursorY < oldHeight - 1) {
  412. lines.pop();
  413. } else {
  414. _cursorY++;
  415. }
  416. }
  417. }
  418. // Ensure cursor is within the screen.
  419. _cursorX = _cursorX.clamp(0, newWidth - 1);
  420. _cursorY = _cursorY.clamp(0, newHeight - 1);
  421. if (!terminal.isUsingAltBuffer()) {
  422. final reflowStrategy = newWidth > oldWidth
  423. ? ReflowStrategyWider(this)
  424. : ReflowStrategyNarrower(this);
  425. reflowStrategy.reflow(newWidth, newHeight, oldWidth, oldHeight);
  426. }
  427. }
  428. BufferLine _newEmptyLine() {
  429. final line = BufferLine();
  430. line.ensure(terminal.viewWidth);
  431. return line;
  432. }
  433. adjustSavedCursor(int dx, int dy) {
  434. if (_savedCursorX != null) {
  435. _savedCursorX = _savedCursorX! + dx;
  436. }
  437. if (_savedCursorY != null) {
  438. _savedCursorY = _savedCursorY! + dy;
  439. }
  440. }
  441. }