diff --git a/tests/shells/test_bash.py b/tests/shells/test_bash.py index 669a0985..84a2fa51 100644 --- a/tests/shells/test_bash.py +++ b/tests/shells/test_bash.py @@ -40,18 +40,17 @@ class TestBash(object): 'll': 'ls -alF'} def test_app_alias(self, shell): - assert 'alias fuck' in shell.app_alias('fuck') - assert 'alias FUCK' in shell.app_alias('FUCK') + assert 'fuck () {' in shell.app_alias('fuck') + assert 'FUCK () {' in shell.app_alias('FUCK') assert 'thefuck' in shell.app_alias('fuck') - assert 'TF_ALIAS=fuck' in shell.app_alias('fuck') - assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck') + assert 'PYTHONIOENCODING' in shell.app_alias('fuck') def test_app_alias_variables_correctly_set(self, shell): alias = shell.app_alias('fuck') - assert "alias fuck='TF_CMD=$(TF_ALIAS" in alias - assert '$(TF_ALIAS=fuck PYTHONIOENCODING' in alias - assert 'PYTHONIOENCODING=utf-8 TF_SHELL_ALIASES' in alias - assert 'ALIASES=$(alias) thefuck' in alias + assert "fuck () {" in alias + assert "TF_ALIAS=fuck" in alias + assert 'PYTHONIOENCODING=utf-8' in alias + assert 'TF_SHELL_ALIASES=$(alias)' in alias def test_get_history(self, history_lines, shell): history_lines(['ls', 'rm']) diff --git a/thefuck/shells/bash.py b/thefuck/shells/bash.py index caf9b1bb..5f56e183 100644 --- a/thefuck/shells/bash.py +++ b/thefuck/shells/bash.py @@ -1,22 +1,29 @@ import os from ..conf import settings +from ..const import ARGUMENT_PLACEHOLDER from ..utils import memoize from .generic import Generic class Bash(Generic): - def app_alias(self, fuck): - # It is VERY important to have the variables declared WITHIN the alias - alias = "alias {0}='TF_CMD=$(TF_ALIAS={0}" \ - " PYTHONIOENCODING=utf-8" \ - " TF_SHELL_ALIASES=$(alias)" \ - " thefuck $(fc -ln -1)) &&" \ - " eval $TF_CMD".format(fuck) - - if settings.alter_history: - return alias + "; history -s $TF_CMD'" - else: - return alias + "'" + def app_alias(self, alias_name): + # It is VERY important to have the variables declared WITHIN the function + return ''' + function {name} () {{ + TF_PREVIOUS=$(fc -ln -1); + TF_CMD=$( + TF_ALIAS={name} + TF_SHELL_ALIASES=$(alias) + PYTHONIOENCODING=utf-8 + thefuck $TF_PREVIOUS {argument_placeholder} $@ + ) && eval $TF_CMD; + {alter_history} + }} + '''.format( + name=alias_name, + argument_placeholder=ARGUMENT_PLACEHOLDER, + alter_history=('history -s $TF_CMD;' + if settings.alter_history else '')) def _parse_alias(self, alias): name, value = alias.replace('alias ', '', 1).split('=', 1)