Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement readline.set_auto_history() #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pyrepl/historical_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def __init__(self, console):
self.transient_history = {}
self.next_history = None
self.isearch_direction = ISEARCH_DIRECTION_NONE
self.should_auto_add_history = True
for c in [next_history, previous_history, restore_history,
first_history, last_history, yank_arg,
forward_history_isearch, reverse_history_isearch,
Expand Down Expand Up @@ -291,12 +292,13 @@ def isearch_next(self):

def finish(self):
super(HistoricalReader, self).finish()
ret = self.get_unicode()
for i, t in self.transient_history.items():
if i < len(self.history) and i != self.historyi:
self.history[i] = t
if ret:
self.history.append(ret)
if self.should_auto_add_history:
ret = self.get_unicode()
for i, t in self.transient_history.items():
if i < len(self.history) and i != self.historyi:
self.history[i] = t
if ret:
self.history.append(ret)

def test():
from pyrepl.unix_console import UnixConsole
Expand Down
5 changes: 5 additions & 0 deletions pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
'redisplay',
'remove_history_item',
'replace_history_item',
'set_auto_history',
'set_completer',
'set_completer_delims',
'set_history_length',
Expand Down Expand Up @@ -252,6 +253,9 @@ def multiline_input(self, more_lines, ps1, ps2, returns_unicode=False):
def parse_and_bind(self, string):
pass # XXX we don't support parsing GNU-readline-style init files

def set_auto_history(self, enabled):
self.get_reader().should_auto_add_history = enabled

def set_completer(self, function=None):
self.config.readline_completer = function

Expand Down Expand Up @@ -400,6 +404,7 @@ def insert_text(self, text):
get_history_item = _wrapper.get_history_item
remove_history_item = _wrapper.remove_history_item
replace_history_item = _wrapper.replace_history_item
set_auto_history = _wrapper.set_auto_history
add_history = _wrapper.add_history
set_startup_hook = _wrapper.set_startup_hook
get_line_buffer = _wrapper.get_line_buffer
Expand Down
12 changes: 12 additions & 0 deletions testing/test_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ def replace(cls, *args):

with open(str(histfile), "r") as f:
assert f.readlines() == ["foo\n", "bar\n"]


@pytest.mark.parametrize('auto_history,expected', [(True, 1), (False, 0)])
def test_set_auto_history(auto_history, expected):
master, slave = pty.openpty()
readline_wrapper = _ReadlineWrapper(slave, slave)
readline_wrapper.set_auto_history(auto_history)

os.write(master, b'input\n')
readline_wrapper.get_reader().readline()

assert readline_wrapper.get_current_history_length() == expected