diff --git a/tests/rules/test_switch_lang.py b/tests/rules/test_switch_lang.py index 12163d4b..71c2c731 100644 --- a/tests/rules/test_switch_lang.py +++ b/tests/rules/test_switch_lang.py @@ -15,6 +15,7 @@ def test_match(command): @pytest.mark.parametrize('command', [ Command(stderr='command not found: pat-get', script=u'pat-get'), Command(stderr='command not found: ls', script=u'ls'), + Command(stderr='command not found: агсл', script=u'агсл'), Command(stderr='some info', script=u'фзе-пуе')]) def test_not_match(command): assert not switch_lang.match(command, None) diff --git a/thefuck/rules/switch_lang.py b/thefuck/rules/switch_lang.py index af427208..2ecd6325 100644 --- a/thefuck/rules/switch_lang.py +++ b/thefuck/rules/switch_lang.py @@ -1,4 +1,6 @@ # -*- encoding: utf-8 -*- +from thefuck.shells import thefuck_alias +from thefuck.utils import memoize target_layout = '''qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?''' @@ -7,6 +9,7 @@ source_layouts = [u'''йцукенгшщзхъфывапролджэячсмит u''';ςερτυθιοπ[]ασδφγηξκλ΄ζχψωβνμ,./:΅ΕΡΤΥΘΙΟΠ{}ΑΣΔΦΓΗΞΚΛ¨"ΖΧΨΩΒΝΜ<>?'''] +@memoize def _get_matched_layout(command): for source_layout in source_layouts: if all([ch in source_layout or ch in '-_' @@ -14,10 +17,6 @@ def _get_matched_layout(command): return source_layout -def match(command, settings): - return 'not found' in command.stderr and _get_matched_layout(command) - - def _switch(ch, layout): if ch in layout: return target_layout[layout.index(ch)] @@ -25,7 +24,18 @@ def _switch(ch, layout): return ch +def _switch_command(command, layout): + return ''.join(_switch(ch, layout) for ch in command.script) + + +def match(command, settings): + if 'not found' not in command.stderr: + return False + matched_layout = _get_matched_layout(command) + return matched_layout and \ + _switch_command(command, matched_layout) != thefuck_alias() + + def get_new_command(command, settings): matched_layout = _get_matched_layout(command) - return ''.join(_switch(ch, matched_layout) for ch in command.script) - + return _switch_command(command, matched_layout)