| commit 3: | f9983aa14ac5 |
| parent 2: | 0118a33819f8 |
| branch: | default |
A little rewrite and stay on error
14 months ago
Changed (Δ302 bytes):
raw changeset »
tll (38 lines added, 52 lines removed)
2 |
2 |
'''Simple launcher''' |
3 |
3 |
|
4 |
4 |
__author__ = "Miki Tebeka <miki.tebeka@gmail.com>" |
5 |
__version__ = "0.3. |
|
5 |
__version__ = "0.3.2" |
|
6 |
6 |
|
7 |
7 |
import Tkinter as tk |
8 |
8 |
from tkFont import Font |
| … | … | @@ -116,37 +116,52 @@ def launch(name, args, aliases): |
116 |
116 |
system("start \"%s\"" % fullname) |
117 |
117 |
|
118 |
118 |
class TLL: |
119 |
def __init__(self, history): |
|
120 |
self.user_cancel = 0 |
|
121 |
|
|
119 |
def __init__(self): |
|
120 |
self.aliases = load_aliases() |
|
121 |
self.history = History() |
|
122 |
self.history.load() |
|
122 |
123 |
self.root = root = tk.Tk() |
123 |
124 |
root.title("TLL") |
124 |
root.bind("<Escape>", |
|
125 |
root.bind("<Escape>", lambda e: self.quit()) |
|
125 |
126 |
font = Font(family="Courier", size=40, weight="bold") |
126 |
127 |
self.command = cmd = tk.Entry(root, width=20, font=font) |
127 |
128 |
cmd.pack(fill=tk.BOTH) |
128 |
cmd.bind("<Return>", lambda e: root.quit()) |
|
129 |
cmd.bind("<Up>", lambda e: self.move(-1)) |
|
130 |
cmd.bind("<Down>", lambda e: self.move(1)) |
|
131 |
||
132 |
|
|
129 |
cmd.bind("<Return>", self.on_enter) |
|
130 |
cmd.bind("<Up>", lambda e: self.on_move(-1)) |
|
131 |
cmd.bind("<Down>", lambda e: self.on_move(1)) |
|
132 |
# Close automatically after 30 seconds |
|
133 |
self.timer = Timer(30, self.quit) |
|
133 |
134 |
|
134 |
135 |
def run(self): |
136 |
self.center_on_screen() |
|
135 |
137 |
self.command.focus() |
138 |
self.timer.start() |
|
139 |
||
136 |
140 |
self.root.mainloop() |
137 |
141 |
|
138 |
def quit(self, event): |
|
139 |
self.user_cancel = 1 |
|
140 |
|
|
142 |
def on_enter(self, event): |
|
143 |
line = self.command.get().strip() |
|
144 |
if not line: |
|
145 |
showerror("TLL Error", "Please enter *something*") |
|
146 |
return |
|
141 |
147 |
|
142 |
|
|
148 |
if " " in line: |
|
149 |
command, args = line.split(" ", 1) |
|
150 |
else: |
|
151 |
command, args = line, "" |
|
152 |
||
153 |
try: |
|
154 |
launch(command, args, self.aliases) |
|
155 |
except ValueError: |
|
156 |
showerror("TLL Error", "Can't launch %s" % command) |
|
157 |
return |
|
158 |
self.quit() |
|
159 |
||
160 |
def on_move(self, step): |
|
143 |
161 |
line = self.history.next(step) |
144 |
162 |
self.command.delete(0, tk.END) |
145 |
163 |
self.command.insert(0, line) |
146 |
164 |
|
147 |
def get(self): |
|
148 |
return self.command.get().strip() |
|
149 |
||
150 |
165 |
# http://mail.python.org/pipermail/tutor/2006-December/051337.html |
151 |
166 |
def center_on_screen(self): |
152 |
167 |
root = self.root |
| … | … | @@ -165,6 +180,10 @@ class TLL: |
165 |
180 |
y = max(y, 0) |
166 |
181 |
root.geometry('+%d+%d' % (x, y)) |
167 |
182 |
|
183 |
def quit(self): |
|
184 |
self.timer.cancel() |
|
185 |
self.root.quit() |
|
186 |
||
168 |
187 |
def set_user_path(): |
169 |
188 |
if "HOME" not in environ: |
170 |
189 |
return |
| … | … | @@ -187,41 +206,8 @@ def main(argv=None): |
187 |
206 |
parser.error("wrong number of arguments") # Will exit |
188 |
207 |
|
189 |
208 |
set_user_path() |
190 |
||
191 |
aliases = load_aliases() |
|
192 |
history = History() |
|
193 |
history.load() |
|
194 |
||
195 |
while 1: |
|
196 |
try: |
|
197 |
history.index = 0 |
|
198 |
ui = TLL(history) |
|
199 |
# Quit after 30 seconds |
|
200 |
timer = Timer(30, ui.quit, (None, )) |
|
201 |
timer.start() |
|
202 |
ui.run() |
|
203 |
timer.cancel() |
|
204 |
||
205 |
if ui.user_cancel: |
|
206 |
raise SystemExit |
|
207 |
||
208 |
command = ui.get() |
|
209 |
if not command: |
|
210 |
showerror("TLL Error", "Please enter *something*") |
|
211 |
continue |
|
212 |
||
213 |
history.update(command) |
|
214 |
history.save() |
|
215 |
||
216 |
if " " in command: |
|
217 |
command, args = command.split(" ", 1) |
|
218 |
else: |
|
219 |
args = "" |
|
220 |
launch(command, args, aliases) |
|
221 |
break |
|
222 |
except ValueError: |
|
223 |
showerror("TLL Error", "Can't launch %s" % command) |
|
224 |
|
|
209 |
ui = TLL() |
|
210 |
ui.run() |
|
225 |
211 |
|
226 |
212 |
if __name__ == "__main__": |
227 |
213 |
main() |
