Files
thefuck/thefuck/rules/no_command.py
T
2015-04-21 08:30:48 +02:00

31 lines
833 B
Python

from difflib import get_close_matches
import os
from pathlib import Path
def _safe(path, method):
try:
return getattr(path, method)()
except OSError:
return []
def _get_all_bins():
return [exe.name
for path in os.environ['PATH'].split(':')
for exe in _safe(Path(path), 'iterdir')
if not _safe(exe, 'is_dir')]
def match(command, settings):
return 'not found' in command.stderr and \
bool(get_close_matches(command.script.split(' ')[0],
_get_all_bins()))
def get_new_command(command, settings):
old_command = command.script.split(' ')[0]
new_command = get_close_matches(old_command,
_get_all_bins())[0]
return ' '.join([new_command] + command.script.split(' ')[1:])