mirror of
https://github.com/nvbn/thefuck.git
synced 2025-11-03 08:32:03 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ed01fedbf | ||
|
|
ab8ac23749 | ||
|
|
e7d5d93056 | ||
|
|
5ccf163594 |
@@ -158,7 +158,8 @@ using matched rule and run it. Rules enabled by default:
|
|||||||
* `no_command` – fixes wrong console commands, for example `vom/vim`;
|
* `no_command` – fixes wrong console commands, for example `vom/vim`;
|
||||||
* `python_command` – prepends `python` when you trying to run not executable/without `./` python script;
|
* `python_command` – prepends `python` when you trying to run not executable/without `./` python script;
|
||||||
* `rm_dir` – adds `-rf` when you trying to remove directory;
|
* `rm_dir` – adds `-rf` when you trying to remove directory;
|
||||||
* `sudo` – prepends `sudo` to previous command if it failed because of permissions.
|
* `sudo` – prepends `sudo` to previous command if it failed because of permissions;
|
||||||
|
* `switch_layout` – switches command from your local layout to en.
|
||||||
|
|
||||||
## Creating your own rules
|
## Creating your own rules
|
||||||
|
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
|
|
||||||
setup(name='thefuck',
|
setup(name='thefuck',
|
||||||
version=1.15,
|
version=1.17,
|
||||||
description="Magnificent app which corrects your previous console command",
|
description="Magnificent app which corrects your previous console command",
|
||||||
author='Vladimir Iakovlev',
|
author='Vladimir Iakovlev',
|
||||||
author_email='nvbn.rm@gmail.com',
|
author_email='nvbn.rm@gmail.com',
|
||||||
|
|||||||
18
tests/rules/test_switch_lang.py
Normal file
18
tests/rules/test_switch_lang.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
from mock import Mock
|
||||||
|
from thefuck.rules import switch_lang
|
||||||
|
|
||||||
|
|
||||||
|
def test_match():
|
||||||
|
assert switch_lang.match(Mock(stderr='command not found: фзе-пуе',
|
||||||
|
script=u'фзе-пуе'), None)
|
||||||
|
assert not switch_lang.match(Mock(stderr='command not found: pat-get',
|
||||||
|
script=u'pat-get'), None)
|
||||||
|
assert not switch_lang.match(Mock(stderr='some info',
|
||||||
|
script=u'фзе-пуе'), None)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_new_command():
|
||||||
|
assert switch_lang.get_new_command(
|
||||||
|
Mock(script=u'фзе-пуе штыефдд мшь'), None) == 'apt-get install vim'
|
||||||
@@ -53,8 +53,8 @@ def test_get_command():
|
|||||||
return_value=True):
|
return_value=True):
|
||||||
Popen.return_value.stdout.read.return_value = b'stdout'
|
Popen.return_value.stdout.read.return_value = b'stdout'
|
||||||
Popen.return_value.stderr.read.return_value = b'stderr'
|
Popen.return_value.stderr.read.return_value = b'stderr'
|
||||||
assert main.get_command(Mock(), ['thefuck', 'apt-get',
|
assert main.get_command(Mock(), [b'thefuck', b'apt-get',
|
||||||
'search', 'vim']) \
|
b'search', b'vim']) \
|
||||||
== main.Command('apt-get search vim', 'stdout', 'stderr')
|
== main.Command('apt-get search vim', 'stdout', 'stderr')
|
||||||
Popen.assert_called_once_with('apt-get search vim',
|
Popen.assert_called_once_with('apt-get search vim',
|
||||||
shell=True,
|
shell=True,
|
||||||
|
|||||||
@@ -76,7 +76,10 @@ def wait_output(settings, popen):
|
|||||||
|
|
||||||
def get_command(settings, args):
|
def get_command(settings, args):
|
||||||
"""Creates command from `args` and executes it."""
|
"""Creates command from `args` and executes it."""
|
||||||
script = ' '.join(args[1:])
|
if sys.version_info[0] < 3:
|
||||||
|
script = ' '.join(arg.decode('utf-8') for arg in args[1:])
|
||||||
|
else:
|
||||||
|
script = ' '.join(args[1:])
|
||||||
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
|
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
|
||||||
env=dict(os.environ, LANG='C'))
|
env=dict(os.environ, LANG='C'))
|
||||||
if wait_output(settings, result):
|
if wait_output(settings, result):
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ def match(command, settings):
|
|||||||
|
|
||||||
|
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return './{}'.format(command.script)
|
return u'./{}'.format(command.script)
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ local_settings = {'command_not_found': '/usr/lib/command-not-found'}
|
|||||||
|
|
||||||
def _get_output(command, settings):
|
def _get_output(command, settings):
|
||||||
name = command.script.split(' ')[command.script.startswith('sudo')]
|
name = command.script.split(' ')[command.script.startswith('sudo')]
|
||||||
check_script = '{} {}'.format(settings.command_not_found, name)
|
check_script = u'{} {}'.format(settings.command_not_found, name)
|
||||||
result = Popen(check_script, shell=True, stderr=PIPE)
|
result = Popen(check_script, shell=True, stderr=PIPE)
|
||||||
return result.stderr.read().decode('utf-8')
|
return result.stderr.read().decode('utf-8')
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,12 @@
|
|||||||
# 2) is interpreted as shell script
|
# 2) is interpreted as shell script
|
||||||
|
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
toks = command.script.split()
|
toks = command.script.split()
|
||||||
return (len(toks) > 0
|
return (len(toks) > 0
|
||||||
and toks[0].endswith('.py')
|
and toks[0].endswith('.py')
|
||||||
and ('Permission denied' in command.stderr or
|
and ('Permission denied' in command.stderr or
|
||||||
'command not found' in command.stderr))
|
'command not found' in command.stderr))
|
||||||
|
|
||||||
|
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return 'python ' + command.script
|
return 'python ' + command.script
|
||||||
|
|||||||
@@ -14,4 +14,4 @@ def match(command, settings):
|
|||||||
|
|
||||||
|
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return 'sudo {}'.format(command.script)
|
return u'sudo {}'.format(command.script)
|
||||||
|
|||||||
29
thefuck/rules/switch_lang.py
Normal file
29
thefuck/rules/switch_lang.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
target_layout = '''qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?'''
|
||||||
|
|
||||||
|
source_layouts = [u'''йцукенгшщзхъфывапролджэячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,''']
|
||||||
|
|
||||||
|
|
||||||
|
def _get_matched_layout(command):
|
||||||
|
for source_layout in source_layouts:
|
||||||
|
if all([ch in source_layout or ch in '-_'
|
||||||
|
for ch in command.script.split(' ')[0]]):
|
||||||
|
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)]
|
||||||
|
else:
|
||||||
|
return ch
|
||||||
|
|
||||||
|
|
||||||
|
def get_new_command(command, settings):
|
||||||
|
matched_layout = _get_matched_layout(command)
|
||||||
|
return ''.join(_switch(ch, matched_layout) for ch in command.script)
|
||||||
|
|
||||||
Reference in New Issue
Block a user