terminal.dart 14 KB

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