"""History object for use with prompt_toolkit."""importprompt_toolkit.historyfromxonsh.built_insimportXSH
[docs]classPromptToolkitHistory(prompt_toolkit.history.History):"""History class that implements the prompt-toolkit history interface with the xonsh backend. """def__init__(self,load_prev=True,*args,**kwargs):"""Initialize history object."""super().__init__()self.load_prev=load_prev
[docs]defload_history_strings(self):"""Loads synchronous history strings"""ifnotself.load_prev:returnhist=XSH.historyifhistisNone:returnforcmdinhist.all_items(newest_first=True):line=cmd["inp"].rstrip()strs=self.get_strings()iflen(strs)==0orline!=strs[-1]:yieldline
def_cust_history_matches(self,i):"""Custom history search method for prompt_toolkit that matches previous commands anywhere on a line, not just at the start. This gets monkeypatched into the prompt_toolkit prompter if ``XONSH_HISTORY_MATCH_ANYWHERE=True``"""return(self.history_search_textisNoneorself.history_search_textinself._working_lines[i])