mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-30 22:54:14 +00:00 
			
		
		
		
	#682: Implement instant mode aliases for bash and zsh
This commit is contained in:
		| @@ -25,6 +25,10 @@ class Parser(object): | |||||||
|             nargs='?', |             nargs='?', | ||||||
|             const=get_alias(), |             const=get_alias(), | ||||||
|             help='[custom-alias-name] prints alias for current shell') |             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( |         self._parser.add_argument( | ||||||
|             '-h', '--help', |             '-h', '--help', | ||||||
|             action='store_true', |             action='store_true', | ||||||
|   | |||||||
| @@ -50,6 +50,11 @@ def main(): | |||||||
|     elif known_args.command: |     elif known_args.command: | ||||||
|         fix_command(known_args) |         fix_command(known_args) | ||||||
|     elif known_args.alias: |     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: |     else: | ||||||
|         parser.print_usage() |         parser.print_usage() | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| import os | import os | ||||||
|  | from uuid import uuid4 | ||||||
| from ..conf import settings | from ..conf import settings | ||||||
| from ..const import ARGUMENT_PLACEHOLDER | from ..const import ARGUMENT_PLACEHOLDER, USER_COMMAND_MARK | ||||||
| from ..utils import memoize | from ..utils import memoize | ||||||
| from .generic import Generic | from .generic import Generic | ||||||
|  |  | ||||||
| @@ -27,6 +28,21 @@ class Bash(Generic): | |||||||
|             alter_history=('history -s $TF_CMD;' |             alter_history=('history -s $TF_CMD;' | ||||||
|                            if settings.alter_history else '')) |                            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): |     def _parse_alias(self, alias): | ||||||
|         name, value = alias.replace('alias ', '', 1).split('=', 1) |         name, value = alias.replace('alias ', '', 1).split('=', 1) | ||||||
|         if value[0] == value[-1] == '"' or value[0] == value[-1] == "'": |         if value[0] == value[-1] == '"' or value[0] == value[-1] == "'": | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ class Fish(Generic): | |||||||
|             default.add(alias.strip()) |             default.add(alias.strip()) | ||||||
|         return default |         return default | ||||||
|  |  | ||||||
|     def app_alias(self, fuck): |     def app_alias(self, alias_name): | ||||||
|         if settings.alter_history: |         if settings.alter_history: | ||||||
|             alter_history = ('    builtin history delete --exact' |             alter_history = ('    builtin history delete --exact' | ||||||
|                              ' --case-sensitive -- $fucked_up_command\n' |                              ' --case-sensitive -- $fucked_up_command\n' | ||||||
| @@ -33,7 +33,7 @@ class Fish(Generic): | |||||||
|                 '  if [ "$unfucked_command" != "" ]\n' |                 '  if [ "$unfucked_command" != "" ]\n' | ||||||
|                 '    eval $unfucked_command\n{1}' |                 '    eval $unfucked_command\n{1}' | ||||||
|                 '  end\n' |                 '  end\n' | ||||||
|                 'end').format(fuck, alter_history) |                 'end').format(alias_name, alter_history) | ||||||
|  |  | ||||||
|     @memoize |     @memoize | ||||||
|     @cache('.config/fish/config.fish', '.config/fish/functions') |     @cache('.config/fish/config.fish', '.config/fish/functions') | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ import os | |||||||
| import shlex | import shlex | ||||||
| import six | import six | ||||||
| from collections import namedtuple | from collections import namedtuple | ||||||
|  | from warnings import warn | ||||||
| from ..utils import memoize | from ..utils import memoize | ||||||
| from ..conf import settings | from ..conf import settings | ||||||
| from ..system import Path | from ..system import Path | ||||||
| @@ -32,9 +33,13 @@ class Generic(object): | |||||||
|         """Prepares command for running in shell.""" |         """Prepares command for running in shell.""" | ||||||
|         return command_script |         return command_script | ||||||
|  |  | ||||||
|     def app_alias(self, fuck): |     def app_alias(self, alias_name): | ||||||
|         return "alias {0}='eval $(TF_ALIAS={0} PYTHONIOENCODING=utf-8 " \ |         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): |     def _get_history_file_name(self): | ||||||
|         return '' |         return '' | ||||||
|   | |||||||
| @@ -2,8 +2,8 @@ from .generic import Generic, ShellConfiguration | |||||||
|  |  | ||||||
|  |  | ||||||
| class Powershell(Generic): | class Powershell(Generic): | ||||||
|     def app_alias(self, fuck): |     def app_alias(self, alias_name): | ||||||
|         return 'function ' + fuck + ' {\n' \ |         return 'function ' + alias_name + ' {\n' \ | ||||||
|                '    $history = (Get-History -Count 1).CommandLine;\n' \ |                '    $history = (Get-History -Count 1).CommandLine;\n' \ | ||||||
|                '    if (-not [string]::IsNullOrWhiteSpace($history)) {\n' \ |                '    if (-not [string]::IsNullOrWhiteSpace($history)) {\n' \ | ||||||
|                '        $fuck = $(thefuck $history);\n' \ |                '        $fuck = $(thefuck $history);\n' \ | ||||||
|   | |||||||
| @@ -6,10 +6,10 @@ from .generic import Generic | |||||||
|  |  | ||||||
|  |  | ||||||
| class Tcsh(Generic): | class Tcsh(Generic): | ||||||
|     def app_alias(self, fuck): |     def app_alias(self, alias_name): | ||||||
|         return ("alias {0} 'setenv TF_ALIAS {0} && " |         return ("alias {0} 'setenv TF_ALIAS {0} && " | ||||||
|                 "set fucked_cmd=`history -h 2 | head -n 1` && " |                 "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): |     def _parse_alias(self, alias): | ||||||
|         name, value = alias.split("\t", 1) |         name, value = alias.split("\t", 1) | ||||||
|   | |||||||
| @@ -1,7 +1,8 @@ | |||||||
| from time import time | from time import time | ||||||
| import os | import os | ||||||
|  | from uuid import uuid4 | ||||||
| from ..conf import settings | from ..conf import settings | ||||||
| from ..const import ARGUMENT_PLACEHOLDER | from ..const import ARGUMENT_PLACEHOLDER, USER_COMMAND_MARK | ||||||
| from ..utils import memoize | from ..utils import memoize | ||||||
| from .generic import Generic | from .generic import Generic | ||||||
|  |  | ||||||
| @@ -26,6 +27,21 @@ class Zsh(Generic): | |||||||
|             alter_history=('test -n "$TF_CMD" && print -s $TF_CMD' |             alter_history=('test -n "$TF_CMD" && print -s $TF_CMD' | ||||||
|                            if settings.alter_history else '')) |                            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): |     def _parse_alias(self, alias): | ||||||
|         name, value = alias.split('=', 1) |         name, value = alias.split('=', 1) | ||||||
|         if value[0] == value[-1] == '"' or value[0] == value[-1] == "'": |         if value[0] == value[-1] == '"' or value[0] == value[-1] == "'": | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user