terminal.dart 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. import 'dart:async';
  2. import 'dart:collection';
  3. import 'dart:math' show max, min;
  4. import 'package:xterm/buffer/buffer.dart';
  5. import 'package:xterm/buffer/line/line.dart';
  6. import 'package:xterm/input/keys.dart';
  7. import 'package:xterm/input/keytab/keytab.dart';
  8. import 'package:xterm/input/keytab/keytab_escape.dart';
  9. import 'package:xterm/input/keytab/keytab_record.dart';
  10. import 'package:xterm/mouse/mouse_mode.dart';
  11. import 'package:xterm/mouse/position.dart';
  12. import 'package:xterm/mouse/selection.dart';
  13. import 'package:xterm/terminal/ansi.dart';
  14. import 'package:xterm/terminal/cursor.dart';
  15. import 'package:xterm/terminal/platform.dart';
  16. import 'package:xterm/terminal/sbc.dart';
  17. import 'package:xterm/terminal/tabs.dart';
  18. import 'package:xterm/terminal/terminal_backend.dart';
  19. import 'package:xterm/terminal/terminal_search.dart';
  20. import 'package:xterm/terminal/terminal_search_interaction.dart';
  21. import 'package:xterm/terminal/terminal_ui_interaction.dart';
  22. import 'package:xterm/theme/terminal_color.dart';
  23. import 'package:xterm/theme/terminal_theme.dart';
  24. import 'package:xterm/theme/terminal_themes.dart';
  25. import 'package:xterm/util/constants.dart';
  26. import 'package:xterm/util/debug_handler.dart';
  27. import 'package:xterm/util/observable.dart';
  28. typedef TerminalInputHandler = void Function(String);
  29. typedef BellHandler = void Function();
  30. typedef TitleChangeHandler = void Function(String);
  31. typedef IconChangeHandler = void Function(String);
  32. void _defaultBellHandler() {}
  33. void _defaultTitleHandler(String _) {}
  34. void _defaultIconHandler(String _) {}
  35. class Terminal
  36. with Observable
  37. implements TerminalUiInteraction, TerminalSearchInteraction {
  38. Terminal({
  39. this.backend,
  40. this.onBell = _defaultBellHandler,
  41. this.onTitleChange = _defaultTitleHandler,
  42. this.onIconChange = _defaultIconHandler,
  43. this.platform = PlatformBehaviors.unix,
  44. TerminalTheme theme = TerminalThemes.defaultTheme,
  45. required int maxLines,
  46. }) : _theme = theme,
  47. _maxLines = maxLines {
  48. _search = TerminalSearch(this);
  49. _userSearchTask = _search.createSearchTask("UserSearch");
  50. backend?.init();
  51. backend?.exitCode.then((value) {
  52. _isTerminated = true;
  53. _backendExited.complete(value);
  54. });
  55. backend?.out.listen(write);
  56. _mainBuffer = Buffer(terminal: this, isAltBuffer: false);
  57. _altBuffer = Buffer(terminal: this, isAltBuffer: true);
  58. _buffer = _mainBuffer;
  59. cursor = Cursor(
  60. fg: theme.foreground,
  61. bg: TerminalColor.transparent, // transparent
  62. flags: 0x00, // no flags
  63. );
  64. tabs.reset();
  65. }
  66. late TerminalSearch _search;
  67. late TerminalSearchTask _userSearchTask;
  68. bool _dirty = false;
  69. @override
  70. bool get dirty {
  71. if (_dirty) {
  72. _dirty = false;
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. int _maxLines;
  79. int get maxLines {
  80. return max(viewHeight, _maxLines);
  81. }
  82. int _viewWidth = 80;
  83. int _viewHeight = 25;
  84. int get viewWidth => _viewWidth;
  85. int get viewHeight => _viewHeight;
  86. int get visibleHeight => min(_viewHeight, buffer.height);
  87. @override
  88. int get invisibleHeight => buffer.height - visibleHeight;
  89. /// ### Insert/Replace Mode (IRM)
  90. ///
  91. /// The terminal displays received characters at the cursor position.
  92. /// Insert/Replace mode determines how the terminal adds characters to the
  93. /// screen. Insert mode displays the new character and moves previously
  94. /// displayed characters to the right. Replace mode adds characters by
  95. /// replacing the character at the cursor position.
  96. ///
  97. /// You can set or reset insert/replace mode as follows.
  98. // ignore: unused_field
  99. bool _replaceMode = true;
  100. // ignore: unused_field
  101. bool _screenMode = false; // DECSCNM (black on white background)
  102. bool _autoWrapMode = true;
  103. bool get autoWrapMode => _autoWrapMode;
  104. /// ### DECOM – Origin Mode (DEC Private)
  105. ///
  106. /// This is a private parameter applicable to set mode (SM) and reset mode
  107. /// (RM) control sequences. The reset state causes the origin to be at the
  108. /// upper-left character position on the screen. Line and column numbers are,
  109. /// therefore, independent of current margin settings. The cursor may be
  110. /// positioned outside the margins with a cursor position (CUP) or horizontal
  111. /// and vertical position (HVP) control.
  112. ///
  113. /// The set state causes the origin to be at the upper-left character position
  114. /// within the margins. Line and column numbers are therefore relative to the
  115. /// current margin settings. The cursor is not allowed to be positioned
  116. /// outside the margins.
  117. ///
  118. /// The cursor is moved to the new home position when this mode is set or
  119. /// reset.
  120. ///
  121. /// Lines and columns are numbered consecutively, with the origin being line
  122. /// 1, column 1.
  123. bool get originMode => _originMode;
  124. bool _originMode = false;
  125. /// ### LNM – Line Feed/New Line Mode
  126. ///
  127. /// This is a parameter applicable to set mode (SM) and reset mode (RM)
  128. /// control sequences. The reset state causes the interpretation of the line
  129. /// feed (LF), defined in ANSI Standard X3.4-1977, to imply only vertical
  130. /// movement of the active position and causes the RETURN key (CR) to send the
  131. /// single code CR. The set state causes the LF to imply movement to the first
  132. /// position of the following line and causes the RETURN key to send the two
  133. /// codes (CR, LF). This is the New Line (NL) option.
  134. ///
  135. /// This mode does not affect the index (IND), or next line (NEL) format
  136. /// effectors.
  137. bool get lineFeedMode => _lineFeedMode;
  138. bool _lineFeedMode = true;
  139. /// See: [lineFeedMode]
  140. bool get newLineMode => !_lineFeedMode;
  141. /// ### Bracketed Paste Mode
  142. ///
  143. /// When bracketed paste mode is set, pasted text is bracketed with control
  144. /// sequences so that the program can differentiate pasted text from typed-in
  145. /// text. When bracketed paste mode is set, the program will receive: `ESC
  146. /// [200 ~`, followed by the pasted text, followed by `ESC [ 201 ~`.
  147. bool get bracketedPasteMode => _bracketedPasteMode;
  148. bool _bracketedPasteMode = false;
  149. bool _showCursor = true;
  150. @override
  151. bool get showCursor => _showCursor;
  152. /// DECCKM – Cursor Keys Mode (DEC Private)
  153. ///
  154. /// This is a private parameter applicable to set mode (SM) and reset mode
  155. /// (RM) control sequences. This mode is only effective when the terminal is
  156. /// in keypad application mode (see DECKPAM) and the ANSI/VT52 mode (DECANM)
  157. /// is set (see DECANM). Under these conditions, if the cursor key mode is
  158. /// reset, the four cursor function keys will send ANSI cursor control
  159. /// commands. If cursor key mode is set, the four cursor function keys will
  160. /// send application functions.
  161. bool get applicationCursorKeys => _applicationCursorKeys;
  162. bool _applicationCursorKeys = false;
  163. bool _blinkingCursor = true;
  164. bool get blinkingCursor => _blinkingCursor;
  165. late Buffer _buffer;
  166. late Buffer _mainBuffer;
  167. late Buffer _altBuffer;
  168. /// Queue of input characters. addLast() to add, removeFirst() to consume.
  169. final _queue = ListQueue<int>(81920);
  170. bool _slowMotion = false;
  171. bool get slowMotion => _slowMotion;
  172. MouseMode _mouseMode = MouseMode.none;
  173. MouseMode get mouseMode => _mouseMode;
  174. @override
  175. TerminalTheme get theme => _theme;
  176. TerminalTheme _theme;
  177. set theme(TerminalTheme value) {
  178. if (_theme != value) {
  179. if (cursor.fg == _theme.foreground) {
  180. cursor.fg = value.foreground;
  181. }
  182. _theme = value;
  183. refresh();
  184. }
  185. }
  186. // final cellAttr = CellAttrTemplate();
  187. late final Cursor cursor;
  188. final keytab = Keytab.defaultKeytab();
  189. final _selection = Selection();
  190. final tabs = Tabs();
  191. final debug = DebugHandler();
  192. final TerminalBackend? backend;
  193. final BellHandler onBell;
  194. final TitleChangeHandler onTitleChange;
  195. final IconChangeHandler onIconChange;
  196. @override
  197. final PlatformBehavior platform;
  198. Buffer get buffer {
  199. return _buffer;
  200. }
  201. @override
  202. int get cursorX => buffer.cursorX;
  203. @override
  204. int get cursorY => buffer.cursorY;
  205. @override
  206. void setScrollOffsetFromBottom(int scrollOffset) {
  207. final oldOffset = _buffer.scrollOffsetFromBottom;
  208. _buffer.setScrollOffsetFromBottom(scrollOffset);
  209. if (oldOffset != scrollOffset) {
  210. _dirty = true;
  211. refresh();
  212. }
  213. }
  214. /// Writes data to the terminal. Terminal sequences and special characters are
  215. /// interpreted.
  216. ///
  217. /// See also: [Buffer.write]
  218. @override
  219. void write(String text) {
  220. _queue.addAll(text.runes);
  221. _processInput();
  222. backend?.ackProcessed();
  223. refresh();
  224. }
  225. /// Writes data to the terminal. Special characters are interpreted.
  226. ///
  227. /// See also: [Buffer.writeChar]
  228. void writeChar(int codePoint) {
  229. _queue.addLast(codePoint);
  230. _processInput();
  231. refresh();
  232. }
  233. @override
  234. List<BufferLine> getVisibleLines() {
  235. return _buffer.getVisibleLines();
  236. }
  237. void _processInput() {
  238. while (_queue.isNotEmpty) {
  239. // if (_slowMotion) {
  240. // await Future.delayed(Duration(milliseconds: 100));
  241. // }
  242. const esc = 0x1b;
  243. final char = _queue.removeFirst();
  244. if (char == esc) {
  245. final finished = ansiHandler(_queue, this);
  246. // Terminal sequence in the queue is not completed, and no charater is
  247. // consumed.
  248. if (!finished) {
  249. _queue.addFirst(esc);
  250. break;
  251. }
  252. continue;
  253. }
  254. _processChar(char);
  255. }
  256. }
  257. void _processChar(int codePoint) {
  258. // If the character doesn't have special effect. Write it directly to the
  259. // buffer.
  260. if (codePoint > sbcMaxCodePoint) {
  261. debug.onChar(codePoint);
  262. _buffer.writeChar(codePoint);
  263. return;
  264. }
  265. // The character may have special effect.
  266. final sbcHandler = sbcHandlers[codePoint];
  267. if (sbcHandler != null) {
  268. debug.onSbc(codePoint);
  269. sbcHandler(codePoint, this);
  270. }
  271. }
  272. @override
  273. void refresh() {
  274. _dirty = true;
  275. notifyListeners();
  276. }
  277. void setSlowMotion(bool enabled) {
  278. _slowMotion = enabled;
  279. }
  280. void setOriginMode(bool enabled) {
  281. _originMode = enabled;
  282. buffer.setPosition(0, 0);
  283. }
  284. void setScreenMode(bool enabled) {
  285. _screenMode = true;
  286. }
  287. void setApplicationCursorKeys(bool enabled) {
  288. _applicationCursorKeys = enabled;
  289. }
  290. void setShowCursor(bool showCursor) {
  291. _showCursor = showCursor;
  292. }
  293. void setBlinkingCursor(bool enabled) {
  294. _blinkingCursor = enabled;
  295. }
  296. void setAutoWrapMode(bool enabled) {
  297. _autoWrapMode = enabled;
  298. }
  299. void setBracketedPasteMode(bool enabled) {
  300. _bracketedPasteMode = enabled;
  301. }
  302. void setInsertMode() {
  303. _replaceMode = false;
  304. }
  305. void setReplaceMode() {
  306. _replaceMode = true;
  307. }
  308. void setNewLineMode() {
  309. _lineFeedMode = false;
  310. }
  311. void setLineFeedMode() {
  312. _lineFeedMode = true;
  313. }
  314. void setMouseMode(MouseMode mode) {
  315. _mouseMode = mode;
  316. }
  317. void useMainBuffer() {
  318. _buffer = _mainBuffer;
  319. }
  320. void useAltBuffer() {
  321. _buffer = _altBuffer;
  322. }
  323. bool isUsingMainBuffer() {
  324. return _buffer == _mainBuffer;
  325. }
  326. bool isUsingAltBuffer() {
  327. return _buffer == _altBuffer;
  328. }
  329. /// Resize the terminal screen. [newWidth] and [newHeight] should be greater
  330. /// than 0. Text reflow is currently not implemented and will be avaliable in
  331. /// the future.
  332. @override
  333. void resize(
  334. int newWidth, int newHeight, int newPixelWidth, int newPixelHeight) {
  335. backend?.resize(newWidth, newHeight, newPixelWidth, newPixelHeight);
  336. newWidth = max(newWidth, 1);
  337. newHeight = max(newHeight, 1);
  338. final oldWidth = _viewWidth;
  339. final oldHeight = _viewHeight;
  340. _viewWidth = newWidth;
  341. _viewHeight = newHeight;
  342. //we need to resize both buffers so that they are ready when we switch between them
  343. _altBuffer.resize(oldWidth, oldHeight, newWidth, newHeight);
  344. _mainBuffer.resize(oldWidth, oldHeight, newWidth, newHeight);
  345. if (buffer == _altBuffer) {
  346. buffer.clearScrollback();
  347. }
  348. _altBuffer.resetVerticalMargins();
  349. _mainBuffer.resetVerticalMargins();
  350. }
  351. @override
  352. void keyInput(
  353. TerminalKey key, {
  354. bool ctrl = false,
  355. bool alt = false,
  356. bool shift = false,
  357. bool mac = false,
  358. // bool meta,
  359. String? character,
  360. }) {
  361. debug.onMsg(key);
  362. for (var record in keytab.records) {
  363. if (record.key != key) {
  364. continue;
  365. }
  366. if (record.ctrl != null && record.ctrl != ctrl) {
  367. continue;
  368. }
  369. if (record.shift != null && record.shift != shift) {
  370. continue;
  371. }
  372. if (record.alt != null && record.alt != alt) {
  373. continue;
  374. }
  375. if (record.anyModifier == true &&
  376. (ctrl != true && alt != true && shift != true)) {
  377. continue;
  378. }
  379. if (record.anyModifier == false &&
  380. !(ctrl != true && alt != true && shift != true)) {
  381. continue;
  382. }
  383. if (record.appScreen != null && record.appScreen != isUsingAltBuffer()) {
  384. continue;
  385. }
  386. if (record.newLine != null && record.newLine != newLineMode) {
  387. continue;
  388. }
  389. if (record.appCursorKeys != null &&
  390. record.appCursorKeys != applicationCursorKeys) {
  391. continue;
  392. }
  393. if (record.mac != null && record.mac != mac) {
  394. continue;
  395. }
  396. // TODO: support VT52
  397. if (record.ansi == false) {
  398. continue;
  399. }
  400. if (record.action.type == KeytabActionType.input) {
  401. debug.onMsg('input: ${record.action.value}');
  402. final input = keytabUnescape(record.action.value);
  403. backend?.write(input);
  404. return;
  405. }
  406. }
  407. if (ctrl) {
  408. if (key.index >= TerminalKey.keyA.index &&
  409. key.index <= TerminalKey.keyZ.index) {
  410. final input = key.index - TerminalKey.keyA.index + 1;
  411. backend?.write(String.fromCharCode(input));
  412. return;
  413. }
  414. }
  415. if (alt) {
  416. if (key.index >= TerminalKey.keyA.index &&
  417. key.index <= TerminalKey.keyZ.index) {
  418. final charCode = key.index - TerminalKey.keyA.index + 65;
  419. // only process ALT + Key when this combination has no other meaning
  420. // (reflected in the given character argument
  421. if (character == null ||
  422. character.toLowerCase() ==
  423. String.fromCharCode(charCode).toLowerCase()) {
  424. final input = [0x1b, charCode];
  425. backend?.write(String.fromCharCodes(input));
  426. }
  427. return;
  428. }
  429. }
  430. }
  431. void selectWordOrRow(Position position) {
  432. if (position.y > buffer.lines.length) {
  433. return;
  434. }
  435. final row = position.y;
  436. final line = buffer.lines[row];
  437. final positionIsInSelection = _selection.contains(position);
  438. final completeLineIsSelected =
  439. _selection.start?.x == 0 && _selection.end?.x == terminalWidth;
  440. if (positionIsInSelection && !completeLineIsSelected) {
  441. // select area on an already existing selection extends it to the full line
  442. _selection.clear();
  443. _selection.init(Position(0, row));
  444. _selection.update(Position(terminalWidth, row));
  445. } else {
  446. // select the word that is under position
  447. var start = position.x;
  448. var end = position.x;
  449. do {
  450. if (start == 0) {
  451. break;
  452. }
  453. final content = line.cellGetContent(start - 1);
  454. if (kWordSeparators.contains(String.fromCharCode(content))) {
  455. break;
  456. }
  457. start--;
  458. } while (true);
  459. do {
  460. if (end >= terminalWidth - 1) {
  461. break;
  462. }
  463. final content = line.cellGetContent(end + 1);
  464. if (kWordSeparators.contains(String.fromCharCode(content))) {
  465. break;
  466. }
  467. end++;
  468. } while (true);
  469. _selection.clear();
  470. _selection.init(Position(start, row));
  471. _selection.update(Position(end, row));
  472. refresh();
  473. }
  474. }
  475. String? getSelectedText() {
  476. if (_selection.isEmpty) {
  477. return null;
  478. }
  479. final builder = StringBuffer();
  480. for (var row = _selection.start!.y; row <= _selection.end!.y; row++) {
  481. if (row >= buffer.height) {
  482. break;
  483. }
  484. final line = buffer.lines[row];
  485. var xStart = 0;
  486. var xEnd = viewWidth - 1;
  487. if (row == _selection.start!.y) {
  488. xStart = _selection.start!.x;
  489. } else if (!line.isWrapped) {
  490. builder.write("\n");
  491. }
  492. if (row == _selection.end!.y) {
  493. xEnd = _selection.end!.x;
  494. }
  495. for (var col = xStart; col <= xEnd; col++) {
  496. // if (col >= line.length) {
  497. // break;
  498. // }
  499. if (line.cellGetWidth(col) == 0) {
  500. continue;
  501. }
  502. var char = line.cellGetContent(col);
  503. if (char == 0x00) {
  504. const blank = 32;
  505. char = blank;
  506. }
  507. builder.writeCharCode(char);
  508. }
  509. }
  510. return builder.toString();
  511. }
  512. @override
  513. void paste(String data) {
  514. if (bracketedPasteMode) {
  515. data = '\x1b[200~$data\x1b[201~';
  516. }
  517. backend?.write(data);
  518. }
  519. int get _tabIndexFromCursor {
  520. var index = buffer.cursorX;
  521. if (buffer.cursorX == viewWidth) {
  522. index = 0;
  523. }
  524. return index;
  525. }
  526. void tabSetAtCursor() {
  527. tabs.setAt(_tabIndexFromCursor);
  528. }
  529. void tabClearAtCursor() {
  530. tabs.clearAt(_tabIndexFromCursor);
  531. }
  532. void tab() {
  533. while (buffer.cursorX < viewWidth) {
  534. buffer.write(' ');
  535. if (tabs.isSetAt(buffer.cursorX)) {
  536. break;
  537. }
  538. }
  539. }
  540. @override
  541. int get backgroundColor => theme.background;
  542. @override
  543. int get bufferHeight => buffer.height;
  544. @override
  545. void clearSelection() {
  546. selection?.clear();
  547. }
  548. @override
  549. int convertViewLineToRawLine(int viewLine) {
  550. if (viewHeight > buffer.height) {
  551. return viewLine;
  552. }
  553. return viewLine + (buffer.height - viewHeight);
  554. }
  555. @override
  556. BufferLine? get currentLine => buffer.currentLine;
  557. @override
  558. int get cursorColor => theme.cursor;
  559. @override
  560. String? get selectedText => getSelectedText();
  561. @override
  562. bool get isReady => backend?.isReady ?? true;
  563. @override
  564. void onMouseTap(Position position) {
  565. mouseMode.onTap(this, position);
  566. }
  567. @override
  568. onMouseDoubleTap(Position position) {
  569. mouseMode.onDoubleTap(this, position);
  570. }
  571. @override
  572. void onPanStart(Position position) {
  573. mouseMode.onPanStart(this, position);
  574. }
  575. @override
  576. void onPanUpdate(Position position) {
  577. mouseMode.onPanUpdate(this, position);
  578. }
  579. @override
  580. int get scrollOffsetFromBottom => buffer.scrollOffsetFromBottom;
  581. @override
  582. int get scrollOffsetFromTop => buffer.scrollOffsetFromTop;
  583. @override
  584. int get terminalHeight => viewHeight;
  585. @override
  586. int get terminalWidth => viewWidth;
  587. @override
  588. Selection? get selection => _selection;
  589. @override
  590. void raiseOnInput(String input) {
  591. backend?.write(input);
  592. }
  593. final _backendExited = Completer<int>();
  594. @override
  595. Future<int> get backendExited => _backendExited.future;
  596. var _isTerminated = false;
  597. @override
  598. void terminateBackend() {
  599. if (_isTerminated) {
  600. return;
  601. }
  602. _isTerminated = true;
  603. backend?.terminate();
  604. }
  605. @override
  606. bool get isTerminated => _isTerminated;
  607. @override
  608. void selectAll() {
  609. _selection.init(Position(0, 0));
  610. _selection.update(Position(terminalWidth, bufferHeight));
  611. refresh();
  612. }
  613. String _composingString = '';
  614. @override
  615. String get composingString => _composingString;
  616. @override
  617. void updateComposingString(String value) {
  618. _composingString = value;
  619. refresh();
  620. }
  621. @override
  622. TerminalSearchResult get userSearchResult => _userSearchTask.searchResult;
  623. @override
  624. int get numberOfSearchHits => _userSearchTask.numberOfSearchHits;
  625. @override
  626. int? get currentSearchHit => _userSearchTask.currentSearchHit;
  627. @override
  628. set currentSearchHit(int? currentSearchHit) {
  629. _userSearchTask.currentSearchHit = currentSearchHit;
  630. _scrollCurrentHitIntoView();
  631. refresh();
  632. }
  633. @override
  634. TerminalSearchOptions get userSearchOptions => _userSearchTask.options;
  635. @override
  636. set userSearchOptions(TerminalSearchOptions options) {
  637. _userSearchTask.options = options;
  638. _scrollCurrentHitIntoView();
  639. refresh();
  640. }
  641. @override
  642. String? get userSearchPattern => _userSearchTask.pattern;
  643. @override
  644. set userSearchPattern(String? newValue) {
  645. _userSearchTask.pattern = newValue;
  646. _scrollCurrentHitIntoView();
  647. refresh();
  648. }
  649. @override
  650. bool get isUserSearchActive => _userSearchTask.isActive;
  651. @override
  652. set isUserSearchActive(bool isUserSearchActive) {
  653. _userSearchTask.isActive = isUserSearchActive;
  654. _scrollCurrentHitIntoView();
  655. refresh();
  656. }
  657. void _scrollCurrentHitIntoView() {
  658. if (!_userSearchTask.isActive) {
  659. return;
  660. }
  661. final currentHit = _userSearchTask.currentSearchHitObject;
  662. if (currentHit != null) {
  663. final desiredScrollOffsetFromTop =
  664. currentHit.startLineIndex + (terminalHeight / 2).floor();
  665. setScrollOffsetFromBottom(buffer.height - desiredScrollOffsetFromTop);
  666. }
  667. }
  668. }