home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import re
- import operator
- import string
- import sys
- import os
- from pyreadline.unicode_helper import ensure_unicode, ensure_str
- if 'pyreadline' in sys.modules:
- pyreadline = sys.modules['pyreadline']
- else:
- import pyreadline
- import lineobj
- import exceptions
-
- class EscapeHistory(exceptions.Exception):
- pass
-
- from pyreadline.logger import log_sock
- _ignore_leading_spaces = False
-
- class LineHistory(object):
-
- def __init__(self):
- self.history = []
- self._history_length = 100
- self._history_cursor = 0
- self.history_filename = os.path.expanduser('~/.history')
- self.lastcommand = None
- self.query = ''
-
-
- def get_history_length(self):
- value = self._history_length
- log_sock('get_history_length:%d' % value, 'history')
- return value
-
-
- def set_history_length(self, value):
- log_sock('set_history_length: old:%d new:%d' % (self._history_length, value), 'history')
- self._history_length = value
-
-
- def get_history_cursor(self):
- value = self._history_cursor
- log_sock('get_history_cursor:%d' % value, 'history')
- return value
-
-
- def set_history_cursor(self, value):
- log_sock('set_history_cursor: old:%d new:%d' % (self._history_cursor, value), 'history')
- self._history_cursor = value
-
- history_length = property(get_history_length, set_history_length)
- history_cursor = property(get_history_cursor, set_history_cursor)
-
- def clear_history(self):
- self.history[:] = []
- self.history_cursor = 0
-
-
- def read_history_file(self, filename = None):
- 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
-
-
-
- def write_history_file(self, filename = None):
- if filename is None:
- filename = self.history_filename
-
- fp = open(filename, 'wb')
- for line in self.history[-(self.history_length):]:
- fp.write(ensure_str(line.get_line_text()))
- fp.write('\n')
-
- fp.close()
-
-
- def add_history(self, 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)
-
-
- def previous_history(self, current):
- if self.history_cursor == len(self.history):
- self.history.append(current.copy())
-
- if self.history_cursor > 0:
- self.history_cursor -= 1
- current.set_line(self.history[self.history_cursor].get_line_text())
- current.point = lineobj.EndOfLine
-
-
-
- def next_history(self, current):
- if self.history_cursor < len(self.history) - 1:
- self.history_cursor += 1
- current.set_line(self.history[self.history_cursor].get_line_text())
-
-
-
- def beginning_of_history(self):
- self.history_cursor = 0
- if len(self.history) > 0:
- self.l_buffer = self.history[0]
-
-
-
- def end_of_history(self, current):
- self.history_cursor = len(self.history)
- current.set_line(self.history[-1].get_line_text())
-
-
- def reverse_search_history(self, searchfor, startpos = None):
- if startpos is None:
- startpos = self.history_cursor
-
- if res:
- self.history_cursor -= res[0][0]
- return res[0][1].get_line_text()
- return ''
-
-
- def forward_search_history(self, searchfor, startpos = None):
- if startpos is None:
- startpos = self.history_cursor
-
- if res:
- self.history_cursor += res[0][0]
- return res[0][1].get_line_text()
- return ''
-
-
- def _non_i_search(self, direction, current):
- c = pyreadline.rl.console
- line = current.get_line_text()
- query = ''
- while None:
- 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
- len(query) > 0
- if event.char in string.letters + string.digits + string.punctuation + ' ':
- query += event.char
- continue
- if event.keyinfo.keyname == 'return':
- break
- continue
- pyreadline.rl._bell()
- continue
- 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)
-
-
- def non_incremental_reverse_search_history(self, current):
- return self._non_i_search(-1, current)
-
-
- def non_incremental_forward_search_history(self, current):
- return self._non_i_search(1, current)
-
-
- 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 or 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
- if 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
- continue
- h != partial.get_line_text()
- 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)
- if 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)
- return lineobj.ReadLineTextBuffer(partial, point = partial.point)
- return lineobj.ReadLineTextBuffer(self.query, point = min(len(self.query), partial.point))
- except IndexError:
- raise
-
-
-
- def history_search_forward(self, partial):
- q = self._search(1, partial)
- return q
-
-
- def history_search_backward(self, partial):
- q = self._search(-1, partial)
- return q
-
-
- if __name__ == '__main__':
- q = LineHistory()
- RL = lineobj.ReadLineTextBuffer
- q.add_history(RL('aaaa'))
- q.add_history(RL('aaba'))
- q.add_history(RL('aaca'))
- q.add_history(RL('akca'))
- q.add_history(RL('bbb'))
- q.add_history(RL('ako'))
-
-