1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-19 17:18:42 +00:00

Make fish like bash/zsh

This commit is contained in:
David 2018-01-06 22:58:37 +00:00
parent 7c858fadb3
commit 5d31dab7bc
2 changed files with 36 additions and 32 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import pytest import pytest
from thefuck.shells import Fish from thefuck.shells import Fish
@ -11,13 +12,15 @@ class TestFish(object):
return Fish() return Fish()
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def Popen(self, mocker): def shell_aliases(self):
mock = mocker.patch('thefuck.shells.fish.Popen') os.environ['TF_SHELL_ALIASES'] = (
mock.return_value.stdout.read.side_effect = [( 'alias fish_key_reader /usr/bin/fish_key_reader|alias g git')
b'cd\nfish_config\nfuck\nfunced\nfuncsave\ngrep\nhistory\nll\nls\n'
b'man\nmath\npopd\npushd\nruby'), @pytest.fixture(autouse=True)
b'alias fish_key_reader /usr/bin/fish_key_reader\nalias g git'] def shell_functions(self):
return mock os.environ['TF_SHELL_FUNCTIONS'] = (
'cd|fish_config|fuck|funced|funcsave|grep|history|ll|ls|'
'man|math|popd|pushd|ruby')
@pytest.mark.parametrize('key, value', [ @pytest.mark.parametrize('key, value', [
('TF_OVERRIDDEN_ALIASES', 'cut,git,sed'), # legacy ('TF_OVERRIDDEN_ALIASES', 'cut,git,sed'), # legacy
@ -75,7 +78,10 @@ class TestFish(object):
assert 'function fuck' in shell.app_alias('fuck') assert 'function fuck' in shell.app_alias('fuck')
assert 'function FUCK' in shell.app_alias('FUCK') assert 'function FUCK' in shell.app_alias('FUCK')
assert 'thefuck' in shell.app_alias('fuck') assert 'thefuck' in shell.app_alias('fuck')
assert 'TF_SHELL=fish' in shell.app_alias('fuck') assert 'TF_SHELL=fish TF_SHELL_ALIASES=(string join \| (alias))' in \
shell.app_alias('fuck')
assert 'TF_SHELL_FUNCTIONS=(string join \| (functions)) TF_ALIAS=fuck' in \
shell.app_alias('fuck')
assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck') assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck') assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')

View File

@ -1,31 +1,11 @@
from subprocess import Popen, PIPE
from time import time from time import time
import os import os
import sys import sys
import six import six
from .. import logs from .. import logs
from ..conf import settings from ..conf import settings
from ..utils import DEVNULL, cache
from .generic import Generic from .generic import Generic
from ..utils import memoize
@cache('~/.config/fish/config.fish', '~/.config/fish/functions')
def _get_functions(overridden):
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}
@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):
@ -47,17 +27,35 @@ class Fish(Generic):
# It is VERY important to have the variables declared WITHIN the alias # It is VERY important to have the variables declared WITHIN the alias
return ('function {0} -d "Correct your previous console command"\n' return ('function {0} -d "Correct your previous console command"\n'
' set -l fucked_up_command $history[1]\n' ' set -l fucked_up_command $history[1]\n'
' env TF_SHELL=fish TF_ALIAS={0} PYTHONIOENCODING=utf-8' ' env TF_SHELL=fish TF_SHELL_ALIASES=(string join \| (alias))'
' TF_SHELL_FUNCTIONS=(string join \| (functions))'
' TF_ALIAS={0} PYTHONIOENCODING=utf-8'
' thefuck $fucked_up_command | read -l unfucked_command\n' ' thefuck $fucked_up_command | read -l unfucked_command\n'
' if [ "$unfucked_command" != "" ]\n' ' if [ "$unfucked_command" != "" ]\n'
' eval $unfucked_command\n{1}' ' eval $unfucked_command\n{1}'
' end\n' ' end\n'
'end').format(alias_name, alter_history) 'end').format(alias_name, alter_history)
def _get_functions(self, overridden):
functions = os.environ.get('TF_SHELL_FUNCTIONS', '').split('|')
logs.debug(functions)
return {func: func for func in functions if func not in overridden and func != ''}
def _get_aliases(self, overridden):
aliases = {}
alias_out = os.environ.get('TF_SHELL_ALIASES', '').split('|')
logs.debug(alias_out)
for alias in [alias for alias in alias_out if alias != '']:
name, value = alias.replace('alias ', '', 1).split(' ', 1)
if name not in overridden:
aliases[name] = value
return aliases
@memoize
def get_aliases(self): def get_aliases(self):
overridden = self._get_overridden_aliases() overridden = self._get_overridden_aliases()
functions = _get_functions(overridden) functions = self._get_functions(overridden)
raw_aliases = _get_aliases(overridden) raw_aliases = self._get_aliases(overridden)
functions.update(raw_aliases) functions.update(raw_aliases)
return functions return functions