1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 10:11:14 +00:00

improve no_command rule

This commit is contained in:
Jon Mason 2015-04-18 10:52:00 -05:00
parent bc4dc011e4
commit de343b84c1
2 changed files with 22 additions and 7 deletions

View File

@ -39,7 +39,7 @@ def test_match(command_found, command_not_found, settings):
with patch('thefuck.rules.no_command.Popen') as Popen: with patch('thefuck.rules.no_command.Popen') as Popen:
Popen.return_value.stderr.read.return_value = command_found Popen.return_value.stderr.read.return_value = command_found
assert match(Command('aptget install vim', '', ''), settings) assert match(Command('aptget install vim', '', ''), settings)
Popen.assert_called_once_with('/usr/lib/command-not-found aptget', Popen.assert_called_with('/usr/lib/command-not-found aptget',
shell=True, stderr=PIPE) shell=True, stderr=PIPE)
Popen.return_value.stderr.read.return_value = command_not_found Popen.return_value.stderr.read.return_value = command_not_found
assert not match(Command('ls', '', ''), settings) assert not match(Command('ls', '', ''), settings)
@ -48,7 +48,7 @@ def test_match(command_found, command_not_found, settings):
Popen.return_value.stderr.read.return_value = command_found Popen.return_value.stderr.read.return_value = command_found
assert match(Command('sudo aptget install vim', '', ''), assert match(Command('sudo aptget install vim', '', ''),
Mock(command_not_found='test')) Mock(command_not_found='test'))
Popen.assert_called_once_with('test aptget', Popen.assert_called_with('test aptget',
shell=True, stderr=PIPE) shell=True, stderr=PIPE)

View File

@ -2,7 +2,6 @@ from subprocess import Popen, PIPE
import re import re
from thefuck.utils import which, wrap_settings from thefuck.utils import which, wrap_settings
local_settings = {'command_not_found': '/usr/lib/command-not-found'} local_settings = {'command_not_found': '/usr/lib/command-not-found'}
@ -12,12 +11,27 @@ def _get_output(command, settings):
result = Popen(check_script, shell=True, stderr=PIPE) result = Popen(check_script, shell=True, stderr=PIPE)
return result.stderr.read().decode() return result.stderr.read().decode()
def _count_history_uses(name):
script = 'history | grep {}'.format(name)
result = Popen(script, shell=True, stdout=PIPE)
return len(list(result.stdout))
def _get_candidate_commands(command, settings):
output = _get_output(command, settings)
if "No command" in output and "from package" in output:
fixed_names = re.findall(r"Command '([^']*)' from package",
output)
return filter(which, fixed_names)
return []
@wrap_settings(local_settings) @wrap_settings(local_settings)
def match(command, settings): def match(command, settings):
if which(settings.command_not_found): if which(settings.command_not_found):
output = _get_output(command, settings) output = _get_output(command, settings)
return "No command" in output and "from package" in output return len(_get_candidate_commands(command, settings)) != 0
@wrap_settings(local_settings) @wrap_settings(local_settings)
@ -25,6 +39,7 @@ def get_new_command(command, settings):
output = _get_output(command, settings) output = _get_output(command, settings)
broken_name = re.findall(r"No command '([^']*)' found", broken_name = re.findall(r"No command '([^']*)' found",
output)[0] output)[0]
fixed_name = re.findall(r"Command '([^']*)' from package", candidates = _get_candidate_commands(command, settings)
output)[0] fixed_name = sorted(candidates, key=_count_history_uses)[0]
return command.script.replace(broken_name, fixed_name, 1) return command.script.replace(broken_name, fixed_name)