Skip to content

Commit

Permalink
#394: Try simpler solution to limit lines count
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbn committed Oct 29, 2015
1 parent 16533e8 commit bd6ee68
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tests/test_shells.py
Expand Up @@ -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


Expand Down
28 changes: 19 additions & 9 deletions thefuck/shells.py
Expand Up @@ -60,19 +60,29 @@ def _script_from_history(self, line):
"""
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:
Expand Down

2 comments on commit bd6ee68

@ls0f
Copy link
Contributor

@ls0f ls0f commented on bd6ee68 Oct 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

history_file.readlines() seems to be malloc much memory and inefficient if the history command file is huge, But it should be okay because the log files has size limit or rows limit.

@ls0f
Copy link
Contributor

@ls0f ls0f commented on bd6ee68 Oct 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tail -f may be is the easiest way to implement the feature, but not pythonic.

Please sign in to comment.