From cd3a3cd8236f61c508074eb3537dd995c86aa0a5 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Sat, 26 Aug 2017 05:46:07 +0200 Subject: [PATCH] #682: Implement instant mode aliases for bash and zsh --- thefuck/argument_parser.py | 4 ++++ thefuck/main.py | 7 ++++++- thefuck/shells/bash.py | 18 +++++++++++++++++- thefuck/shells/fish.py | 4 ++-- thefuck/shells/generic.py | 9 +++++++-- thefuck/shells/powershell.py | 4 ++-- thefuck/shells/tcsh.py | 4 ++-- thefuck/shells/zsh.py | 18 +++++++++++++++++- 8 files changed, 57 insertions(+), 11 deletions(-) diff --git a/thefuck/argument_parser.py b/thefuck/argument_parser.py index 8776dd52..695111c1 100644 --- a/thefuck/argument_parser.py +++ b/thefuck/argument_parser.py @@ -25,6 +25,10 @@ class Parser(object): nargs='?', const=get_alias(), help='[custom-alias-name] prints alias for current shell') + self._parser.add_argument( + '--enable-experimental-instant-mode', + action='store_true', + help='enable experimental instant mode, use on your own risk') self._parser.add_argument( '-h', '--help', action='store_true', diff --git a/thefuck/main.py b/thefuck/main.py index e02cc2ba..ff0341f4 100644 --- a/thefuck/main.py +++ b/thefuck/main.py @@ -50,6 +50,11 @@ def main(): elif known_args.command: fix_command(known_args) elif known_args.alias: - print(shell.app_alias(known_args.alias)) + if known_args.enable_experimental_instant_mode: + alias = shell.instant_mode_alias(known_args.alias) + else: + alias = shell.app_alias(known_args.alias) + + print(alias) else: parser.print_usage() diff --git a/thefuck/shells/bash.py b/thefuck/shells/bash.py index b347decc..c8f9f6a8 100644 --- a/thefuck/shells/bash.py +++ b/thefuck/shells/bash.py @@ -1,6 +1,7 @@ import os +from uuid import uuid4 from ..conf import settings -from ..const import ARGUMENT_PLACEHOLDER +from ..const import ARGUMENT_PLACEHOLDER, USER_COMMAND_MARK from ..utils import memoize from .generic import Generic @@ -27,6 +28,21 @@ class Bash(Generic): alter_history=('history -s $TF_CMD;' if settings.alter_history else '')) + def instant_mode_alias(self, alias_name): + if os.environ.get('THEFUCK_INSTANT_MODE'): + return ''' + export PS1="{user_command_mark}$PS1"; + {app_alias} + '''.format(user_command_mark=USER_COMMAND_MARK, + app_alias=self.app_alias(alias_name)) + else: + return ''' + export THEFUCK_INSTANT_MODE=True; + export THEFUCK_OUTPUT_LOG={log}; + script -feq {log}; + exit + '''.format(log='/tmp/thefuck-script-log-{}'.format(uuid4().hex)) + def _parse_alias(self, alias): name, value = alias.replace('alias ', '', 1).split('=', 1) if value[0] == value[-1] == '"' or value[0] == value[-1] == "'": diff --git a/thefuck/shells/fish.py b/thefuck/shells/fish.py index 6d777461..f0a1f4f3 100644 --- a/thefuck/shells/fish.py +++ b/thefuck/shells/fish.py @@ -18,7 +18,7 @@ class Fish(Generic): default.add(alias.strip()) return default - def app_alias(self, fuck): + def app_alias(self, alias_name): if settings.alter_history: alter_history = (' builtin history delete --exact' ' --case-sensitive -- $fucked_up_command\n' @@ -33,7 +33,7 @@ class Fish(Generic): ' if [ "$unfucked_command" != "" ]\n' ' eval $unfucked_command\n{1}' ' end\n' - 'end').format(fuck, alter_history) + 'end').format(alias_name, alter_history) @memoize @cache('.config/fish/config.fish', '.config/fish/functions') diff --git a/thefuck/shells/generic.py b/thefuck/shells/generic.py index fb190fb8..c3b33e6b 100644 --- a/thefuck/shells/generic.py +++ b/thefuck/shells/generic.py @@ -3,6 +3,7 @@ import os import shlex import six from collections import namedtuple +from warnings import warn from ..utils import memoize from ..conf import settings from ..system import Path @@ -32,9 +33,13 @@ class Generic(object): """Prepares command for running in shell.""" return command_script - def app_alias(self, fuck): + def app_alias(self, alias_name): return "alias {0}='eval $(TF_ALIAS={0} PYTHONIOENCODING=utf-8 " \ - "thefuck $(fc -ln -1))'".format(fuck) + "thefuck $(fc -ln -1))'".format(alias_name) + + def instant_mode_alias(self, alias_name): + warn("Instant mode not supported by your shell") + return self.app_alias(alias_name) def _get_history_file_name(self): return '' diff --git a/thefuck/shells/powershell.py b/thefuck/shells/powershell.py index 938b3d4e..17477805 100644 --- a/thefuck/shells/powershell.py +++ b/thefuck/shells/powershell.py @@ -2,8 +2,8 @@ from .generic import Generic, ShellConfiguration class Powershell(Generic): - def app_alias(self, fuck): - return 'function ' + fuck + ' {\n' \ + def app_alias(self, alias_name): + return 'function ' + alias_name + ' {\n' \ ' $history = (Get-History -Count 1).CommandLine;\n' \ ' if (-not [string]::IsNullOrWhiteSpace($history)) {\n' \ ' $fuck = $(thefuck $history);\n' \ diff --git a/thefuck/shells/tcsh.py b/thefuck/shells/tcsh.py index b1967a9b..d8470eba 100644 --- a/thefuck/shells/tcsh.py +++ b/thefuck/shells/tcsh.py @@ -6,10 +6,10 @@ from .generic import Generic class Tcsh(Generic): - def app_alias(self, fuck): + def app_alias(self, alias_name): return ("alias {0} 'setenv TF_ALIAS {0} && " "set fucked_cmd=`history -h 2 | head -n 1` && " - "eval `thefuck ${{fucked_cmd}}`'").format(fuck) + "eval `thefuck ${{fucked_cmd}}`'").format(alias_name) def _parse_alias(self, alias): name, value = alias.split("\t", 1) diff --git a/thefuck/shells/zsh.py b/thefuck/shells/zsh.py index f0e92085..9d307fac 100644 --- a/thefuck/shells/zsh.py +++ b/thefuck/shells/zsh.py @@ -1,7 +1,8 @@ from time import time import os +from uuid import uuid4 from ..conf import settings -from ..const import ARGUMENT_PLACEHOLDER +from ..const import ARGUMENT_PLACEHOLDER, USER_COMMAND_MARK from ..utils import memoize from .generic import Generic @@ -26,6 +27,21 @@ class Zsh(Generic): alter_history=('test -n "$TF_CMD" && print -s $TF_CMD' if settings.alter_history else '')) + def instant_mode_alias(self, alias_name): + if os.environ.get('THEFUCK_INSTANT_MODE'): + return ''' + export PS1="{user_command_mark}$PS1"; + {app_alias} + '''.format(user_command_mark=USER_COMMAND_MARK, + app_alias=self.app_alias(alias_name)) + else: + return ''' + export THEFUCK_INSTANT_MODE=True; + export THEFUCK_OUTPUT_LOG={log}; + script -feq {log}; + exit + '''.format(log='/tmp/thefuck-script-log-{}'.format(uuid4().hex)) + def _parse_alias(self, alias): name, value = alias.split('=', 1) if value[0] == value[-1] == '"' or value[0] == value[-1] == "'":