terminal.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. import 'dart:math' show max;
  2. import 'package:xterm/src/core/buffer/buffer.dart';
  3. import 'package:xterm/src/core/buffer/line.dart';
  4. import 'package:xterm/src/core/cursor.dart';
  5. import 'package:xterm/src/core/escape/emitter.dart';
  6. import 'package:xterm/src/core/escape/handler.dart';
  7. import 'package:xterm/src/core/escape/parser.dart';
  8. import 'package:xterm/src/core/input/handler.dart';
  9. import 'package:xterm/src/core/input/keys.dart';
  10. import 'package:xterm/src/core/mouse.dart';
  11. import 'package:xterm/src/core/state.dart';
  12. import 'package:xterm/src/core/tabs.dart';
  13. import 'package:xterm/src/utils/ascii.dart';
  14. import 'package:xterm/src/utils/circular_list.dart';
  15. import 'package:xterm/src/utils/observable.dart';
  16. import 'package:xterm/src/utils/platform.dart';
  17. class Terminal with Observable implements TerminalState, EscapeHandler {
  18. final int maxLines;
  19. void Function()? onBell;
  20. void Function(String)? onTitleChange;
  21. void Function(String)? onIconChange;
  22. void Function(String)? onOutput;
  23. void Function(int width, int height, int pixelWidth, int pixelHeight)?
  24. onResize;
  25. /// Flag to toggle os specific behaviors.
  26. final TerminalTargetPlatform platform;
  27. Terminal({
  28. this.maxLines = 1000,
  29. this.onBell,
  30. this.onTitleChange,
  31. this.onIconChange,
  32. this.onOutput,
  33. this.onResize,
  34. this.platform = TerminalTargetPlatform.unknown,
  35. TerminalInputHandler inputHandler = defaultInputHandler,
  36. }) : _inputHandler = inputHandler;
  37. TerminalInputHandler _inputHandler;
  38. late final _parser = EscapeParser(this);
  39. final _emitter = const EscapeEmitter();
  40. late var _buffer = _mainBuffer;
  41. late final _mainBuffer = Buffer(this, maxLines: maxLines, isAltBuffer: false);
  42. late final _altBuffer = Buffer(this, maxLines: maxLines, isAltBuffer: true);
  43. final tabStops = TabStops();
  44. var _precedingCodepoint = 0;
  45. /* TerminalState */
  46. int _viewWidth = 80;
  47. int _viewHeight = 24;
  48. final _cursorStyle = CursorStyle();
  49. bool _insertMode = false;
  50. bool _lineFeedMode = false;
  51. bool _cursorKeysMode = false;
  52. bool _reverseDisplayMode = false;
  53. bool _originMode = false;
  54. bool _autoWrapMode = true;
  55. MouseMode _mouseMode = MouseMode.none;
  56. MouseReportMode _mouseReportMode = MouseReportMode.normal;
  57. bool _cursorBlinkMode = false;
  58. bool _cursorVisibleMode = true;
  59. bool _appKeypadMode = false;
  60. bool _reportFocusMode = false;
  61. bool _altBufferMouseScrollMode = false;
  62. bool _bracketedPasteMode = false;
  63. /* State getters */
  64. @override
  65. int get viewWidth => _viewWidth;
  66. @override
  67. int get viewHeight => _viewHeight;
  68. @override
  69. CursorStyle get cursor => _cursorStyle;
  70. @override
  71. bool get insertMode => _insertMode;
  72. @override
  73. bool get lineFeedMode => _lineFeedMode;
  74. @override
  75. bool get cursorKeysMode => _cursorKeysMode;
  76. @override
  77. bool get reverseDisplayMode => _reverseDisplayMode;
  78. @override
  79. bool get originMode => _originMode;
  80. @override
  81. bool get autoWrapMode => _autoWrapMode;
  82. @override
  83. MouseMode get mouseMode => _mouseMode;
  84. MouseReportMode get mouseReportMode => _mouseReportMode;
  85. @override
  86. bool get cursorBlinkMode => _cursorBlinkMode;
  87. @override
  88. bool get cursorVisibleMode => _cursorVisibleMode;
  89. @override
  90. bool get appKeypadMode => _appKeypadMode;
  91. @override
  92. bool get reportFocusMode => _reportFocusMode;
  93. @override
  94. bool get altBufferMouseScrollMode => _altBufferMouseScrollMode;
  95. @override
  96. bool get bracketedPasteMode => _bracketedPasteMode;
  97. Buffer get buffer => _buffer;
  98. Buffer get mainBuffer => _mainBuffer;
  99. Buffer get altBuffer => _altBuffer;
  100. bool get isUsingAltBuffer => _buffer == _altBuffer;
  101. CircularList<BufferLine> get lines => _buffer.lines;
  102. void setInputHandler(TerminalInputHandler inputHandler) {
  103. _inputHandler = inputHandler;
  104. }
  105. void write(String data) {
  106. _parser.write(data);
  107. notifyListeners();
  108. }
  109. bool keyInput(
  110. TerminalKey key, {
  111. bool shift = false,
  112. bool alt = false,
  113. bool ctrl = false,
  114. }) {
  115. final output = _inputHandler(
  116. TerminalInputEvent(
  117. key: key,
  118. shift: shift,
  119. alt: alt,
  120. ctrl: ctrl,
  121. state: this,
  122. altBuffer: isUsingAltBuffer,
  123. platform: platform,
  124. ),
  125. );
  126. if (output != null) {
  127. onOutput?.call(output);
  128. return true;
  129. }
  130. return false;
  131. }
  132. bool charInput(
  133. int charCode, {
  134. bool alt = false,
  135. bool ctrl = false,
  136. }) {
  137. if (ctrl) {
  138. // a(97) ~ z(122)
  139. if (charCode >= Ascii.a && charCode <= Ascii.z) {
  140. final output = charCode - Ascii.a + 1;
  141. onOutput?.call(String.fromCharCode(output));
  142. return true;
  143. }
  144. // [(91) ~ _(95)
  145. if (charCode >= Ascii.openBracket && charCode <= Ascii.underscore) {
  146. final output = charCode - Ascii.openBracket + 27;
  147. onOutput?.call(String.fromCharCode(output));
  148. return true;
  149. }
  150. }
  151. if (alt && platform != TerminalTargetPlatform.macos) {
  152. if (charCode >= Ascii.a && charCode <= Ascii.z) {
  153. final code = charCode - Ascii.a + 65;
  154. final input = [0x1b, code];
  155. onOutput?.call(String.fromCharCodes(input));
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. void textInput(String text) {
  162. onOutput?.call(text);
  163. }
  164. void paste(String text) {
  165. if (_bracketedPasteMode) {
  166. onOutput?.call(_emitter.bracketedPaste(text));
  167. } else {
  168. textInput(text);
  169. }
  170. }
  171. /// Resize the terminal screen. [newWidth] and [newHeight] should be greater
  172. /// than 0. Text reflow is currently not implemented and will be avaliable in
  173. /// the future.
  174. void resize(
  175. int newWidth,
  176. int newHeight, [
  177. int? pixelWidth,
  178. int? pixelHeight,
  179. ]) {
  180. newWidth = max(newWidth, 1);
  181. newHeight = max(newHeight, 1);
  182. onResize?.call(newWidth, newHeight, pixelWidth ?? 0, pixelHeight ?? 0);
  183. //we need to resize both buffers so that they are ready when we switch between them
  184. _altBuffer.resize(_viewWidth, _viewHeight, newWidth, newHeight);
  185. _mainBuffer.resize(_viewWidth, _viewHeight, newWidth, newHeight);
  186. _viewWidth = newWidth;
  187. _viewHeight = newHeight;
  188. if (buffer == _altBuffer) {
  189. buffer.clearScrollback();
  190. }
  191. _altBuffer.resetVerticalMargins();
  192. _mainBuffer.resetVerticalMargins();
  193. }
  194. @override
  195. String toString() {
  196. return 'Terminal(#$hashCode, $_viewWidth x $_viewHeight, ${_buffer.height} lines)';
  197. }
  198. /* Handlers */
  199. @override
  200. void writeChar(int char) {
  201. _precedingCodepoint = char;
  202. _buffer.writeChar(char);
  203. }
  204. /* SBC */
  205. @override
  206. void bell() {
  207. onBell?.call();
  208. }
  209. @override
  210. void backspaceReturn() {
  211. _buffer.moveCursorX(-1);
  212. }
  213. @override
  214. void tab() {
  215. final nextStop = tabStops.find(_buffer.cursorX, _viewWidth);
  216. if (nextStop != null) {
  217. _buffer.setCursorX(nextStop);
  218. } else {
  219. _buffer.setCursorX(_viewWidth);
  220. _buffer.cursorGoForward(); // Enter pending-wrap state
  221. }
  222. }
  223. @override
  224. void lineFeed() {
  225. _buffer.lineFeed();
  226. }
  227. @override
  228. void carriageReturn() {
  229. _buffer.setCursorX(0);
  230. }
  231. @override
  232. void shiftOut() {
  233. _buffer.charset.use(1);
  234. }
  235. @override
  236. void shiftIn() {
  237. _buffer.charset.use(0);
  238. }
  239. @override
  240. void unknownSBC(int char) {
  241. // no-op
  242. }
  243. /* ANSI sequence */
  244. @override
  245. void saveCursor() {
  246. _buffer.saveCursor();
  247. }
  248. @override
  249. void restoreCursor() {
  250. _buffer.restoreCursor();
  251. }
  252. @override
  253. void index() {
  254. _buffer.index();
  255. }
  256. @override
  257. void nextLine() {
  258. _buffer.index();
  259. _buffer.setCursorX(0);
  260. }
  261. @override
  262. void setTapStop() {
  263. tabStops.isSetAt(_buffer.cursorX);
  264. }
  265. @override
  266. void reverseIndex() {
  267. _buffer.reverseIndex();
  268. }
  269. @override
  270. void designateCharset(int charset) {
  271. _buffer.charset.use(charset);
  272. }
  273. @override
  274. void unkownEscape(int char) {
  275. // no-op
  276. }
  277. /* CSI */
  278. @override
  279. void repeatPreviousCharacter(int count) {
  280. if (_precedingCodepoint == 0) {
  281. return;
  282. }
  283. for (var i = 0; i < count; i++) {
  284. _buffer.writeChar(_precedingCodepoint);
  285. }
  286. }
  287. @override
  288. void setCursor(int x, int y) {
  289. _buffer.setCursor(x, y);
  290. }
  291. @override
  292. void setCursorX(int x) {
  293. _buffer.setCursorX(x);
  294. }
  295. @override
  296. void setCursorY(int y) {
  297. _buffer.setCursorY(y);
  298. }
  299. @override
  300. void moveCursorX(int offset) {
  301. _buffer.moveCursorX(offset);
  302. }
  303. @override
  304. void moveCursorY(int n) {
  305. _buffer.moveCursorY(n);
  306. }
  307. @override
  308. void clearTabStopUnderCursor() {
  309. tabStops.clearAt(_buffer.cursorX);
  310. }
  311. @override
  312. void clearAllTabStops() {
  313. tabStops.clearAll();
  314. }
  315. @override
  316. void sendPrimaryDeviceAttributes() {
  317. onOutput?.call(_emitter.primaryDeviceAttributes());
  318. }
  319. @override
  320. void sendSecondaryDeviceAttributes() {
  321. onOutput?.call(_emitter.secondaryDeviceAttributes());
  322. }
  323. @override
  324. void sendTertiaryDeviceAttributes() {
  325. onOutput?.call(_emitter.tertiaryDeviceAttributes());
  326. }
  327. @override
  328. void sendOperatingStatus() {
  329. onOutput?.call(_emitter.operatingStatus());
  330. }
  331. @override
  332. void sendCursorPosition() {
  333. onOutput?.call(_emitter.cursorPosition(_buffer.cursorX, _buffer.cursorY));
  334. }
  335. @override
  336. void setMargins(int top, [int? bottom]) {
  337. _buffer.setVerticalMargins(top, bottom ?? viewHeight - 1);
  338. }
  339. @override
  340. void cursorNextLine(int amount) {
  341. _buffer.moveCursorY(amount);
  342. _buffer.setCursorX(0);
  343. }
  344. @override
  345. void cursorPrecedingLine(int amount) {
  346. _buffer.moveCursorY(-amount);
  347. _buffer.setCursorX(0);
  348. }
  349. @override
  350. void eraseDisplayBelow() {
  351. _buffer.eraseDisplayFromCursor();
  352. }
  353. @override
  354. void eraseDisplayAbove() {
  355. _buffer.eraseDisplayToCursor();
  356. }
  357. @override
  358. void eraseDisplay() {
  359. _buffer.eraseDisplay();
  360. }
  361. @override
  362. void eraseScrollbackOnly() {
  363. _buffer.clearScrollback();
  364. }
  365. @override
  366. void eraseLineRight() {
  367. _buffer.eraseLineFromCursor();
  368. }
  369. @override
  370. void eraseLineLeft() {
  371. _buffer.eraseLineToCursor();
  372. }
  373. @override
  374. void eraseLine() {
  375. _buffer.eraseLine();
  376. }
  377. @override
  378. void insertLines(int amount) {
  379. _buffer.insertLines(amount);
  380. }
  381. @override
  382. void deleteLines(int amount) {
  383. _buffer.deleteLines(amount);
  384. }
  385. @override
  386. void deleteChars(int amount) {
  387. _buffer.deleteChars(amount);
  388. }
  389. @override
  390. void scrollUp(int amount) {
  391. _buffer.scrollUp(amount);
  392. }
  393. @override
  394. void scrollDown(int amount) {
  395. _buffer.scrollDown(amount);
  396. }
  397. @override
  398. void eraseChars(int amount) {
  399. _buffer.eraseChars(amount);
  400. }
  401. @override
  402. void insertBlankChars(int amount) {
  403. _buffer.insertBlankChars(amount);
  404. }
  405. @override
  406. void unknownCSI(int finalByte) {
  407. // no-op
  408. }
  409. /* Modes */
  410. @override
  411. void setInsertMode(bool enabled) {
  412. _insertMode = enabled;
  413. }
  414. @override
  415. void setLineFeedMode(bool enabled) {
  416. _lineFeedMode = enabled;
  417. }
  418. @override
  419. void setUnknownMode(int mode, bool enabled) {
  420. // no-op
  421. }
  422. /* DEC Private modes */
  423. @override
  424. void setCursorKeysMode(bool enabled) {
  425. _cursorKeysMode = enabled;
  426. }
  427. @override
  428. void setReverseDisplayMode(bool enabled) {
  429. _reverseDisplayMode = enabled;
  430. }
  431. @override
  432. void setOriginMode(bool enabled) {
  433. _originMode = enabled;
  434. }
  435. @override
  436. void setColumnMode(bool enabled) {
  437. // no-op
  438. }
  439. @override
  440. void setAutoWrapMode(bool enabled) {
  441. _autoWrapMode = enabled;
  442. }
  443. @override
  444. void setMouseMode(MouseMode mode) {
  445. _mouseMode = mode;
  446. }
  447. @override
  448. void setCursorBlinkMode(bool enabled) {
  449. _cursorBlinkMode = enabled;
  450. }
  451. @override
  452. void setCursorVisibleMode(bool enabled) {
  453. _cursorVisibleMode = enabled;
  454. }
  455. @override
  456. void useAltBuffer() {
  457. _buffer = _altBuffer;
  458. }
  459. @override
  460. void useMainBuffer() {
  461. _buffer = _mainBuffer;
  462. }
  463. @override
  464. void clearAltBuffer() {
  465. _altBuffer.clear();
  466. }
  467. @override
  468. void setAppKeypadMode(bool enabled) {
  469. _appKeypadMode = enabled;
  470. }
  471. @override
  472. void setReportFocusMode(bool enabled) {
  473. _reportFocusMode = enabled;
  474. }
  475. @override
  476. void setMouseReportMode(MouseReportMode mode) {
  477. _mouseReportMode = mode;
  478. }
  479. @override
  480. void setAltBufferMouseScrollMode(bool enabled) {
  481. _altBufferMouseScrollMode = enabled;
  482. }
  483. @override
  484. void setBracketedPasteMode(bool enabled) {
  485. _bracketedPasteMode = enabled;
  486. }
  487. @override
  488. void setUnknownDecMode(int mode, bool enabled) {
  489. // no-op
  490. }
  491. /* Select Graphic Rendition (SGR) */
  492. @override
  493. void resetCursorStyle() {
  494. _cursorStyle.reset();
  495. }
  496. @override
  497. void setCursorBold() {
  498. _cursorStyle.setBold();
  499. }
  500. @override
  501. void setCursorFaint() {
  502. _cursorStyle.setFaint();
  503. }
  504. @override
  505. void setCursorItalic() {
  506. _cursorStyle.setItalic();
  507. }
  508. @override
  509. void setCursorUnderline() {
  510. _cursorStyle.setUnderline();
  511. }
  512. @override
  513. void setCursorBlink() {
  514. _cursorStyle.setBlink();
  515. }
  516. @override
  517. void setCursorInverse() {
  518. _cursorStyle.setInverse();
  519. }
  520. @override
  521. void setCursorInvisible() {
  522. _cursorStyle.setInvisible();
  523. }
  524. @override
  525. void setCursorStrikethrough() {
  526. _cursorStyle.setStrikethrough();
  527. }
  528. @override
  529. void unsetCursorBold() {
  530. _cursorStyle.unsetBold();
  531. }
  532. @override
  533. void unsetCursorFaint() {
  534. _cursorStyle.unsetFaint();
  535. }
  536. @override
  537. void unsetCursorItalic() {
  538. _cursorStyle.unsetItalic();
  539. }
  540. @override
  541. void unsetCursorUnderline() {
  542. _cursorStyle.unsetUnderline();
  543. }
  544. @override
  545. void unsetCursorBlink() {
  546. _cursorStyle.unsetBlink();
  547. }
  548. @override
  549. void unsetCursorInverse() {
  550. _cursorStyle.unsetInverse();
  551. }
  552. @override
  553. void unsetCursorInvisible() {
  554. _cursorStyle.unsetInvisible();
  555. }
  556. @override
  557. void unsetCursorStrikethrough() {
  558. _cursorStyle.unsetStrikethrough();
  559. }
  560. @override
  561. void setForegroundColor16(int color) {
  562. _cursorStyle.setForegroundColor16(color);
  563. }
  564. @override
  565. void setForegroundColor256(int index) {
  566. _cursorStyle.setForegroundColor256(index);
  567. }
  568. @override
  569. void setForegroundColorRgb(int r, int g, int b) {
  570. _cursorStyle.setForegroundColorRgb(r, g, b);
  571. }
  572. @override
  573. void resetForeground() {
  574. _cursorStyle.resetForegroundColor();
  575. }
  576. @override
  577. void setBackgroundColor16(int color) {
  578. _cursorStyle.setBackgroundColor16(color);
  579. }
  580. @override
  581. void setBackgroundColor256(int index) {
  582. _cursorStyle.setBackgroundColor256(index);
  583. }
  584. @override
  585. void setBackgroundColorRgb(int r, int g, int b) {
  586. _cursorStyle.setBackgroundColorRgb(r, g, b);
  587. }
  588. @override
  589. void resetBackground() {
  590. _cursorStyle.resetBackgroundColor();
  591. }
  592. @override
  593. void unsupportedStyle(int param) {
  594. // no-op
  595. }
  596. /* OSC */
  597. @override
  598. void setTitle(String name) {
  599. onTitleChange?.call(name);
  600. }
  601. @override
  602. void setIconName(String name) {
  603. onIconChange?.call(name);
  604. }
  605. @override
  606. void unknownOSC(String ps) {
  607. // no-op
  608. }
  609. }