mirror of
https://github.com/nvbn/thefuck.git
synced 2025-11-01 15:42:06 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
478fa4cd09 | ||
|
|
273fc097bd | ||
|
|
00d0987cf5 | ||
|
|
3798c341d5 | ||
|
|
e1fe7ff7d0 | ||
|
|
e3edea05ed | ||
|
|
3606131502 | ||
|
|
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`;
|
||||
* `python_command` – prepends `python` when you trying to run not executable/without `./` python script;
|
||||
* `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
|
||||
|
||||
@@ -192,9 +193,7 @@ The Fuck has a few settings parameters, they can be changed in `~/.thefuck/setti
|
||||
|
||||
* `rules` – list of enabled rules, by default all;
|
||||
* `require_confirmation` – require confirmation before running new command, by default `False`;
|
||||
* `wait_command` – max amount of time in seconds for getting previous command output;
|
||||
* `command_not_found` – path to `command_not_found` binary,
|
||||
by default `/usr/lib/command-not-found`.
|
||||
* `wait_command` – max amount of time in seconds for getting previous command output.
|
||||
|
||||
## Developing
|
||||
|
||||
|
||||
2
setup.py
2
setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
||||
|
||||
|
||||
setup(name='thefuck',
|
||||
version=1.15,
|
||||
version=1.19,
|
||||
description="Magnificent app which corrects your previous console command",
|
||||
author='Vladimir Iakovlev',
|
||||
author_email='nvbn.rm@gmail.com',
|
||||
|
||||
@@ -1,62 +1,19 @@
|
||||
from subprocess import PIPE
|
||||
from mock import patch, Mock
|
||||
import pytest
|
||||
from thefuck.rules.no_command import match, get_new_command
|
||||
from thefuck.main import Command
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def command_found():
|
||||
return b'''No command 'aptget' found, did you mean:
|
||||
Command 'apt-get' from package 'apt' (main)
|
||||
aptget: command not found
|
||||
'''
|
||||
|
||||
@pytest.fixture
|
||||
def command_not_found():
|
||||
return b'''No command 'vom' found, but there are 19 similar ones
|
||||
vom: command not found
|
||||
'''
|
||||
def test_match():
|
||||
with patch('thefuck.rules.no_command._get_all_bins',
|
||||
return_value=['vim', 'apt-get']):
|
||||
assert match(Mock(stderr='vom: not found', script='vom file.py'), None)
|
||||
assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None)
|
||||
assert not match(Mock(stderr='some text', script='vom file.py'), None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bins_exists(request):
|
||||
p = patch('thefuck.rules.no_command.which',
|
||||
return_value=True)
|
||||
p.start()
|
||||
request.addfinalizer(p.stop)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def settings():
|
||||
class _Settings(object):
|
||||
pass
|
||||
return _Settings
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('bins_exists')
|
||||
def test_match(command_found, command_not_found, settings):
|
||||
with patch('thefuck.rules.no_command.Popen') as Popen:
|
||||
Popen.return_value.stderr.read.return_value = command_found
|
||||
assert match(Command('aptget install vim', '', ''), settings)
|
||||
Popen.assert_called_once_with('/usr/lib/command-not-found aptget',
|
||||
shell=True, stderr=PIPE)
|
||||
Popen.return_value.stderr.read.return_value = command_not_found
|
||||
assert not match(Command('ls', '', ''), settings)
|
||||
|
||||
with patch('thefuck.rules.no_command.Popen') as Popen:
|
||||
Popen.return_value.stderr.read.return_value = command_found
|
||||
assert match(Command('sudo aptget install vim', '', ''),
|
||||
Mock(command_not_found='test'))
|
||||
Popen.assert_called_once_with('test aptget',
|
||||
shell=True, stderr=PIPE)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('bins_exists')
|
||||
def test_get_new_command(command_found):
|
||||
with patch('thefuck.rules.no_command._get_output',
|
||||
return_value=command_found.decode()):
|
||||
assert get_new_command(Command('aptget install vim', '', ''), settings)\
|
||||
== 'apt-get install vim'
|
||||
assert get_new_command(Command('sudo aptget install vim', '', ''), settings) \
|
||||
== 'sudo apt-get install vim'
|
||||
def test_get_new_command():
|
||||
with patch('thefuck.rules.no_command._get_all_bins',
|
||||
return_value=['vim', 'apt-get']):
|
||||
assert get_new_command(
|
||||
Mock(stderr='vom: not found',
|
||||
script='vom file.py'),
|
||||
None) == 'vim file.py'
|
||||
|
||||
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'
|
||||
@@ -76,7 +76,10 @@ def wait_output(settings, popen):
|
||||
|
||||
def get_command(settings, args):
|
||||
"""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,
|
||||
env=dict(os.environ, LANG='C'))
|
||||
if wait_output(settings, result):
|
||||
|
||||
@@ -7,5 +7,5 @@ def match(command, settings):
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
||||
return './{}'.format(command.script)
|
||||
return u'./{}'.format(command.script)
|
||||
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
from subprocess import Popen, PIPE
|
||||
import re
|
||||
from thefuck.utils import which, wrap_settings
|
||||
from difflib import get_close_matches
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
local_settings = {'command_not_found': '/usr/lib/command-not-found'}
|
||||
def _safe(path, method):
|
||||
try:
|
||||
return getattr(path, method)()
|
||||
except OSError:
|
||||
return []
|
||||
|
||||
|
||||
def _get_output(command, settings):
|
||||
name = command.script.split(' ')[command.script.startswith('sudo')]
|
||||
check_script = '{} {}'.format(settings.command_not_found, name)
|
||||
result = Popen(check_script, shell=True, stderr=PIPE)
|
||||
return result.stderr.read().decode('utf-8')
|
||||
def _get_all_bins():
|
||||
return [exe.name
|
||||
for path in os.environ['PATH'].split(':')
|
||||
for exe in _safe(Path(path), 'iterdir')
|
||||
if not _safe(exe, 'is_dir')]
|
||||
|
||||
|
||||
@wrap_settings(local_settings)
|
||||
def match(command, settings):
|
||||
if which(settings.command_not_found):
|
||||
output = _get_output(command, settings)
|
||||
return "No command" in output and "from package" in output
|
||||
return 'not found' in command.stderr and \
|
||||
bool(get_close_matches(command.script.split(' ')[0],
|
||||
_get_all_bins()))
|
||||
|
||||
|
||||
@wrap_settings(local_settings)
|
||||
def get_new_command(command, settings):
|
||||
output = _get_output(command, settings)
|
||||
broken_name = re.findall(r"No command '([^']*)' found",
|
||||
output)[0]
|
||||
fixed_name = re.findall(r"Command '([^']*)' from package",
|
||||
output)[0]
|
||||
return command.script.replace(broken_name, fixed_name, 1)
|
||||
old_command = command.script.split(' ')[0]
|
||||
new_command = get_close_matches(old_command,
|
||||
_get_all_bins())[0]
|
||||
return ' '.join([new_command] + command.script.split(' ')[1:])
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
# 2) is interpreted as shell script
|
||||
|
||||
def match(command, settings):
|
||||
toks = command.script.split()
|
||||
return (len(toks) > 0
|
||||
and toks[0].endswith('.py')
|
||||
and ('Permission denied' in command.stderr or
|
||||
'command not found' in command.stderr))
|
||||
toks = command.script.split()
|
||||
return (len(toks) > 0
|
||||
and toks[0].endswith('.py')
|
||||
and ('Permission denied' in command.stderr or
|
||||
'command not found' in command.stderr))
|
||||
|
||||
|
||||
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):
|
||||
return 'sudo {}'.format(command.script)
|
||||
return u'sudo {}'.format(command.script)
|
||||
|
||||
30
thefuck/rules/switch_lang.py
Normal file
30
thefuck/rules/switch_lang.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
target_layout = '''qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?'''
|
||||
|
||||
source_layouts = [u'''йцукенгшщзхъфывапролджэячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,''',
|
||||
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