terminal.dart 20 KB

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