1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-14 06:38:32 +00:00
2016-11-23 22:42:16 -05:00

59 lines
2.0 KiB
Python

import os
import bashlex
from ..conf import settings
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 _parse_alias(self, alias):
name, value = alias.replace('alias ', '', 1).split('=', 1)
if value[0] == value[-1] == '"' or value[0] == value[-1] == "'":
value = value[1:-1]
return name, value
@memoize
def get_aliases(self):
raw_aliases = os.environ.get('TF_SHELL_ALIASES', '').split('\n')
return dict(self._parse_alias(alias)
for alias in raw_aliases if alias and '=' in alias)
def _get_history_file_name(self):
return os.environ.get("HISTFILE",
os.path.expanduser('~/.bash_history'))
def _get_history_line(self, command_script):
return u'{}\n'.format(command_script)
def how_to_configure(self):
if os.path.join(os.path.expanduser('~'), '.bashrc'):
config = '~/.bashrc'
elif os.path.join(os.path.expanduser('~'), '.bash_profile'):
config = '~/.bashrc'
else:
config = 'bash config'
return 'eval $(thefuck --alias)', config
def split_command(self, command):
generic = Generic()
# If bashlex fails for some reason, fallback to shlex
# See https://github.com/idank/bashlex#limitations
try:
return generic.decode_utf8(list(bashlex.split(generic.encode_utf8(command))))
except:
return generic.split_command(command)