2016-01-23 05:06:22 +03:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
from time import time
|
|
|
|
import os
|
2016-04-22 03:14:31 +03:00
|
|
|
import sys
|
|
|
|
import six
|
|
|
|
from .. import logs
|
2016-04-29 23:21:28 -03:00
|
|
|
from ..conf import settings
|
2017-10-10 08:30:26 +02:00
|
|
|
from ..utils import DEVNULL, cache
|
2016-01-23 05:06:22 +03:00
|
|
|
from .generic import Generic
|
|
|
|
|
|
|
|
|
|
|
|
class Fish(Generic):
|
|
|
|
def _get_overridden_aliases(self):
|
2016-04-06 23:11:19 -03:00
|
|
|
overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES',
|
|
|
|
os.environ.get('TF_OVERRIDDEN_ALIASES', ''))
|
2016-04-06 22:58:08 -03:00
|
|
|
default = {'cd', 'grep', 'ls', 'man', 'open'}
|
2016-04-06 23:11:19 -03:00
|
|
|
for alias in overridden.split(','):
|
2016-04-06 22:58:08 -03:00
|
|
|
default.add(alias.strip())
|
|
|
|
return default
|
2016-01-23 05:06:22 +03:00
|
|
|
|
2017-08-26 05:46:07 +02:00
|
|
|
def app_alias(self, alias_name):
|
2016-04-29 23:21:28 -03:00
|
|
|
if settings.alter_history:
|
2016-11-17 22:57:11 -02:00
|
|
|
alter_history = (' builtin history delete --exact'
|
|
|
|
' --case-sensitive -- $fucked_up_command\n'
|
|
|
|
' builtin history merge ^ /dev/null\n')
|
2016-04-29 23:21:28 -03:00
|
|
|
else:
|
2016-04-30 18:39:08 -03:00
|
|
|
alter_history = ''
|
2016-03-09 21:58:18 -03:00
|
|
|
# It is VERY important to have the variables declared WITHIN the alias
|
2016-01-23 05:06:22 +03:00
|
|
|
return ('function {0} -d "Correct your previous console command"\n'
|
|
|
|
' set -l fucked_up_command $history[1]\n'
|
|
|
|
' env TF_ALIAS={0} PYTHONIOENCODING=utf-8'
|
|
|
|
' thefuck $fucked_up_command | read -l unfucked_command\n'
|
|
|
|
' if [ "$unfucked_command" != "" ]\n'
|
2016-04-30 18:39:08 -03:00
|
|
|
' eval $unfucked_command\n{1}'
|
2016-01-23 05:06:22 +03:00
|
|
|
' end\n'
|
2017-08-26 05:46:07 +02:00
|
|
|
'end').format(alias_name, alter_history)
|
2016-01-23 05:06:22 +03:00
|
|
|
|
2017-08-30 15:05:44 +02:00
|
|
|
@cache('~/.config/fish/config.fish', '~/.config/fish/functions')
|
2016-01-23 05:06:22 +03:00
|
|
|
def get_aliases(self):
|
|
|
|
overridden = self._get_overridden_aliases()
|
|
|
|
proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL)
|
|
|
|
functions = proc.stdout.read().decode('utf-8').strip().split('\n')
|
|
|
|
return {func: func for func in functions if func not in overridden}
|
|
|
|
|
|
|
|
def _expand_aliases(self, command_script):
|
|
|
|
aliases = self.get_aliases()
|
|
|
|
binary = command_script.split(' ')[0]
|
|
|
|
if binary in aliases:
|
|
|
|
return u'fish -ic "{}"'.format(command_script.replace('"', r'\"'))
|
|
|
|
else:
|
|
|
|
return command_script
|
|
|
|
|
|
|
|
def _get_history_file_name(self):
|
|
|
|
return os.path.expanduser('~/.config/fish/fish_history')
|
|
|
|
|
|
|
|
def _get_history_line(self, command_script):
|
|
|
|
return u'- cmd: {}\n when: {}\n'.format(command_script, int(time()))
|
|
|
|
|
|
|
|
def _script_from_history(self, line):
|
|
|
|
if '- cmd: ' in line:
|
|
|
|
return line.split('- cmd: ', 1)[1]
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def and_(self, *commands):
|
|
|
|
return u'; and '.join(commands)
|
|
|
|
|
2017-03-28 18:09:38 +02:00
|
|
|
def or_(self, *commands):
|
|
|
|
return u'; or '.join(commands)
|
|
|
|
|
2016-01-23 05:06:22 +03:00
|
|
|
def how_to_configure(self):
|
2017-03-13 21:50:13 +01:00
|
|
|
return self._create_shell_configuration(
|
2017-10-05 20:57:08 +02:00
|
|
|
content=u"thefuck --alias | source",
|
2017-03-13 21:50:13 +01:00
|
|
|
path='~/.config/fish/config.fish',
|
|
|
|
reload='fish')
|
2016-04-22 03:14:31 +03:00
|
|
|
|
|
|
|
def put_to_history(self, command):
|
|
|
|
try:
|
|
|
|
return self._put_to_history(command)
|
|
|
|
except IOError:
|
|
|
|
logs.exception("Can't update history", sys.exc_info())
|
|
|
|
|
|
|
|
def _put_to_history(self, command_script):
|
|
|
|
"""Puts command script to shell history."""
|
|
|
|
history_file_name = self._get_history_file_name()
|
|
|
|
if os.path.isfile(history_file_name):
|
|
|
|
with open(history_file_name, 'a') as history:
|
|
|
|
entry = self._get_history_line(command_script)
|
|
|
|
if six.PY2:
|
|
|
|
history.write(entry.encode('utf-8'))
|
|
|
|
else:
|
|
|
|
history.write(entry)
|