1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-20 17:48:41 +00:00
thefuck/thefuck/rules/z_no_command.py

34 lines
915 B
Python
Raw Normal View History

2015-04-21 06:55:47 +02:00
from difflib import get_close_matches
import os
from pathlib import Path
from thefuck.utils import sudo_support
2015-04-21 08:38:52 +02:00
def _safe(fn, fallback):
2015-04-21 08:30:48 +02:00
try:
2015-04-21 08:38:52 +02:00
return fn()
2015-04-21 08:30:48 +02:00
except OSError:
2015-04-21 08:38:52 +02:00
return fallback
2015-04-21 08:30:48 +02:00
2015-04-21 06:55:47 +02:00
def _get_all_bins():
return [exe.name
2015-04-21 08:57:35 +02:00
for path in os.environ.get('PATH', '').split(':')
for exe in _safe(lambda: list(Path(path).iterdir()), [])
2015-04-21 08:38:52 +02:00
if not _safe(exe.is_dir, True)]
2015-04-08 19:00:03 +02:00
@sudo_support
2015-04-08 21:08:35 +02:00
def match(command, settings):
2015-04-21 06:55:47 +02:00
return 'not found' in command.stderr and \
bool(get_close_matches(command.script.split(' ')[0],
_get_all_bins()))
2015-04-08 19:00:03 +02:00
@sudo_support
2015-04-08 21:08:35 +02:00
def get_new_command(command, settings):
2015-04-21 06:55:47 +02:00
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:])