Commits

Miki Tebeka  committed f9983aa

A little rewrite and stay on error

  • Participants
  • Parent commits 0118a33

Comments (0)

Files changed (1)

 '''Simple launcher'''
 
 __author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
-__version__ = "0.3.1"
+__version__ = "0.3.2"
 
 import Tkinter as tk
 from tkFont import Font
         system("start \"%s\"" % fullname)
 
 class TLL:
-    def __init__(self, history):
-        self.user_cancel = 0
-        self.history = history
+    def __init__(self):
+        self.aliases = load_aliases()
+        self.history = History()
+        self.history.load()
         self.root = root = tk.Tk()
         root.title("TLL")
-        root.bind("<Escape>", self.quit)
+        root.bind("<Escape>", lambda e: self.quit())
         font = Font(family="Courier", size=40, weight="bold")
         self.command = cmd = tk.Entry(root, width=20, font=font)
         cmd.pack(fill=tk.BOTH)
-        cmd.bind("<Return>", lambda e: root.quit())
-        cmd.bind("<Up>", lambda e: self.move(-1))
-        cmd.bind("<Down>", lambda e: self.move(1))
-
-        self.center_on_screen()
+        cmd.bind("<Return>", self.on_enter)
+        cmd.bind("<Up>", lambda e: self.on_move(-1))
+        cmd.bind("<Down>", lambda e: self.on_move(1))
+        # Close automatically after 30 seconds
+        self.timer = Timer(30, self.quit)
 
     def run(self):
+        self.center_on_screen()
         self.command.focus()
+        self.timer.start()
+
         self.root.mainloop()
 
-    def quit(self, event):
-        self.user_cancel = 1
-        self.root.quit()
+    def on_enter(self, event):
+        line = self.command.get().strip()
+        if not line:
+            showerror("TLL Error", "Please enter *something*")
+            return
 
-    def move(self, step):
+        if " " in line:
+            command, args = line.split(" ", 1)
+        else:
+            command, args = line, ""
+
+        try:
+            launch(command, args, self.aliases)
+        except ValueError:
+            showerror("TLL Error", "Can't launch %s" % command)
+            return
+        self.quit()
+
+    def on_move(self, step):
         line = self.history.next(step)
         self.command.delete(0, tk.END)
         self.command.insert(0, line)
 
-    def get(self):
-        return self.command.get().strip()
-
     # http://mail.python.org/pipermail/tutor/2006-December/051337.html
     def center_on_screen(self):
         root = self.root
         y = max(y, 0)
         root.geometry('+%d+%d' % (x, y))
 
+    def quit(self):
+        self.timer.cancel()
+        self.root.quit()
+
 def set_user_path():
     if "HOME" not in environ:
         return
         parser.error("wrong number of arguments") # Will exit
 
     set_user_path()
-
-    aliases = load_aliases()
-    history = History()
-    history.load()
-
-    while 1:
-        try:
-            history.index = 0
-            ui = TLL(history)
-            # Quit after 30 seconds
-            timer = Timer(30, ui.quit, (None, ))
-            timer.start()
-            ui.run()
-            timer.cancel()
-
-            if ui.user_cancel:
-                raise SystemExit
-
-            command = ui.get()
-            if not command:
-                showerror("TLL Error", "Please enter *something*")
-                continue
-
-            history.update(command)
-            history.save()
-
-            if " " in command:
-                command, args = command.split(" ", 1)
-            else:
-                args = ""
-            launch(command, args, aliases)
-            break
-        except ValueError:
-            showerror("TLL Error", "Can't launch %s" % command)
-            raise SystemExit
+    ui = TLL()
+    ui.run()
 
 if __name__ == "__main__":
     main()