Add command not found handler

This commit is contained in:
nvbn
2015-04-08 19:00:03 +02:00
parent 9b135b67d0
commit 8e5cc9d73a
5 changed files with 79 additions and 1 deletions
+1
View File
@@ -2,6 +2,7 @@ from collections import namedtuple
from imp import load_source
from pathlib import Path
from os.path import expanduser
from os import environ
from subprocess import Popen, PIPE
import sys
+23
View File
@@ -0,0 +1,23 @@
from subprocess import Popen, PIPE
import re
def _get_output(command):
name = command.script.split(' ')[command.script.startswith('sudo')]
check_script = '/usr/lib/command-not-found {}'.format(name)
result = Popen(check_script, shell=True, stderr=PIPE)
return result.stderr.read().decode()
def match(command):
output = _get_output(command)
return "No command" in output and "from package" in output
def get_new_command(command):
output = _get_output(command)
broken_name = re.findall(r"No command '([^']*)' found",
output)[0]
fixed_name = re.findall(r"Command '([^']*)' from package",
output)[0]
return command.script.replace(broken_name, fixed_name)