1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-20 20:09:07 +00:00

#394: Try simpler solution to limit lines count

This commit is contained in:
nvbn 2015-10-29 20:17:17 +08:00
parent 16533e85a7
commit bd6ee68c03
2 changed files with 20 additions and 10 deletions

View File

@ -18,7 +18,7 @@ def history_lines(mocker):
def aux(lines):
mock = mocker.patch('io.open')
mock.return_value.__enter__\
.return_value.__iter__.return_value = lines
.return_value.readlines.return_value = lines
return aux

View File

@ -60,19 +60,29 @@ class Generic(object):
"""
return ''
def _get_history_lines(self, history_file):
"""Returns all history lines.
If `settings.history_limit` defined, limits result to `settings.history_limit`.
"""
if not settings.history_limit:
return history_file.readlines()
buffer = []
for line in history_file.readlines():
if len(buffer) > settings.history_limit:
buffer.pop(0)
buffer.append(line)
return buffer
def get_history(self):
"""Returns list of history entries."""
tail_num = settings.history_limit
history_file_name = self._get_history_file_name()
if os.path.isfile(history_file_name):
if tail_num is not None and tail_num.isdigit():
_, f = os.popen2("tail -n {} {}".format(tail_num, history_file_name))
_.close()
else:
f = io.open(history_file_name, 'r',
encoding='utf-8', errors='ignore')
with f as history:
for line in history:
with io.open(history_file_name, 'r',
encoding='utf-8', errors='ignore') as history_file:
for line in self._get_history_lines(history_file):
prepared = self._script_from_history(line)\
.strip()
if prepared: