#475: Try to use already used executable in no_command
This commit is contained in:
@@ -1,37 +1,15 @@
|
||||
from difflib import get_close_matches
|
||||
from thefuck.shells import shell
|
||||
from thefuck.utils import get_closest, memoize, get_all_executables, get_alias
|
||||
|
||||
|
||||
def _not_corrected(history, tf_alias):
|
||||
"""Returns all lines from history except that comes before `fuck`."""
|
||||
previous = None
|
||||
for line in history:
|
||||
if previous is not None and line != tf_alias:
|
||||
yield previous
|
||||
previous = line
|
||||
if history:
|
||||
yield history[-1]
|
||||
|
||||
|
||||
@memoize
|
||||
def _history_of_exists_without_current(command):
|
||||
history = shell.get_history()
|
||||
tf_alias = get_alias()
|
||||
executables = get_all_executables()
|
||||
return [line for line in _not_corrected(history, tf_alias)
|
||||
if not line.startswith(tf_alias) and not line == command.script
|
||||
and line.split(' ')[0] in executables]
|
||||
from thefuck.utils import get_closest, get_valid_history_without_current
|
||||
|
||||
|
||||
def match(command):
|
||||
return len(get_close_matches(command.script,
|
||||
_history_of_exists_without_current(command)))
|
||||
get_valid_history_without_current(command)))
|
||||
|
||||
|
||||
def get_new_command(command):
|
||||
return get_closest(command.script,
|
||||
_history_of_exists_without_current(command))
|
||||
get_valid_history_without_current(command))
|
||||
|
||||
|
||||
priority = 9999
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from difflib import get_close_matches
|
||||
from thefuck.utils import get_all_executables
|
||||
from thefuck.utils import get_all_executables, \
|
||||
get_valid_history_without_current, get_closest
|
||||
from thefuck.specific.sudo import sudo_support
|
||||
|
||||
|
||||
@@ -11,10 +12,29 @@ def match(command):
|
||||
get_all_executables())))
|
||||
|
||||
|
||||
def _get_used_executables(command):
|
||||
for script in get_valid_history_without_current(command):
|
||||
yield script.split(' ')[0]
|
||||
|
||||
|
||||
@sudo_support
|
||||
def get_new_command(command):
|
||||
old_command = command.script_parts[0]
|
||||
new_cmds = get_close_matches(old_command, get_all_executables(), cutoff=0.1)
|
||||
|
||||
# One from history:
|
||||
already_used = get_closest(
|
||||
old_command, _get_used_executables(command),
|
||||
fallback_to_first=False)
|
||||
if already_used:
|
||||
new_cmds = [already_used]
|
||||
else:
|
||||
new_cmds = []
|
||||
|
||||
# Other from all executables:
|
||||
new_cmds += [cmd for cmd in get_close_matches(old_command,
|
||||
get_all_executables())
|
||||
if cmd not in new_cmds]
|
||||
|
||||
return [' '.join([new_command] + command.script_parts[1:])
|
||||
for new_command in new_cmds]
|
||||
|
||||
|
||||
@@ -269,3 +269,24 @@ def get_installation_info():
|
||||
|
||||
def get_alias():
|
||||
return os.environ.get('TF_ALIAS', 'fuck')
|
||||
|
||||
|
||||
@memoize
|
||||
def get_valid_history_without_current(command):
|
||||
def _not_corrected(history, tf_alias):
|
||||
"""Returns all lines from history except that comes before `fuck`."""
|
||||
previous = None
|
||||
for line in history:
|
||||
if previous is not None and line != tf_alias:
|
||||
yield previous
|
||||
previous = line
|
||||
if history:
|
||||
yield history[-1]
|
||||
|
||||
from thefuck.shells import shell
|
||||
history = shell.get_history()
|
||||
tf_alias = get_alias()
|
||||
executables = get_all_executables()
|
||||
return [line for line in _not_corrected(history, tf_alias)
|
||||
if not line.startswith(tf_alias) and not line == command.script
|
||||
and line.split(' ')[0] in executables]
|
||||
|
||||
Reference in New Issue
Block a user