1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-05 18:31:10 +01:00

#620: Add bash support

This commit is contained in:
Vladimir Iakovlev 2017-03-28 12:01:09 +02:00
parent 7532c65c62
commit beda1854cf
2 changed files with 26 additions and 20 deletions

View File

@ -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'])

View File

@ -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)