mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-22 12:58:33 +00:00
#394: Try simpler solution to limit lines count
This commit is contained in:
parent
16533e85a7
commit
bd6ee68c03
@ -18,7 +18,7 @@ def history_lines(mocker):
|
|||||||
def aux(lines):
|
def aux(lines):
|
||||||
mock = mocker.patch('io.open')
|
mock = mocker.patch('io.open')
|
||||||
mock.return_value.__enter__\
|
mock.return_value.__enter__\
|
||||||
.return_value.__iter__.return_value = lines
|
.return_value.readlines.return_value = lines
|
||||||
return aux
|
return aux
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,19 +60,29 @@ class Generic(object):
|
|||||||
"""
|
"""
|
||||||
return ''
|
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):
|
def get_history(self):
|
||||||
"""Returns list of history entries."""
|
"""Returns list of history entries."""
|
||||||
tail_num = settings.history_limit
|
|
||||||
history_file_name = self._get_history_file_name()
|
history_file_name = self._get_history_file_name()
|
||||||
if os.path.isfile(history_file_name):
|
if os.path.isfile(history_file_name):
|
||||||
if tail_num is not None and tail_num.isdigit():
|
with io.open(history_file_name, 'r',
|
||||||
_, f = os.popen2("tail -n {} {}".format(tail_num, history_file_name))
|
encoding='utf-8', errors='ignore') as history_file:
|
||||||
_.close()
|
for line in self._get_history_lines(history_file):
|
||||||
else:
|
|
||||||
f = io.open(history_file_name, 'r',
|
|
||||||
encoding='utf-8', errors='ignore')
|
|
||||||
with f as history:
|
|
||||||
for line in history:
|
|
||||||
prepared = self._script_from_history(line)\
|
prepared = self._script_from_history(line)\
|
||||||
.strip()
|
.strip()
|
||||||
if prepared:
|
if prepared:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user