1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-19 09:08:47 +00:00

Handle user defined fish aliases

This commit is contained in:
David 2018-01-01 14:54:22 +00:00
parent f966ecd4f5
commit cc1c558754
2 changed files with 28 additions and 7 deletions

View File

@ -10,12 +10,13 @@ class TestFish(object):
def shell(self): def shell(self):
return Fish() return Fish()
@pytest.fixture(autouse=True) @pytest.fixture
def Popen(self, mocker): def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.fish.Popen') mock = mocker.patch('thefuck.shells.fish.Popen')
mock.return_value.stdout.read.return_value = ( mock.return_value.stdout.read.side_effect = [(
b'cd\nfish_config\nfuck\nfunced\nfuncsave\ngrep\nhistory\nll\nls\n' b'cd\nfish_config\nfuck\nfunced\nfuncsave\ngrep\nhistory\nll\nls\n'
b'man\nmath\npopd\npushd\nruby') b'man\nmath\npopd\npushd\nruby'),
b'alias g git']
return mock return mock
@pytest.mark.parametrize('key, value', [ @pytest.mark.parametrize('key, value', [
@ -43,6 +44,7 @@ class TestFish(object):
('vim', 'vim'), ('vim', 'vim'),
('ll', 'fish -ic "ll"'), ('ll', 'fish -ic "ll"'),
('ls', 'ls')]) # Fish has no aliases but functions ('ls', 'ls')]) # Fish has no aliases but functions
@pytest.mark.usefixtures('Popen')
def test_from_shell(self, before, after, shell): def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after assert shell.from_shell(before) == after
@ -55,6 +57,7 @@ class TestFish(object):
def test_or_(self, shell): def test_or_(self, shell):
assert shell.or_('foo', 'bar') == 'foo; or bar' assert shell.or_('foo', 'bar') == 'foo; or bar'
@pytest.mark.usefixtures('Popen')
def test_get_aliases(self, shell): def test_get_aliases(self, shell):
assert shell.get_aliases() == {'fish_config': 'fish_config', assert shell.get_aliases() == {'fish_config': 'fish_config',
'fuck': 'fuck', 'fuck': 'fuck',
@ -65,7 +68,8 @@ class TestFish(object):
'math': 'math', 'math': 'math',
'popd': 'popd', 'popd': 'popd',
'pushd': 'pushd', 'pushd': 'pushd',
'ruby': 'ruby'} 'ruby': 'ruby',
'g': 'git'}
def test_app_alias(self, shell): def test_app_alias(self, shell):
assert 'function fuck' in shell.app_alias('fuck') assert 'function fuck' in shell.app_alias('fuck')

View File

@ -10,12 +10,24 @@ from .generic import Generic
@cache('~/.config/fish/config.fish', '~/.config/fish/functions') @cache('~/.config/fish/config.fish', '~/.config/fish/functions')
def _get_aliases(overridden): def _get_functions(overridden):
proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL) proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL)
functions = proc.stdout.read().decode('utf-8').strip().split('\n') functions = proc.stdout.read().decode('utf-8').strip().split('\n')
return {func: func for func in functions if func not in overridden} return {func: func for func in functions if func not in overridden}
@cache('~/.config/fish/config.fish')
def _get_aliases(overridden):
aliases = {}
proc = Popen(['fish', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
alias_out = proc.stdout.read().decode('utf-8').strip().split('\n')
for alias in alias_out:
name, value = alias.replace('alias ', '', 1).split(' ', 1)
if name not in overridden:
aliases[name] = value
return aliases
class Fish(Generic): class Fish(Generic):
def _get_overridden_aliases(self): def _get_overridden_aliases(self):
overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES', overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES',
@ -44,12 +56,17 @@ class Fish(Generic):
def get_aliases(self): def get_aliases(self):
overridden = self._get_overridden_aliases() overridden = self._get_overridden_aliases()
return _get_aliases(overridden) functions = _get_functions(overridden)
raw_aliases = _get_aliases(overridden)
functions.update(raw_aliases)
return functions
def _expand_aliases(self, command_script): def _expand_aliases(self, command_script):
aliases = self.get_aliases() aliases = self.get_aliases()
binary = command_script.split(' ')[0] binary = command_script.split(' ')[0]
if binary in aliases: if binary in aliases and aliases[binary] != binary:
return command_script.replace(binary, aliases[binary], 1)
elif binary in aliases:
return u'fish -ic "{}"'.format(command_script.replace('"', r'\"')) return u'fish -ic "{}"'.format(command_script.replace('"', r'\"'))
else: else:
return command_script return command_script