Last active
August 29, 2015 14:05
-
-
Save robertely/92d3b1d1c5cc2b354895 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from gi.repository import Gtk, Gdk | |
import ConfigParser | |
from subprocess import Popen | |
from glob import glob | |
from difflib import get_close_matches | |
from pprint import pprint | |
### | |
# | |
# Gnome do poorly made in <100 lines. | |
# I uses a simple lenvenstine distance instead of real auto complete | |
# Esc closes it. | |
# | |
## | |
paths = ['/usr/share/applications/', '$HOME/.local/share/applications/'] | |
class MyWindow(Gtk.Window): | |
def __init__(self): | |
Gtk.Window.__init__(self, title="Herp") | |
Gtk.Window.set_decorated(self, False) | |
Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER) | |
self.searchbox = Gtk.Entry() | |
# self.button.connect("clicked", self.on_button_clicked) | |
self.searchbox.connect('changed', self.on_changed) | |
self.searchbox.connect('changed', self.on_changed) | |
self.searchbox.connect("key-release-event", self.on_key_release) | |
self.add(self.searchbox) | |
def on_changed(self, widget): | |
text = widget.get_text().strip() | |
print '\n' + text | |
pprint(get_close_matches(text, applications.list, 10, cutoff=.01)) | |
def on_key_release(self, widget, ev, data=None): | |
if ev.keyval == Gdk.KEY_Escape: | |
exit(0) | |
if ev.keyval == Gdk.KEY_Return: | |
applications.execute(get_close_matches(widget.get_text().strip(), applications.list, 10, cutoff=.01)[0]) | |
exit(0) | |
class applications: | |
def __init__(self, paths): | |
self.paths = paths | |
self.files = self.__filelist() | |
self.apps = {} | |
for file in self.files: | |
try: | |
Config = ConfigParser.ConfigParser() | |
Config.read(file) | |
name = Config.get('Desktop Entry', 'name') | |
self.apps[name] = {'name': name, | |
'path': file, | |
'type': Config.get('Desktop Entry', 'type'), | |
'exec': Config.get('Desktop Entry', 'exec'), | |
'icon': Config.get('Desktop Entry', 'icon')} | |
except: | |
continue | |
self.list = self.apps.keys() | |
def __filelist(self): | |
result = [] | |
for path in self.paths: | |
result = result + glob(path + '*.desktop') | |
return result | |
def execute(self, name): | |
Popen(self.apps[name]['exec'], shell=True) | |
applications = applications(paths) | |
win = MyWindow() | |
win.connect("delete-event", Gtk.main_quit) | |
win.show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment