terminal.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. String? character,
  349. }) {
  350. debug.onMsg(key);
  351. for (var record in keytab.records) {
  352. if (record.key != key) {
  353. continue;
  354. }
  355. if (record.ctrl != null && record.ctrl != ctrl) {
  356. continue;
  357. }
  358. if (record.shift != null && record.shift != shift) {
  359. continue;
  360. }
  361. if (record.alt != null && record.alt != alt) {
  362. continue;
  363. }
  364. if (record.anyModifier == true &&
  365. (ctrl != true && alt != true && shift != true)) {
  366. continue;
  367. }
  368. if (record.anyModifier == false &&
  369. !(ctrl != true && alt != true && shift != true)) {
  370. continue;
  371. }
  372. if (record.appScreen != null && record.appScreen != isUsingAltBuffer()) {
  373. continue;
  374. }
  375. if (record.newLine != null && record.newLine != newLineMode) {
  376. continue;
  377. }
  378. if (record.appCursorKeys != null &&
  379. record.appCursorKeys != applicationCursorKeys) {
  380. continue;
  381. }
  382. if (record.mac != null && record.mac != mac) {
  383. continue;
  384. }
  385. // TODO: support VT52
  386. if (record.ansi == false) {
  387. continue;
  388. }
  389. if (record.action.type == KeytabActionType.input) {
  390. debug.onMsg('input: ${record.action.value}');
  391. final input = keytabUnescape(record.action.value);
  392. backend?.write(input);
  393. return;
  394. }
  395. }
  396. if (ctrl) {
  397. if (key.index >= TerminalKey.keyA.index &&
  398. key.index <= TerminalKey.keyZ.index) {
  399. final input = key.index - TerminalKey.keyA.index + 1;
  400. backend?.write(String.fromCharCode(input));
  401. return;
  402. }
  403. }
  404. if (alt) {
  405. if (key.index >= TerminalKey.keyA.index &&
  406. key.index <= TerminalKey.keyZ.index) {
  407. final charCode = key.index - TerminalKey.keyA.index + 65;
  408. // only process ALT + Key when this combination has no other meaning
  409. // (reflected in the given character argument
  410. if (character == null ||
  411. character.toLowerCase() ==
  412. String.fromCharCode(charCode).toLowerCase()) {
  413. final input = [0x1b, charCode];
  414. backend?.write(String.fromCharCodes(input));
  415. }
  416. return;
  417. }
  418. }
  419. }
  420. void selectWordOrRow(Position position) {
  421. if (position.y > buffer.lines.length) {
  422. return;
  423. }
  424. final row = position.y;
  425. final line = buffer.lines[row];
  426. final positionIsInSelection = _selection.contains(position);
  427. final completeLineIsSelected =
  428. _selection.start?.x == 0 && _selection.end?.x == terminalWidth;
  429. if (positionIsInSelection && !completeLineIsSelected) {
  430. // select area on an already existing selection extends it to the full line
  431. _selection.clear();
  432. _selection.init(Position(0, row));
  433. _selection.update(Position(terminalWidth, row));
  434. } else {
  435. // select the word that is under position
  436. var start = position.x;
  437. var end = position.x;
  438. do {
  439. if (start == 0) {
  440. break;
  441. }
  442. final content = line.cellGetContent(start - 1);
  443. if (kWordSeparators.contains(String.fromCharCode(content))) {
  444. break;
  445. }
  446. start--;
  447. } while (true);
  448. do {
  449. if (end >= terminalWidth - 1) {
  450. break;
  451. }
  452. final content = line.cellGetContent(end + 1);
  453. if (kWordSeparators.contains(String.fromCharCode(content))) {
  454. break;
  455. }
  456. end++;
  457. } while (true);
  458. _selection.clear();
  459. _selection.init(Position(start, row));
  460. _selection.update(Position(end, row));
  461. refresh();
  462. }
  463. }
  464. String? getSelectedText() {
  465. if (_selection.isEmpty) {
  466. return null;
  467. }
  468. final builder = StringBuffer();
  469. for (var row = _selection.start!.y; row <= _selection.end!.y; row++) {
  470. if (row >= buffer.height) {
  471. break;
  472. }
  473. final line = buffer.lines[row];
  474. var xStart = 0;
  475. var xEnd = viewWidth - 1;
  476. if (row == _selection.start!.y) {
  477. xStart = _selection.start!.x;
  478. } else if (!line.isWrapped) {
  479. builder.write("\n");
  480. }
  481. if (row == _selection.end!.y) {
  482. xEnd = _selection.end!.x;
  483. }
  484. for (var col = xStart; col <= xEnd; col++) {
  485. // if (col >= line.length) {
  486. // break;
  487. // }
  488. if (line.cellGetWidth(col) == 0) {
  489. continue;
  490. }
  491. var char = line.cellGetContent(col);
  492. if (char == 0x00) {
  493. const blank = 32;
  494. char = blank;
  495. }
  496. builder.writeCharCode(char);
  497. }
  498. }
  499. return builder.toString();
  500. }
  501. @override
  502. void paste(String data) {
  503. if (bracketedPasteMode) {
  504. data = '\x1b[200~$data\x1b[201~';
  505. }
  506. backend?.write(data);
  507. }
  508. int get _tabIndexFromCursor {
  509. var index = buffer.cursorX;
  510. if (buffer.cursorX == viewWidth) {
  511. index = 0;
  512. }
  513. return index;
  514. }
  515. void tabSetAtCursor() {
  516. tabs.setAt(_tabIndexFromCursor);
  517. }
  518. void tabClearAtCursor() {
  519. tabs.clearAt(_tabIndexFromCursor);
  520. }
  521. void tab() {
  522. while (buffer.cursorX < viewWidth) {
  523. buffer.write(' ');
  524. if (tabs.isSetAt(buffer.cursorX)) {
  525. break;
  526. }
  527. }
  528. }
  529. @override
  530. int get backgroundColor => theme.background;
  531. @override
  532. int get bufferHeight => buffer.height;
  533. @override
  534. void clearSelection() {
  535. selection?.clear();
  536. }
  537. @override
  538. int convertViewLineToRawLine(int viewLine) {
  539. if (viewHeight > buffer.height) {
  540. return viewLine;
  541. }
  542. return viewLine + (buffer.height - viewHeight);
  543. }
  544. @override
  545. BufferLine? get currentLine => buffer.currentLine;
  546. @override
  547. int get cursorColor => theme.cursor;
  548. @override
  549. String? get selectedText => getSelectedText();
  550. @override
  551. bool get isReady => true;
  552. @override
  553. void onMouseTap(Position position) {
  554. mouseMode.onTap(this, position);
  555. }
  556. @override
  557. onMouseDoubleTap(Position position) {
  558. mouseMode.onDoubleTap(this, position);
  559. }
  560. @override
  561. void onPanStart(Position position) {
  562. mouseMode.onPanStart(this, position);
  563. }
  564. @override
  565. void onPanUpdate(Position position) {
  566. mouseMode.onPanUpdate(this, position);
  567. }
  568. @override
  569. int get scrollOffsetFromBottom => buffer.scrollOffsetFromBottom;
  570. @override
  571. int get scrollOffsetFromTop => buffer.scrollOffsetFromTop;
  572. @override
  573. int get terminalHeight => viewHeight;
  574. @override
  575. int get terminalWidth => viewWidth;
  576. @override
  577. Selection? get selection => _selection;
  578. @override
  579. void raiseOnInput(String input) {
  580. backend?.write(input);
  581. }
  582. final _backendExited = Completer<int>();
  583. @override
  584. Future<int> get backendExited => _backendExited.future;
  585. var _isTerminated = false;
  586. @override
  587. void terminateBackend() {
  588. if (_isTerminated) {
  589. return;
  590. }
  591. _isTerminated = true;
  592. backend?.terminate();
  593. }
  594. @override
  595. bool get isTerminated => _isTerminated;
  596. @override
  597. void selectAll() {
  598. _selection.init(Position(0, 0));
  599. _selection.update(Position(terminalWidth, bufferHeight));
  600. refresh();
  601. }
  602. String _composingString = '';
  603. @override
  604. String get composingString => _composingString;
  605. @override
  606. void updateComposingString(String value) {
  607. _composingString = value;
  608. refresh();
  609. }
  610. @override
  611. TerminalSearchResult get userSearchResult => _userSearchTask.searchResult;
  612. @override
  613. int get numberOfSearchHits => _userSearchTask.numberOfSearchHits;
  614. @override
  615. int? get currentSearchHit => _userSearchTask.currentSearchHit;
  616. @override
  617. set currentSearchHit(int? currentSearchHit) {
  618. _userSearchTask.currentSearchHit = currentSearchHit;
  619. _scrollCurrentHitIntoView();
  620. refresh();
  621. }
  622. @override
  623. TerminalSearchOptions get userSearchOptions => _userSearchTask.options;
  624. @override
  625. set userSearchOptions(TerminalSearchOptions options) {
  626. _userSearchTask.options = options;
  627. _scrollCurrentHitIntoView();
  628. refresh();
  629. }
  630. @override
  631. String? get userSearchPattern => _userSearchTask.pattern;
  632. @override
  633. set userSearchPattern(String? newValue) {
  634. _userSearchTask.pattern = newValue;
  635. _scrollCurrentHitIntoView();
  636. refresh();
  637. }
  638. @override
  639. bool get isUserSearchActive => _userSearchTask.isActive;
  640. @override
  641. set isUserSearchActive(bool isUserSearchActive) {
  642. _userSearchTask.isActive = isUserSearchActive;
  643. _scrollCurrentHitIntoView();
  644. refresh();
  645. }
  646. void _scrollCurrentHitIntoView() {
  647. if (!_userSearchTask.isActive) {
  648. return;
  649. }
  650. final currentHit = _userSearchTask.currentSearchHitObject;
  651. if (currentHit != null) {
  652. final desiredScrollOffsetFromTop =
  653. currentHit.startLineIndex + (terminalHeight / 2).floor();
  654. setScrollOffsetFromBottom(buffer.height - desiredScrollOffsetFromTop);
  655. }
  656. }
  657. }