buffer.dart 13 KB

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