mirror of
https://github.com/nvbn/thefuck.git
synced 2025-11-02 08:02:04 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7071763a3 | ||
|
|
27b5b9de6a | ||
|
|
c0eae8b85c |
2
setup.py
2
setup.py
@@ -11,7 +11,7 @@ elif (3, 0) < sys.version_info < (3, 3):
|
|||||||
' ({}.{} detected).'.format(*sys.version_info[:2]))
|
' ({}.{} detected).'.format(*sys.version_info[:2]))
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
VERSION = '1.47'
|
VERSION = '1.48'
|
||||||
|
|
||||||
install_requires = ['psutil', 'colorama', 'six']
|
install_requires = ['psutil', 'colorama', 'six']
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,16 @@ stats
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def git_not_command_closest():
|
||||||
|
return '''git: 'tags' is not a git command. See 'git --help'.
|
||||||
|
|
||||||
|
Did you mean one of these?
|
||||||
|
stage
|
||||||
|
tag
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def git_command():
|
def git_command():
|
||||||
return "* master"
|
return "* master"
|
||||||
@@ -37,8 +47,11 @@ def test_match(git_not_command, git_command, git_not_command_one_of_this):
|
|||||||
assert not match(Command('git branch', stderr=git_command), None)
|
assert not match(Command('git branch', stderr=git_command), None)
|
||||||
|
|
||||||
|
|
||||||
def test_get_new_command(git_not_command, git_not_command_one_of_this):
|
def test_get_new_command(git_not_command, git_not_command_one_of_this,
|
||||||
assert get_new_command(Command('git brnch', stderr=git_not_command), None)\
|
git_not_command_closest):
|
||||||
== 'git branch'
|
assert get_new_command(Command('git brnch', stderr=git_not_command), None) \
|
||||||
|
== 'git branch'
|
||||||
assert get_new_command(Command('git st', stderr=git_not_command_one_of_this),
|
assert get_new_command(Command('git st', stderr=git_not_command_one_of_this),
|
||||||
None) == 'git status'
|
None) == 'git status'
|
||||||
|
assert get_new_command(Command('git tags', stderr=git_not_command_closest),
|
||||||
|
None) == 'git tag'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from mock import Mock
|
from mock import Mock
|
||||||
from thefuck.utils import sudo_support, wrap_settings, memoize
|
from thefuck.utils import sudo_support, wrap_settings, memoize, get_closest
|
||||||
from thefuck.types import Settings
|
from thefuck.types import Settings
|
||||||
from tests.utils import Command
|
from tests.utils import Command
|
||||||
|
|
||||||
@@ -32,3 +32,12 @@ def test_memoize():
|
|||||||
memoized()
|
memoized()
|
||||||
memoized()
|
memoized()
|
||||||
fn.assert_called_once_with()
|
fn.assert_called_once_with()
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetClosest(object):
|
||||||
|
|
||||||
|
def test_when_can_match(self):
|
||||||
|
assert 'branch' == get_closest('brnch', ['branch', 'status'])
|
||||||
|
|
||||||
|
def test_when_cant_match(self):
|
||||||
|
assert 'status' == get_closest('st', ['status', 'reset'])
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import difflib
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from subprocess import check_output
|
from subprocess import check_output
|
||||||
|
from thefuck.utils import get_closest
|
||||||
|
|
||||||
# Formulars are base on each local system's status
|
# Formulars are base on each local system's status
|
||||||
|
|
||||||
brew_formulas = []
|
brew_formulas = []
|
||||||
try:
|
try:
|
||||||
brew_path_prefix = check_output(['brew', '--prefix'],
|
brew_path_prefix = check_output(['brew', '--prefix'],
|
||||||
@@ -17,8 +18,8 @@ except:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _get_similar_formulars(formula_name):
|
def _get_similar_formula(formula_name):
|
||||||
return difflib.get_close_matches(formula_name, brew_formulas, 1, 0.85)
|
return get_closest(formula_name, brew_formulas, 1, 0.85)
|
||||||
|
|
||||||
|
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
@@ -29,7 +30,7 @@ def match(command, settings):
|
|||||||
if is_proper_command:
|
if is_proper_command:
|
||||||
formula = re.findall(r'Error: No available formula for ([a-z]+)',
|
formula = re.findall(r'Error: No available formula for ([a-z]+)',
|
||||||
command.stderr)[0]
|
command.stderr)[0]
|
||||||
has_possible_formulas = len(_get_similar_formulars(formula)) > 0
|
has_possible_formulas = bool(_get_similar_formula(formula))
|
||||||
|
|
||||||
return has_possible_formulas
|
return has_possible_formulas
|
||||||
|
|
||||||
@@ -37,6 +38,6 @@ def match(command, settings):
|
|||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
not_exist_formula = re.findall(r'Error: No available formula for ([a-z]+)',
|
not_exist_formula = re.findall(r'Error: No available formula for ([a-z]+)',
|
||||||
command.stderr)[0]
|
command.stderr)[0]
|
||||||
exist_formula = _get_similar_formulars(not_exist_formula)[0]
|
exist_formula = _get_similar_formula(not_exist_formula)
|
||||||
|
|
||||||
return command.script.replace(not_exist_formula, exist_formula, 1)
|
return command.script.replace(not_exist_formula, exist_formula, 1)
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import difflib
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from thefuck.utils import get_closest
|
||||||
|
|
||||||
BREW_CMD_PATH = '/Library/Homebrew/cmd'
|
BREW_CMD_PATH = '/Library/Homebrew/cmd'
|
||||||
TAP_PATH = '/Library/Taps'
|
TAP_PATH = '/Library/Taps'
|
||||||
@@ -78,8 +77,8 @@ if brew_path_prefix:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _get_similar_commands(command):
|
def _get_similar_command(command):
|
||||||
return difflib.get_close_matches(command, brew_commands)
|
return get_closest(command, brew_commands)
|
||||||
|
|
||||||
|
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
@@ -90,7 +89,7 @@ def match(command, settings):
|
|||||||
if is_proper_command:
|
if is_proper_command:
|
||||||
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
||||||
command.stderr)[0]
|
command.stderr)[0]
|
||||||
has_possible_commands = len(_get_similar_commands(broken_cmd)) > 0
|
has_possible_commands = bool(_get_similar_command(broken_cmd))
|
||||||
|
|
||||||
return has_possible_commands
|
return has_possible_commands
|
||||||
|
|
||||||
@@ -98,6 +97,6 @@ def match(command, settings):
|
|||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
||||||
command.stderr)[0]
|
command.stderr)[0]
|
||||||
new_cmd = _get_similar_commands(broken_cmd)[0]
|
new_cmd = _get_similar_command(broken_cmd)
|
||||||
|
|
||||||
return command.script.replace(broken_cmd, new_cmd, 1)
|
return command.script.replace(broken_cmd, new_cmd, 1)
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
from difflib import get_close_matches
|
||||||
import re
|
import re
|
||||||
|
from thefuck.utils import get_closest
|
||||||
|
|
||||||
|
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
@@ -7,10 +9,19 @@ def match(command, settings):
|
|||||||
and 'Did you mean' in command.stderr)
|
and 'Did you mean' in command.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_all_git_matched_commands(stderr):
|
||||||
|
should_yield = False
|
||||||
|
for line in stderr.split('\n'):
|
||||||
|
if 'Did you mean' in line:
|
||||||
|
should_yield = True
|
||||||
|
elif should_yield and line:
|
||||||
|
yield line.strip()
|
||||||
|
|
||||||
|
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
broken_cmd = re.findall(r"git: '([^']*)' is not a git command",
|
broken_cmd = re.findall(r"git: '([^']*)' is not a git command",
|
||||||
command.stderr)[0]
|
command.stderr)[0]
|
||||||
new_cmd = re.findall(r'Did you mean[^\n]*\n\s*([^\n]*)',
|
new_cmd = get_closest(broken_cmd,
|
||||||
command.stderr)[0]
|
_get_all_git_matched_commands(command.stderr))
|
||||||
return command.script.replace(broken_cmd, new_cmd, 1)
|
return command.script.replace(broken_cmd, new_cmd, 1)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import re
|
import re
|
||||||
|
from thefuck.utils import get_closest
|
||||||
from difflib import get_close_matches
|
|
||||||
|
|
||||||
|
|
||||||
def extract_possisiblities(command):
|
def extract_possisiblities(command):
|
||||||
@@ -26,9 +25,5 @@ def match(command, settings):
|
|||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
script = command.script.split(' ')
|
script = command.script.split(' ')
|
||||||
possisiblities = extract_possisiblities(command)
|
possisiblities = extract_possisiblities(command)
|
||||||
matches = get_close_matches(script[1], possisiblities)
|
script[1] = get_closest(script[1], possisiblities)
|
||||||
if matches:
|
|
||||||
script[1] = matches[0]
|
|
||||||
else:
|
|
||||||
script[1] = possisiblities[0]
|
|
||||||
return ' '.join(script)
|
return ' '.join(script)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from difflib import get_close_matches
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
@@ -85,3 +86,12 @@ def memoize(fn):
|
|||||||
return memo[key]
|
return memo[key]
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def get_closest(word, possibilities, n=3, cutoff=0.6):
|
||||||
|
"""Returns closest match or just first from possibilities."""
|
||||||
|
possibilities = list(possibilities)
|
||||||
|
try:
|
||||||
|
return get_close_matches(word, possibilities, n, cutoff)[0]
|
||||||
|
except IndexError:
|
||||||
|
return possibilities[0]
|
||||||
|
|||||||
Reference in New Issue
Block a user