buffer.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/circular_list.dart';
  6. import 'package:xterm/utli/scroll_range.dart';
  7. import 'package:xterm/utli/unicode_v11.dart';
  8. class Buffer {
  9. Buffer(this.terminal) {
  10. resetVerticalMargins();
  11. lines = CircularList(
  12. terminal.maxLines,
  13. (BufferLine? line) {
  14. line?.clear();
  15. line?.ensure(terminal.viewWidth);
  16. },
  17. (int) => _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. }
  77. final line = currentLine;
  78. line.ensure(_cursorX + 1);
  79. line.cellInitialize(
  80. _cursorX,
  81. content: codePoint,
  82. width: cellWidth,
  83. cursor: terminal.cursor,
  84. );
  85. if (_cursorX < terminal.viewWidth) {
  86. _cursorX++;
  87. }
  88. if (cellWidth == 2) {
  89. writeChar(0);
  90. }
  91. }
  92. /// get line in the viewport. [index] starts from 0, must be smaller than
  93. /// [Terminal.viewHeight].
  94. BufferLine getViewLine(int index) {
  95. index = index.clamp(0, terminal.viewHeight - 1);
  96. return lines[convertViewLineToRawLine(index)]!;
  97. }
  98. BufferLine get currentLine {
  99. return getViewLine(_cursorY);
  100. }
  101. int get height {
  102. return lines.length;
  103. }
  104. int convertViewLineToRawLine(int viewLine) {
  105. if (terminal.viewHeight > height) {
  106. return viewLine;
  107. }
  108. return viewLine + (height - terminal.viewHeight);
  109. }
  110. int convertRawLineToViewLine(int rawLine) {
  111. if (terminal.viewHeight > height) {
  112. return rawLine;
  113. }
  114. return rawLine - (height - terminal.viewHeight);
  115. }
  116. void newLine() {
  117. if (terminal.newLineMode) {
  118. setCursorX(0);
  119. }
  120. index();
  121. }
  122. void carriageReturn() {
  123. setCursorX(0);
  124. }
  125. void backspace() {
  126. if (_cursorX == 0 && currentLine.isWrapped) {
  127. movePosition(terminal.viewWidth - 1, -1);
  128. } else if (_cursorX == terminal.viewWidth) {
  129. movePosition(-2, 0);
  130. } else {
  131. movePosition(-1, 0);
  132. }
  133. }
  134. List<BufferLine> getVisibleLines() {
  135. if (height < terminal.viewHeight) {
  136. return lines.toList();
  137. }
  138. final result = <BufferLine>[];
  139. for (var i = height - terminal.viewHeight; i < height; i++) {
  140. final y = i - scrollOffsetFromBottom;
  141. if (y >= 0 && y < height) {
  142. result.add(lines[y]!);
  143. }
  144. }
  145. return result;
  146. }
  147. void eraseDisplayFromCursor() {
  148. eraseLineFromCursor();
  149. for (var i = _cursorY + 1; i < terminal.viewHeight; i++) {
  150. getViewLine(i).erase(terminal.cursor, 0, terminal.viewWidth);
  151. }
  152. }
  153. void eraseDisplayToCursor() {
  154. eraseLineToCursor();
  155. for (var i = 0; i < _cursorY; i++) {
  156. getViewLine(i).erase(terminal.cursor, 0, terminal.viewWidth);
  157. }
  158. }
  159. void eraseDisplay() {
  160. for (var i = 0; i < terminal.viewHeight; i++) {
  161. final line = getViewLine(i);
  162. line.erase(terminal.cursor, 0, terminal.viewWidth);
  163. }
  164. }
  165. void eraseLineFromCursor() {
  166. currentLine.erase(terminal.cursor, _cursorX, terminal.viewWidth);
  167. }
  168. void eraseLineToCursor() {
  169. currentLine.erase(terminal.cursor, 0, _cursorX);
  170. }
  171. void eraseLine() {
  172. currentLine.erase(terminal.cursor, 0, terminal.viewWidth);
  173. }
  174. void eraseCharacters(int count) {
  175. final start = _cursorX;
  176. currentLine.erase(terminal.cursor, start, start + count);
  177. }
  178. ScrollRange getAreaScrollRange() {
  179. var top = convertViewLineToRawLine(_marginTop);
  180. var bottom = convertViewLineToRawLine(_marginBottom) + 1;
  181. if (bottom > lines.length) {
  182. bottom = lines.length;
  183. }
  184. return ScrollRange(top, bottom);
  185. }
  186. void areaScrollDown(int lines) {
  187. final scrollRange = getAreaScrollRange();
  188. for (var i = scrollRange.bottom; i > scrollRange.top;) {
  189. i--;
  190. if (i >= scrollRange.top + lines) {
  191. this.lines[i] = this.lines[i - lines];
  192. } else {
  193. this.lines[i] = _newEmptyLine();
  194. }
  195. }
  196. }
  197. void areaScrollUp(int lines) {
  198. final scrollRange = getAreaScrollRange();
  199. for (var i = scrollRange.top; i < scrollRange.bottom; i++) {
  200. if (i + lines < scrollRange.bottom) {
  201. this.lines[i] = this.lines[i + lines];
  202. } else {
  203. this.lines[i] = _newEmptyLine();
  204. }
  205. }
  206. }
  207. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  208. ///
  209. /// ESC D
  210. ///
  211. /// [index] causes the active position to move downward one line without
  212. /// changing the column position. If the active position is at the bottom
  213. /// margin, a scroll up is performed.
  214. void index() {
  215. if (isInScrollableRegion) {
  216. if (_cursorY < _marginBottom) {
  217. moveCursorY(1);
  218. } else {
  219. areaScrollUp(1);
  220. }
  221. return;
  222. }
  223. // the cursor is not in the scrollable region
  224. if (_cursorY >= terminal.viewHeight - 1) {
  225. // we are at the bottom so a new line is created.
  226. lines.addNew();
  227. // keep viewport from moving if user is scrolling.
  228. if (isUserScrolling) {
  229. _scrollOffsetFromBottom++;
  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.trimStart(lines.length - terminal.viewHeight);
  341. }
  342. void clear() {
  343. lines.clear();
  344. for (int i = 0; i < terminal.viewHeight; i++) {
  345. lines.addNew();
  346. }
  347. }
  348. void insertBlankCharacters(int count) {
  349. for (var i = 0; i < count; i++) {
  350. currentLine.insert(_cursorX + i);
  351. currentLine.cellSetFlags(_cursorX + i, terminal.cursor.flags);
  352. }
  353. }
  354. void insertLines(int count) {
  355. if (hasScrollableRegion && !isInScrollableRegion) {
  356. return;
  357. }
  358. setCursorX(0);
  359. for (var i = 0; i < count; i++) {
  360. insertLine();
  361. }
  362. }
  363. void insertLine() {
  364. if (!isInScrollableRegion) {
  365. final index = convertViewLineToRawLine(_cursorX);
  366. final newLine = _newEmptyLine();
  367. lines.splice(index, 0, [newLine]);
  368. } else {
  369. final bottom = convertViewLineToRawLine(marginBottom);
  370. final newLine = _newEmptyLine();
  371. lines.splice(_cursorY, 0, [newLine]);
  372. }
  373. }
  374. void deleteLines(int count) {
  375. if (hasScrollableRegion && !isInScrollableRegion) {
  376. return;
  377. }
  378. setCursorX(0);
  379. for (var i = 0; i < count; i++) {
  380. deleteLine();
  381. }
  382. }
  383. void deleteLine() {
  384. final index = convertViewLineToRawLine(_cursorX);
  385. if (index >= height) {
  386. return;
  387. }
  388. lines.splice(index, 1, []);
  389. }
  390. void resize(int newWidth, int newHeight) {
  391. if (newWidth > terminal.viewWidth) {
  392. lines.forEach((item, index) {
  393. item?.ensure(newWidth);
  394. }, true);
  395. }
  396. if (newHeight > terminal.viewHeight) {
  397. // Grow larger
  398. for (var i = 0; i < newHeight - terminal.viewHeight; i++) {
  399. if (_cursorY < terminal.viewHeight - 1) {
  400. lines.addNew();
  401. } else {
  402. _cursorY++;
  403. }
  404. }
  405. } else {
  406. // Shrink smaller
  407. for (var i = 0; i < terminal.viewHeight - newHeight; i++) {
  408. if (_cursorY < terminal.viewHeight - 1) {
  409. lines.pop();
  410. } else {
  411. _cursorY++;
  412. }
  413. }
  414. }
  415. // Ensure cursor is within the screen.
  416. _cursorX = _cursorX.clamp(0, newWidth - 1);
  417. _cursorY = _cursorY.clamp(0, newHeight - 1);
  418. }
  419. BufferLine _newEmptyLine() {
  420. final line = BufferLine();
  421. line.ensure(terminal.viewWidth);
  422. return line;
  423. }
  424. }