terminal_search.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import 'package:equatable/equatable.dart';
  2. import 'package:xterm/buffer/line/line.dart';
  3. import 'package:xterm/terminal/terminal.dart';
  4. import 'package:xterm/util/constants.dart';
  5. class TerminalSearchResult {
  6. final _hitsByLine = Map<int, List<TerminalSearchHit>>();
  7. late final _allHits;
  8. int _currentSearchHit = 0;
  9. TerminalSearchResult.fromHits(List<TerminalSearchHit> hits) {
  10. _allHits = hits;
  11. for (final hit in hits) {
  12. if (!_hitsByLine.containsKey(hit.startLineIndex)) {
  13. _hitsByLine[hit.startLineIndex] =
  14. List<TerminalSearchHit>.empty(growable: true);
  15. }
  16. if (!_hitsByLine.containsKey(hit.endLineIndex)) {
  17. _hitsByLine[hit.endLineIndex] =
  18. List<TerminalSearchHit>.empty(growable: true);
  19. }
  20. _hitsByLine[hit.startLineIndex]!.add(hit);
  21. if (hit.startLineIndex != hit.endLineIndex) {
  22. _hitsByLine[hit.endLineIndex]!.add(hit);
  23. }
  24. }
  25. if (_allHits.length > 0) {
  26. _currentSearchHit = 1;
  27. } else {
  28. _currentSearchHit = 0;
  29. }
  30. }
  31. TerminalSearchResult.empty()
  32. : _allHits = List<TerminalSearchHit>.empty(growable: false);
  33. List<TerminalSearchHit> get allHits => _allHits;
  34. int get currentSearchHit => _currentSearchHit;
  35. void set currentSearchHit(int currentSearchHit) {
  36. if (_allHits.length <= 0) {
  37. _currentSearchHit = 0;
  38. } else {
  39. _currentSearchHit = currentSearchHit.clamp(1, _allHits.length).toInt();
  40. }
  41. }
  42. bool hasEntriesForLine(int line) {
  43. return _hitsByLine.containsKey(line);
  44. }
  45. List<TerminalSearchHit> getEntriesForLine(int line) {
  46. return _hitsByLine[line] ?? List<TerminalSearchHit>.empty(growable: false);
  47. }
  48. bool contains(int line, int col) {
  49. return _hitsByLine[line]?.any((hit) => hit.contains(line, col)) ?? false;
  50. }
  51. }
  52. class TerminalSearchHit {
  53. TerminalSearchHit(
  54. this.startLineIndex, this.startIndex, this.endLineIndex, this.endIndex);
  55. final int startLineIndex;
  56. final int startIndex;
  57. final int endLineIndex;
  58. final int endIndex;
  59. bool contains(int line, int col) {
  60. if (line < startLineIndex || line > endLineIndex) {
  61. return false;
  62. }
  63. if (line == startLineIndex && startLineIndex == endLineIndex) {
  64. return col >= startIndex && col < endIndex;
  65. }
  66. if (line == startLineIndex) {
  67. return col >= startIndex;
  68. }
  69. if (line == endLineIndex) {
  70. return col < endIndex;
  71. }
  72. // here we are sure that the given point is inside a full line match
  73. return true;
  74. }
  75. }
  76. class TerminalSearchOptions extends Equatable {
  77. TerminalSearchOptions({
  78. this.caseSensitive = false,
  79. this.matchWholeWord = false,
  80. this.useRegex = false,
  81. });
  82. final bool caseSensitive;
  83. final bool matchWholeWord;
  84. final bool useRegex;
  85. TerminalSearchOptions copyWith(
  86. {bool? caseSensitive, bool? matchWholeWord, bool? useRegex}) {
  87. return TerminalSearchOptions(
  88. caseSensitive: caseSensitive ?? this.caseSensitive,
  89. matchWholeWord: matchWholeWord ?? this.matchWholeWord,
  90. useRegex: useRegex ?? this.useRegex,
  91. );
  92. }
  93. @override
  94. bool get stringify => true;
  95. @override
  96. List<Object?> get props => [
  97. caseSensitive,
  98. matchWholeWord,
  99. useRegex,
  100. ];
  101. }
  102. class TerminalSearchTask {
  103. TerminalSearchTask(this._search, this._terminal, this._dirtyTagName,
  104. this._terminalSearchOptions);
  105. final TerminalSearch _search;
  106. final Terminal _terminal;
  107. String? _pattern = null;
  108. bool _isPatternDirty = true;
  109. RegExp? _searchRegexp = null;
  110. final String _dirtyTagName;
  111. TerminalSearchOptions _terminalSearchOptions;
  112. bool _isActive = false;
  113. bool get isActive => _isActive;
  114. void set isActive(bool isActive) {
  115. _isActive = isActive;
  116. if (isActive) {
  117. _invalidate();
  118. }
  119. }
  120. bool? _hasBeenUsingAltBuffer;
  121. TerminalSearchResult? _lastSearchResult = null;
  122. bool _isAnyLineDirty() {
  123. final bufferLength = _terminal.buffer.lines.length;
  124. for (var i = 0; i < bufferLength; i++) {
  125. if (_terminal.buffer.lines[i].isTagDirty(_dirtyTagName)) {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. void _markLinesForSearchDone() {
  132. final bufferLength = _terminal.buffer.lines.length;
  133. for (var i = 0; i < bufferLength; i++) {
  134. _terminal.buffer.lines[i].markTagAsNonDirty(_dirtyTagName);
  135. }
  136. }
  137. bool _isTerminalStateDirty() {
  138. if (_isAnyLineDirty()) {
  139. return true;
  140. }
  141. if (_hasBeenUsingAltBuffer != null &&
  142. _hasBeenUsingAltBuffer! != _terminal.isUsingAltBuffer()) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool get _isDirty {
  148. if (_isPatternDirty) {
  149. return true;
  150. }
  151. return _isTerminalStateDirty();
  152. }
  153. String? get pattern => _pattern;
  154. void set pattern(String? newPattern) {
  155. if (newPattern != _pattern) {
  156. _pattern = newPattern;
  157. _invalidate();
  158. }
  159. }
  160. TerminalSearchOptions get options => _terminalSearchOptions;
  161. void set options(TerminalSearchOptions newOptions) {
  162. if (_terminalSearchOptions == newOptions) {
  163. return;
  164. }
  165. _terminalSearchOptions = newOptions;
  166. _invalidate();
  167. }
  168. void _invalidate() {
  169. _isPatternDirty = true;
  170. _searchRegexp = null;
  171. }
  172. TerminalSearchResult get searchResult {
  173. if (_pattern == null || !_isActive) {
  174. return TerminalSearchResult.empty();
  175. }
  176. if (_lastSearchResult != null && !_isDirty) {
  177. return _lastSearchResult!;
  178. }
  179. final terminalWidth = _terminal.terminalWidth;
  180. if (_searchRegexp == null) {
  181. var pattern = _pattern!;
  182. if (!_terminalSearchOptions.useRegex) {
  183. pattern = RegExp.escape(_pattern!);
  184. }
  185. final regex = '(?<hit>$pattern)';
  186. _searchRegexp = RegExp(regex,
  187. caseSensitive: _terminalSearchOptions.caseSensitive,
  188. multiLine: false);
  189. }
  190. final hits = List<TerminalSearchHit>.empty(growable: true);
  191. for (final match
  192. in _searchRegexp!.allMatches(_search.terminalSearchString)) {
  193. final start = match.start;
  194. final end = match.end;
  195. final startLineIndex = (start / terminalWidth).floor();
  196. final endLineIndex = (end / terminalWidth).floor();
  197. // subtract the lines that got added in order to get the index inside the line
  198. final startIndex = start - startLineIndex * terminalWidth;
  199. final endIndex = end - endLineIndex * terminalWidth;
  200. if (_terminalSearchOptions.matchWholeWord) {
  201. // we match a whole word when the hit fulfills:
  202. // 1) starts at a line beginning or has a word-separator before it
  203. final startIsOK =
  204. startIndex == 0 || kWordSeparators.contains(match.input[start - 1]);
  205. // 2) ends with a line or has a word-separator after it
  206. final endIsOK = endIndex == terminalWidth ||
  207. kWordSeparators.contains(match.input[end]);
  208. if (!startIsOK || !endIsOK) {
  209. continue;
  210. }
  211. }
  212. hits.add(
  213. TerminalSearchHit(
  214. startLineIndex,
  215. startIndex,
  216. endLineIndex,
  217. endIndex,
  218. ),
  219. );
  220. }
  221. _markLinesForSearchDone();
  222. _isPatternDirty = false;
  223. _lastSearchResult = TerminalSearchResult.fromHits(hits);
  224. _hasBeenUsingAltBuffer = _terminal.isUsingAltBuffer();
  225. return _lastSearchResult!;
  226. }
  227. }
  228. class TerminalSearch {
  229. TerminalSearch(this._terminal);
  230. final Terminal _terminal;
  231. String? _cachedSearchString;
  232. int? _lastTerminalWidth;
  233. TerminalSearchTask createSearchTask(String dirtyTagName) {
  234. return TerminalSearchTask(
  235. this, _terminal, dirtyTagName, TerminalSearchOptions());
  236. }
  237. String get terminalSearchString {
  238. final bufferLength = _terminal.buffer.lines.length;
  239. final terminalWidth = _terminal.terminalWidth;
  240. var isAnySearchStringInvalid = false;
  241. for (var i = 0; i < bufferLength; i++) {
  242. if (!_terminal.buffer.lines[i].hasCachedSearchString) {
  243. isAnySearchStringInvalid = true;
  244. }
  245. }
  246. late String completeSearchString;
  247. if (_cachedSearchString != null &&
  248. _lastTerminalWidth != null &&
  249. _lastTerminalWidth! == terminalWidth &&
  250. !isAnySearchStringInvalid) {
  251. completeSearchString = _cachedSearchString!;
  252. } else {
  253. final bufferContent = StringBuffer();
  254. for (var i = 0; i < bufferLength; i++) {
  255. final BufferLine line = _terminal.buffer.lines[i];
  256. final searchString = line.toSearchString(terminalWidth);
  257. bufferContent.write(searchString);
  258. if (searchString.length < terminalWidth) {
  259. // fill up so that the row / col can be mapped back later on
  260. bufferContent.writeAll(
  261. List<String>.filled(terminalWidth - searchString.length, ' '));
  262. }
  263. }
  264. completeSearchString = bufferContent.toString();
  265. _cachedSearchString = completeSearchString;
  266. _lastTerminalWidth = terminalWidth;
  267. }
  268. return completeSearchString;
  269. }
  270. }