buffer.dart 14 KB

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