1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-14 06:38:32 +00:00

Merge pull request #409 from nvbn/394-history-limit

#394 history limit
This commit is contained in:
Vladimir Iakovlev 2015-11-19 11:17:21 +08:00
commit b3e9b36bd1
4 changed files with 19 additions and 14 deletions

View File

@ -280,7 +280,8 @@ The Fuck has a few settings parameters which can be changed in `$XDG_CONFIG_HOME
* `wait_command` – max amount of time in seconds for getting previous command output; * `wait_command` – max amount of time in seconds for getting previous command output;
* `no_colors` – disable colored output; * `no_colors` – disable colored output;
* `priority` – dict with rules priorities, rule with lower `priority` will be matched first; * `priority` – dict with rules priorities, rule with lower `priority` will be matched first;
* `debug` – enables debug output, by default `False`. * `debug` – enables debug output, by default `False`;
* `history_limit` &ndash numeric value of how many history commands will be scanned, like `2000`;
Example of `settings.py`: Example of `settings.py`:
@ -303,7 +304,8 @@ Or via environment variables:
* `THEFUCK_NO_COLORS` – disable colored output, `true/false`; * `THEFUCK_NO_COLORS` – disable colored output, `true/false`;
* `THEFUCK_PRIORITY` – priority of the rules, like `no_command=9999:apt_get=100`, * `THEFUCK_PRIORITY` – priority of the rules, like `no_command=9999:apt_get=100`,
rule with lower `priority` will be matched first; rule with lower `priority` will be matched first;
* `THEFUCK_DEBUG` – enables debug output, `true/false`. * `THEFUCK_DEBUG` – enables debug output, `true/false`;
* `THEFUCK_HISTORY_LIMIT` – how many history commands will be scanned, like `2000`.
For example: For example:
@ -314,6 +316,7 @@ export THEFUCK_REQUIRE_CONFIRMATION='true'
export THEFUCK_WAIT_COMMAND=10 export THEFUCK_WAIT_COMMAND=10
export THEFUCK_NO_COLORS='false' export THEFUCK_NO_COLORS='false'
export THEFUCK_PRIORITY='no_command=9999:apt_get=100' export THEFUCK_PRIORITY='no_command=9999:apt_get=100'
export THEFUCK_HISTORY_LIMIT='2000'
``` ```
## Developing ## Developing

View File

@ -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

View File

@ -16,6 +16,7 @@ DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
'no_colors': False, 'no_colors': False,
'debug': False, 'debug': False,
'priority': {}, 'priority': {},
'history_limit': None,
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}} 'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}}
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules', ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
@ -24,7 +25,8 @@ ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation', 'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation',
'THEFUCK_NO_COLORS': 'no_colors', 'THEFUCK_NO_COLORS': 'no_colors',
'THEFUCK_PRIORITY': 'priority', 'THEFUCK_PRIORITY': 'priority',
'THEFUCK_DEBUG': 'debug'} 'THEFUCK_DEBUG': 'debug',
'THEFUCK_HISTORY_LIMIT': 'history_limit'}
SETTINGS_HEADER = u"""# The Fuck settings file SETTINGS_HEADER = u"""# The Fuck settings file
# #
@ -126,6 +128,8 @@ class Settings(dict):
return int(val) return int(val)
elif attr in ('require_confirmation', 'no_colors', 'debug'): elif attr in ('require_confirmation', 'no_colors', 'debug'):
return val.lower() == 'true' return val.lower() == 'true'
elif attr == 'history_limit':
return int(val)
else: else:
return val return val

View File

@ -12,6 +12,7 @@ import os
import shlex import shlex
import six import six
from .utils import DEVNULL, memoize, cache from .utils import DEVNULL, memoize, cache
from .conf import settings
class Generic(object): class Generic(object):
@ -52,21 +53,18 @@ class Generic(object):
with open(history_file_name, 'a') as history: with open(history_file_name, 'a') as history:
history.write(self._get_history_line(command_script)) history.write(self._get_history_line(command_script))
def _script_from_history(self, line):
"""Returns prepared history line.
Should return a blank line if history line is corrupted or empty.
"""
return ''
def get_history(self): def get_history(self):
"""Returns list of history entries.""" """Returns list of history entries."""
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):
with io.open(history_file_name, 'r', with io.open(history_file_name, 'r',
encoding='utf-8', errors='ignore') as history: encoding='utf-8', errors='ignore') as history_file:
for line in history:
lines = history_file.readlines()
if settings.history_limit:
lines = lines[-settings.history_limit:]
for line in lines:
prepared = self._script_from_history(line)\ prepared = self._script_from_history(line)\
.strip() .strip()
if prepared: if prepared: