terminal.dart 20 KB

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