buffer.dart 13 KB

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