MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / str_search

Method str_search

cmd2/history.py:275–292  ·  view source on GitHub ↗

Find history items which contain a given string. :param search: the string 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 ascending order,

(self, search: str, include_persisted: bool = False)

Source from the content-addressed store, hash-verified

273 return self._build_result_dictionary(start, end)
274
275 def str_search(self, search: str, include_persisted: bool = False) -> dict[int, "HistoryItem"]:
276 """Find history items which contain a given string.
277
278 :param search: the string to search for
279 :param include_persisted: if True, then search full history including persisted history
280 :return: a dictionary of history items keyed by their 1-based index in ascending order,
281 or an empty dictionary if the string was not found
282 """
283
284 def isin(history_item: HistoryItem) -> bool:
285 """Filter function for string search of history."""
286 sloppy = su.norm_fold(search)
287 inraw = sloppy in su.norm_fold(history_item.raw)
288 inexpanded = sloppy in su.norm_fold(history_item.expanded)
289 return inraw or inexpanded
290
291 start = 0 if include_persisted else self.session_start_index
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.

Callers 2

_get_historyMethod · 0.80
test_history_str_searchFunction · 0.80

Calls 1

Tested by 1

test_history_str_searchFunction · 0.64