#614: Add --repeat option

This commit is contained in:
Vladimir Iakovlev
2017-03-28 18:09:38 +02:00
parent c3eca8234a
commit cfa831c88d
15 changed files with 97 additions and 29 deletions
+15 -5
View File
@@ -1,5 +1,5 @@
import sys
from argparse import ArgumentParser
from argparse import ArgumentParser, SUPPRESS
from .const import ARGUMENT_PLACEHOLDER
from .utils import get_alias
@@ -20,17 +20,27 @@ class Parser(object):
'-h', '--help',
action='store_true',
help='show this help message and exit')
self._parser.add_argument(
group = self._parser.add_mutually_exclusive_group()
group.add_argument(
'-y', '--yes',
action='store_true',
help='execute fixed command without confirmation')
group.add_argument(
'-r', '--repeat',
action='store_true',
help='repeat on failure')
self._parser.add_argument(
'-d', '--debug',
action='store_true',
help='enable debug output')
self._parser.add_argument('command',
nargs='*',
help='command that should be fixed')
self._parser.add_argument(
'--force-command',
action='store',
help=SUPPRESS)
self._parser.add_argument(
'command',
nargs='*',
help='command that should be fixed')
def _get_arguments(self, argv):
if ARGUMENT_PLACEHOLDER in argv:
+2
View File
@@ -121,6 +121,8 @@ class Settings(dict):
from_args['require_confirmation'] = not args.yes
if args.debug:
from_args['debug'] = args.debug
if args.repeat:
from_args['repeat'] = args.repeat
return from_args
+3 -1
View File
@@ -34,6 +34,7 @@ DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
'wait_slow_command': 15,
'slow_commands': ['lein', 'react-native', 'gradle',
'./gradlew', 'vagrant'],
'repeat': False,
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}}
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
@@ -46,7 +47,8 @@ ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
'THEFUCK_HISTORY_LIMIT': 'history_limit',
'THEFUCK_ALTER_HISTORY': 'alter_history',
'THEFUCK_WAIT_SLOW_COMMAND': 'wait_slow_command',
'THEFUCK_SLOW_COMMANDS': 'slow_commands'}
'THEFUCK_SLOW_COMMANDS': 'slow_commands',
'THEFUCK_REPEAT': 'repeat'}
SETTINGS_HEADER = u"""# The Fuck settings file
#
+3 -1
View File
@@ -20,9 +20,11 @@ def fix_command(known_args):
settings.init(known_args)
with logs.debug_time('Total'):
logs.debug(u'Run with settings: {}'.format(pformat(settings)))
raw_command = ([known_args.force_command] if known_args.force_command
else known_args.command)
try:
command = types.Command.from_raw_script(known_args.command)
command = types.Command.from_raw_script(raw_command)
except EmptyCommand:
logs.debug('Empty command, nothing to do')
return
+3
View File
@@ -66,6 +66,9 @@ class Fish(Generic):
def and_(self, *commands):
return u'; and '.join(commands)
def or_(self, *commands):
return u'; or '.join(commands)
def how_to_configure(self):
return self._create_shell_configuration(
content=u"eval (thefuck --alias | tr '\n' ';')",
+3
View File
@@ -66,6 +66,9 @@ class Generic(object):
def and_(self, *commands):
return u' && '.join(commands)
def or_(self, *commands):
return u' || '.join(commands)
def how_to_configure(self):
return
+19 -1
View File
@@ -9,6 +9,7 @@ from .shells import shell
from .conf import settings
from .const import DEFAULT_PRIORITY, ALL_ENABLED
from .exceptions import EmptyCommand
from .utils import get_alias
class Command(object):
@@ -276,6 +277,22 @@ class CorrectedCommand(object):
return u'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
self.script, self.side_effect, self.priority)
def _get_script(self):
"""Returns fixed commands script.
If `settings.repeat` is `True`, appends command with second attempt
of running fuck in case fixed command fails again.
"""
if settings.repeat:
repeat_fuck = '{} --repeat {}--force-command {}'.format(
get_alias(),
'--debug ' if settings.debug else '',
shell.quote(self.script))
return shell.or_(self.script, repeat_fuck)
else:
return self.script
def run(self, old_cmd):
"""Runs command from rule for passed command.
@@ -289,4 +306,5 @@ class CorrectedCommand(object):
# This depends on correct setting of PYTHONIOENCODING by the alias:
logs.debug(u'PYTHONIOENCODING: {}'.format(
os.environ.get('PYTHONIOENCODING', '!!not-set!!')))
print(self.script)
print(self._get_script())