From bad7ebf0a3a8d2efb0c99748832ea8f90a5b9fd8 Mon Sep 17 00:00:00 2001 From: Jorgen Stenarson Date: Thu, 16 Sep 2010 20:41:57 +0200 Subject: [PATCH] Fixing readline api problems Bug #637506 --- pyreadline/__init__.py | 2 ++ pyreadline/lineeditor/history.py | 16 ++++++++++++++++ pyreadline/rlmain.py | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/pyreadline/__init__.py b/pyreadline/__init__.py index 9e265f0..a50dc7f 100644 --- a/pyreadline/__init__.py +++ b/pyreadline/__init__.py @@ -16,7 +16,9 @@ __all__ = [ 'parse_and_bind', 'read_init_file', 'read_history_file', 'write_history_file', + 'get_current_history_length', 'get_history_length', + 'get_history_item', 'set_history_length', 'set_startup_hook', 'set_pre_input_hook', diff --git a/pyreadline/lineeditor/history.py b/pyreadline/lineeditor/history.py index 0ee9d71..b4a0e84 100644 --- a/pyreadline/lineeditor/history.py +++ b/pyreadline/lineeditor/history.py @@ -33,11 +33,27 @@ class LineHistory(object): self.lastcommand = None self.query = u"" + def get_current_history_length(self): + u'''Return the number of lines currently in the history. + (This is different from get_history_length(), which returns + the maximum number of lines that will be written to a history file.)''' + value = len(self.history) + log(u"get_current_history_length:%d"%value) + return value + def get_history_length(self): + u'''Return the desired length of the history file. Negative values imply + unlimited history file size.''' value = self._history_length log(u"get_history_length:%d"%value) return value + def get_history_item(self, index): + u'''Return the current contents of history item at index.''' + item = self.history[index] + log(u"get_history_item: index:%d item:%r"%(index, item)) + return item.get_line_text() + def set_history_length(self, value): log(u"set_history_length: old:%d new:%d"%(self._history_length, value)) self._history_length = value diff --git a/pyreadline/rlmain.py b/pyreadline/rlmain.py index 9e3756e..36f77cd 100644 --- a/pyreadline/rlmain.py +++ b/pyreadline/rlmain.py @@ -128,6 +128,12 @@ class BaseReadline(object): u'''Append a line to the history buffer, as if it was the last line typed.''' self.mode._history.add_history(line) + def get_current_history_length(self ): + u'''Return the number of lines currently in the history. + (This is different from get_history_length(), which returns + the maximum number of lines that will be written to a history file.)''' + return self.mode._history.get_current_history_length() + def get_history_length(self ): u'''Return the desired length of the history file. @@ -142,6 +148,10 @@ class BaseReadline(object): ''' self.mode._history.set_history_length(length) + def get_history_item(self, index): + u'''Return the current contents of history item at index.''' + return self.mode._history.get_history_item(index) + def clear_history(self): u'''Clear readline history''' self.mode._history.clear_history() @@ -581,7 +591,9 @@ write_history_file = rl.write_history_file read_history_file = rl.read_history_file get_completer_delims = rl.get_completer_delims +get_current_history_length = rl.get_current_history_length get_history_length = rl.get_history_length +get_history_item = rl.get_history_item get_line_buffer = rl.get_line_buffer set_completer = rl.set_completer get_completer = rl.get_completer