Find history items which match a given regular expression. :param regex: the regular expression to search for. :param include_persisted: if True, then search full history including persisted history :return: a dictionary of history items keyed by their 1-based index in ascen
(self, regex: str, include_persisted: bool = False)
| 292 | return self._build_result_dictionary(start, len(self), isin) |
| 293 | |
| 294 | def regex_search(self, regex: str, include_persisted: bool = False) -> dict[int, "HistoryItem"]: |
| 295 | """Find history items which match a given regular expression. |
| 296 | |
| 297 | :param regex: the regular expression to search for. |
| 298 | :param include_persisted: if True, then search full history including persisted history |
| 299 | :return: a dictionary of history items keyed by their 1-based index in ascending order, |
| 300 | or an empty dictionary if the regex was not matched |
| 301 | """ |
| 302 | regex = regex.strip() |
| 303 | if regex.startswith(r"/") and regex.endswith(r"/"): |
| 304 | regex = regex[1:-1] |
| 305 | finder = re.compile(regex, re.DOTALL | re.MULTILINE) |
| 306 | |
| 307 | def isin(hi: HistoryItem) -> bool: |
| 308 | """Filter function for doing a regular expression search of history.""" |
| 309 | return bool(finder.search(hi.raw) or finder.search(hi.expanded)) |
| 310 | |
| 311 | start = 0 if include_persisted else self.session_start_index |
| 312 | return self._build_result_dictionary(start, len(self), isin) |
| 313 | |
| 314 | def truncate(self, max_length: int) -> None: |
| 315 | """Truncate the length of the history, dropping the oldest items if necessary. |