1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-20 09:39:01 +00:00

added confirmation function in logs.py

This commit is contained in:
ICalhoun 2021-04-05 13:20:20 -04:00
parent 4c7479b3ad
commit 488a33d1b7
3 changed files with 50 additions and 18 deletions

View File

@ -6,17 +6,17 @@ from .. import logs, types, const
from ..conf import settings from ..conf import settings
from ..corrector import get_corrected_commands from ..corrector import get_corrected_commands
from ..exceptions import EmptyCommand from ..exceptions import EmptyCommand
from ..ui import select_command from ..ui import select_command, confirm_command
from ..utils import get_alias, get_all_executables from ..utils import get_alias, get_all_executables
def _get_raw_command(known_args): def _get_raw_command(known_args):
if known_args.force_command: if known_args.force_command:
return known_args.force_command return known_args.force_command
elif not os.environ.get('TF_HISTORY'): elif not os.environ.get("TF_HISTORY"):
return known_args.command return known_args.command
else: else:
history = os.environ['TF_HISTORY'].split('\n')[::-1] history = os.environ["TF_HISTORY"].split("\n")[::-1]
alias = get_alias() alias = get_alias()
executables = get_all_executables() executables = get_all_executables()
for command in history: for command in history:
@ -29,20 +29,24 @@ def _get_raw_command(known_args):
def fix_command(known_args): def fix_command(known_args):
"""Fixes previous command. Used when `thefuck` called without arguments.""" """Fixes previous command. Used when `thefuck` called without arguments."""
settings.init(known_args) settings.init(known_args)
with logs.debug_time('Total'): with logs.debug_time("Total"):
logs.debug(u'Run with settings: {}'.format(pformat(settings))) logs.debug(u"Run with settings: {}".format(pformat(settings)))
raw_command = _get_raw_command(known_args) raw_command = _get_raw_command(known_args)
try: try:
command = types.Command.from_raw_script(raw_command) command = types.Command.from_raw_script(raw_command)
except EmptyCommand: except EmptyCommand:
logs.debug('Empty command, nothing to do') logs.debug("Empty command, nothing to do")
return return
corrected_commands = get_corrected_commands(command) corrected_commands = get_corrected_commands(command)
selected_command = select_command(corrected_commands) selected_command = select_command(corrected_commands)
if selected_command: confirmation = True
if selected_command.script == "reboot":
confirmation = confirm_command("Reboot System?")
if selected_command and confirmation:
selected_command.run(command) selected_command.run(command)
else: else:
sys.exit(1) sys.exit(1)

View File

@ -139,3 +139,14 @@ def version(thefuck_version, python_version, shell_info):
u'The Fuck {} using Python {} and {}\n'.format(thefuck_version, u'The Fuck {} using Python {} and {}\n'.format(thefuck_version,
python_version, python_version,
shell_info)) shell_info))
def confirmation(confirm):
if confirm is True:
sys.stderr.write(u"\n{bold}System Rebooting!{reset}".format(
bold=color(colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL)))
else:
sys.stderr.write(u"\n{bold}Reboot Cancelled{reset}".format(
bold=color(colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL)))

View File

@ -6,6 +6,7 @@ from .exceptions import NoRuleMatched
from .system import get_key from .system import get_key
from .utils import get_alias from .utils import get_alias
from . import logs, const from . import logs, const
from .types import CorrectedCommand
def read_actions(): def read_actions():
@ -14,13 +15,13 @@ def read_actions():
key = get_key() key = get_key()
# Handle arrows, j/k (qwerty), and n/e (colemak) # Handle arrows, j/k (qwerty), and n/e (colemak)
if key in (const.KEY_UP, const.KEY_CTRL_N, 'k', 'e'): if key in (const.KEY_UP, const.KEY_CTRL_N, "k", "e"):
yield const.ACTION_PREVIOUS yield const.ACTION_PREVIOUS
elif key in (const.KEY_DOWN, const.KEY_CTRL_P, 'j', 'n'): elif key in (const.KEY_DOWN, const.KEY_CTRL_P, "j", "n"):
yield const.ACTION_NEXT yield const.ACTION_NEXT
elif key in (const.KEY_CTRL_C, 'q'): elif key in (const.KEY_CTRL_C, "q"):
yield const.ACTION_ABORT yield const.ACTION_ABORT
elif key in ('\n', '\r'): elif key in ("\n", "\r"):
yield const.ACTION_SELECT yield const.ACTION_SELECT
@ -58,20 +59,16 @@ class CommandSelector(object):
def select_command(corrected_commands): def select_command(corrected_commands):
"""Returns: """Returns:
- the first command when confirmation disabled; - the first command when confirmation disabled;
- None when ctrl+c pressed; - None when ctrl+c pressed;
- selected command. - selected command.
:type corrected_commands: Iterable[thefuck.types.CorrectedCommand] :type corrected_commands: Iterable[thefuck.types.CorrectedCommand]
:rtype: thefuck.types.CorrectedCommand | None :rtype: thefuck.types.CorrectedCommand | None
""" """
try: try:
selector = CommandSelector(corrected_commands) selector = CommandSelector(corrected_commands)
except NoRuleMatched: except NoRuleMatched:
logs.failed('No fucks given' if get_alias() == 'fuck' logs.failed("No fucks given" if get_alias() == "fuck" else "Nothing found")
else 'Nothing found')
return return
if not settings.require_confirmation: if not settings.require_confirmation:
@ -82,10 +79,10 @@ def select_command(corrected_commands):
for action in read_actions(): for action in read_actions():
if action == const.ACTION_SELECT: if action == const.ACTION_SELECT:
sys.stderr.write('\n') sys.stderr.write("\n")
return selector.value return selector.value
elif action == const.ACTION_ABORT: elif action == const.ACTION_ABORT:
logs.failed('\nAborted') logs.failed("\nAborted")
return return
elif action == const.ACTION_PREVIOUS: elif action == const.ACTION_PREVIOUS:
selector.previous() selector.previous()
@ -93,3 +90,23 @@ def select_command(corrected_commands):
elif action == const.ACTION_NEXT: elif action == const.ACTION_NEXT:
selector.next() selector.next()
logs.confirm_text(selector.value) logs.confirm_text(selector.value)
def confirm_command(confirmation_text):
"""Returns:
- the first command when confirmation disabled;
- None when ctrl+c pressed;
- selected command.
:type corrected_commands: Iterable[thefuck.types.CorrectedCommand]
:rtype: thefuck.types.CorrectedCommand | None
"""
logs.confirm_text(CorrectedCommand(confirmation_text, None, 0))
for action in read_actions():
if action == const.ACTION_SELECT:
logs.confirmation(True)
return True
elif action == const.ACTION_ABORT:
logs.confirmation(False)
return False