1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-06 02:41:10 +01:00

#N/A Ignore history lines before fuck call in history rule

This commit is contained in:
nvbn 2015-07-20 01:53:32 +03:00
parent 7e03b55729
commit ee87d1c547
2 changed files with 27 additions and 8 deletions

View File

@ -6,7 +6,14 @@ from tests.utils import Command
@pytest.fixture @pytest.fixture
def history(mocker): def history(mocker):
return mocker.patch('thefuck.rules.history.get_history', return mocker.patch('thefuck.rules.history.get_history',
return_value=['ls cat', 'diff x', 'nocommand x']) return_value=['le cat', 'fuck', 'ls cat',
'diff x', 'nocommand x'])
@pytest.fixture
def alias(mocker):
return mocker.patch('thefuck.rules.history.thefuck_alias',
return_value='fuck')
@pytest.fixture @pytest.fixture
@ -15,19 +22,19 @@ def callables(mocker):
return_value=['diff', 'ls']) return_value=['diff', 'ls'])
@pytest.mark.usefixtures('history', 'callables', 'no_memoize') @pytest.mark.usefixtures('history', 'callables', 'no_memoize', 'alias')
@pytest.mark.parametrize('script', ['ls cet', 'daff x']) @pytest.mark.parametrize('script', ['ls cet', 'daff x'])
def test_match(script): def test_match(script):
assert match(Command(script=script), None) assert match(Command(script=script), None)
@pytest.mark.usefixtures('history', 'callables', 'no_memoize') @pytest.mark.usefixtures('history', 'callables', 'no_memoize', 'alias')
@pytest.mark.parametrize('script', ['apt-get', 'nocommand y']) @pytest.mark.parametrize('script', ['apt-get', 'nocommand y'])
def test_not_match(script): def test_not_match(script):
assert not match(Command(script=script), None) assert not match(Command(script=script), None)
@pytest.mark.usefixtures('history', 'callables', 'no_memoize') @pytest.mark.usefixtures('history', 'callables', 'no_memoize', 'alias')
@pytest.mark.parametrize('script, result', [ @pytest.mark.parametrize('script, result', [
('ls cet', 'ls cat'), ('ls cet', 'ls cat'),
('daff x', 'diff x')]) ('daff x', 'diff x')])

View File

@ -1,17 +1,28 @@
from difflib import get_close_matches from difflib import get_close_matches
from thefuck.shells import get_history from thefuck.shells import get_history, thefuck_alias
from thefuck.utils import get_closest, memoize from thefuck.utils import get_closest, memoize
from thefuck.rules.no_command import get_all_callables from thefuck.rules.no_command import get_all_callables
def _not_corrected(history, tf_alias):
"""Returns all lines from history except that comes before `fuck`."""
previous = None
for line in history:
if previous is not None and line != tf_alias:
yield previous
previous = line
yield history[-1]
@memoize @memoize
def _history_of_exists_without_current(command): def _history_of_exists_without_current(command):
history = get_history()
tf_alias = thefuck_alias()
callables = get_all_callables() callables = get_all_callables()
return [line for line in get_history() return [line for line in _not_corrected(history, tf_alias)
if line != command.script if not line.startswith(tf_alias) and not line == command.script
and line.split(' ')[0] in callables] and line.split(' ')[0] in callables]
def match(command, settings): def match(command, settings):
return len(get_close_matches(command.script, return len(get_close_matches(command.script,
_history_of_exists_without_current(command))) _history_of_exists_without_current(command)))
@ -21,4 +32,5 @@ def get_new_command(command, settings):
return get_closest(command.script, return get_closest(command.script,
_history_of_exists_without_current(command)) _history_of_exists_without_current(command))
priority = 9999 priority = 9999