#591: Add path_from_history rule
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
from collections import Counter
|
||||
import re
|
||||
from thefuck.system import Path
|
||||
from thefuck.utils import (get_valid_history_without_current,
|
||||
memoize, replace_argument)
|
||||
from thefuck.shells import shell
|
||||
|
||||
|
||||
patterns = [r'no such file or directory: (.*)$',
|
||||
r"cannot access '(.*)': No such file or directory",
|
||||
r': (.*): No such file or directory',
|
||||
r"can't cd to (.*)$"]
|
||||
|
||||
|
||||
@memoize
|
||||
def _get_destination(command):
|
||||
for pattern in patterns:
|
||||
found = re.findall(pattern, command.stderr)
|
||||
if found:
|
||||
if found[0] in command.script_parts:
|
||||
return found[0]
|
||||
|
||||
|
||||
def match(command):
|
||||
return bool(_get_destination(command))
|
||||
|
||||
|
||||
def _get_all_absolute_paths_from_history(command):
|
||||
counter = Counter()
|
||||
|
||||
for line in get_valid_history_without_current(command):
|
||||
splitted = shell.split_command(line)
|
||||
|
||||
for param in splitted[1:]:
|
||||
if param.startswith('/') or param.startswith('~'):
|
||||
if param.endswith('/'):
|
||||
param = param[:-1]
|
||||
|
||||
counter[param] += 1
|
||||
|
||||
return (path for path, _ in counter.most_common(None))
|
||||
|
||||
|
||||
def get_new_command(command):
|
||||
destination = _get_destination(command)
|
||||
paths = _get_all_absolute_paths_from_history(command)
|
||||
|
||||
return [replace_argument(command.script, destination, path)
|
||||
for path in paths if path.endswith(destination)
|
||||
and Path(path).expanduser().exists()]
|
||||
|
||||
|
||||
priority = 800
|
||||
+3
-1
@@ -268,7 +268,9 @@ def get_valid_history_without_current(command):
|
||||
from thefuck.shells import shell
|
||||
history = shell.get_history()
|
||||
tf_alias = get_alias()
|
||||
executables = set(get_all_executables())
|
||||
executables = set(get_all_executables())\
|
||||
.union(shell.get_builtin_commands())
|
||||
|
||||
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