terminal.dart 15 KB

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