Пример #1
0
    def forward_search_history(self, searchfor, startpos=None):
        if startpos is None:
            startpos = min(self.history_cursor,
                           max(0,
                               self.get_current_history_length() - 1))
        origpos = startpos

        result = lineobj.ReadLineTextBuffer("")

        for idx, line in list(enumerate(self.history))[startpos:]:
            if searchfor in line:
                startpos = idx
                break

        #If we get a new search without change in search term it means
        #someone pushed ctrl-r and we should find the next match
        if self.last_search_for == searchfor and startpos < self.get_current_history_length(
        ) - 1:
            startpos += 1
            for idx, line in list(enumerate(self.history))[startpos:]:
                if searchfor in line:
                    startpos = idx
                    break

        if self.history:
            result = self.history[startpos].get_line_text()
        else:
            result = u""
        self.history_cursor = startpos
        self.last_search_for = searchfor
        return result
Пример #2
0
    def _non_i_search(self, direction, current):
        c = pyreadline.rl.console
        line = current.get_line_text()
        query = ''
        while 1:
            c.pos(*pyreadline.rl.prompt_end_pos)
            scroll = c.write_scrolling(":%s" % query)
            pyreadline.rl._update_prompt_pos(scroll)
            pyreadline.rl._clear_after()

            event = c.getkeypress()

            if event.keyinfo.keyname == 'backspace':
                if len(query) > 0:
                    query = query[:-1]
                else:
                    break
            elif event.char in string.letters + string.digits + string.punctuation + ' ':
                query += event.char
            elif event.keyinfo.keyname == 'return':
                break
            else:
                pyreadline.rl._bell()
        res = ""
        if query:
            if direction == -1:
                res = self.reverse_search_history(query)

            else:
                res = self.forward_search_history(query)
        return lineobj.ReadLineTextBuffer(res, point=0)
Пример #3
0
    def reverse_search_history(self, searchfor, startpos=None):
        if startpos is None:
            startpos = self.history_cursor
        origpos = startpos

        result = lineobj.ReadLineTextBuffer("")

        for idx, line in list(enumerate(self.history))[startpos:0:-1]:
            if searchfor in line:
                startpos = idx
                break

        #If we get a new search without change in search term it means
        #someone pushed ctrl-r and we should find the next match
        if self.last_search_for == searchfor and startpos > 0:
            startpos -= 1
            for idx, line in list(enumerate(self.history))[startpos:0:-1]:
                if searchfor in line:
                    startpos = idx
                    break

        if self.history:
            result = self.history[startpos].get_line_text()
        else:
            result = u""
        self.history_cursor = startpos
        self.last_search_for = searchfor
        log(u"reverse_search_history: old:%d new:%d result:%r" %
            (origpos, self.history_cursor, result))
        return result
Пример #4
0
 def read_history_file(self, filename=None):
     '''Load a readline history file.'''
     if filename is None:
         filename = self.history_filename
     try:
         for line in open(filename, 'r'):
             self.add_history(lineobj.ReadLineTextBuffer(line.rstrip()))
     except IOError:
         self.history = []
         self.history_cursor = 0
Пример #5
0
 def read_history_file(self, filename=None):
     """Load a readline history file."""
     if filename is None:
         filename = self.history_filename
     try:
         for line in open(filename, "r"):
             self.add_history(
                 lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip())))
     except IOError:
         self.history = []
         self.history_cursor = 0
Пример #6
0
 def add_history(self, line):
     u'''Append a line to the history buffer, as if it was the last line typed.'''
     if not hasattr(line, "get_line_text"):
         line = lineobj.ReadLineTextBuffer(line)
     if not line.get_line_text():
         pass
     elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text():
         pass
     else:
         self.history.append(line)
     self.history_cursor = len(self.history)
Пример #7
0
 def _search(self, direction, partial):
     try:
         if (self.lastcommand != self.history_search_forward and
                 self.lastcommand != self.history_search_backward):
             self.query = ''.join(partial[0:partial.point].get_line_text())
         hcstart = max(self.history_cursor, 0)
         hc = self.history_cursor + direction
         while (direction < 0 and hc >= 0) or (direction > 0 and hc < len(self.history)):
             h = self.history[hc]
             if not self.query:
                 self.history_cursor = hc
                 result = lineobj.ReadLineTextBuffer(h, point=len(h.get_line_text()))
                 return result
             elif (h.get_line_text().startswith(self.query) and (h != partial.get_line_text())):
                 self.history_cursor = hc
                 result = lineobj.ReadLineTextBuffer(h, point=partial.point)
                 return result
             hc += direction
         else:
             if len(self.history) == 0:
                 pass
             elif hc >= len(self.history) and not self.query:
                 self.history_cursor = len(self.history)
                 return lineobj.ReadLineTextBuffer("", point=0)
             elif self.history[max(min(hcstart, len(self.history) - 1), 0)].get_line_text().startswith(
                     self.query) and self.query:
                 return lineobj.ReadLineTextBuffer(self.history[max(min(hcstart, len(self.history) - 1), 0)],
                                                   point=partial.point)
             else:
                 return lineobj.ReadLineTextBuffer(partial, point=partial.point)
             return lineobj.ReadLineTextBuffer(self.query, point=min(len(self.query), partial.point))
     except IndexError:
         raise