Skip to content

Instantly share code, notes, and snippets.

@adl32x
Created November 9, 2015 16:03
Show Gist options
  • Save adl32x/4e22a8c582cdead3cec6 to your computer and use it in GitHub Desktop.
Save adl32x/4e22a8c582cdead3cec6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os
import fcntl, sys, termios
from difflib import SequenceMatcher
del sys.argv[0]
ARGS = ' '.join(sys.argv)
SKIP = ['cd', '', 'ls', 'cd ..']
class colors:
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
ENDC = '\033[0m'
@staticmethod
def green_text(string):
return "%s%s%s" % (colors.GREEN, string, colors.ENDC)
@staticmethod
def blue_text(string):
return "%s%s%s" % (colors.BLUE, string, colors.ENDC)
@staticmethod
def warn_text(string):
return "%s%s%s" % (colors.WARNING, string, colors.ENDC)
file_input = open(os.path.join(os.path.expanduser('~'), '.bash_history'))
lines = file_input.readlines()
results = set()
strong_results = set()
for line in lines:
if line not in SKIP:
results.add( ( SequenceMatcher(None, line, ARGS).ratio(), line.strip() ) )
to_strong = True
for args in sys.argv:
if args.lower() not in line.lower():
to_strong = False
break
if to_strong:
strong_results.add(line.strip())
results = list(results)
results.sort()
results = results[len(results)-10:]
print_list = list(strong_results) + [element[1] for element in results]
print_list.reverse()
for index, line in enumerate(print_list):
print "%s: %s" % (colors.green_text( str(len(print_list) - index) ), line)
try:
choise = int(raw_input("> "))
command = print_list[len(print_list) - choise]
for char in command:
fcntl.ioctl(sys.stdin, termios.TIOCSTI, char)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment