-
Notifications
You must be signed in to change notification settings - Fork 4
/
scheduler.py
78 lines (63 loc) · 2.42 KB
/
scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#coding: utf8
#################################### IMPORTS ###################################
# Std Libs
import functools
import textwrap
# Sublime Libs
import sublime
import sublime_plugin
################################### CONSTANTS ##################################
class Delay(int): pass
class Pause: pass
class Callback: pass
class AwaitCallback: pass
class Cancel: pass
class Finish: pass
################################### SCHEDULER ##################################
def yields_from(f):
@functools.wraps(f)
def run(*args):
routine = f(*args)
def routine_send(v):
try: return routine.send(v)
except StopIteration: return Finish
def routine_throw(e=StopIteration):
try: return routine.throw(e)
except StopIteration: return Finish
def my_next(yielded=None):
if yielded is None:
try: yielded = next(routine)
except StopIteration:return
if yielded is None:
my_next()
elif yielded is Callback:
my_next(routine_send(lambda v: my_next(routine_send(v))))
elif yielded is Cancel:
my_next(routine_send(lambda: my_next(routine_throw())))
elif isinstance(yielded, Delay):
sublime.set_timeout(my_next, yielded)
elif yielded in (AwaitCallback, Finish):
return
my_next()
return run
#################################### HELPERS ###################################
def input_panel(caption="Input:",
initial_text="",
cancel=Finish ):
panel = sublime.active_window().show_input_panel (
caption, '', (yield Callback), None, (yield Cancel))
panel.run_command('insert_snippet', dict(contents=initial_text))
try: return (yield AwaitCallback)
except StopIteration:
if cancel is Finish: yield Finish
else: return None
def quick_panel(items, flags=0, selected_index=-1, cancel=Finish):
show_panel = sublime.active_window().show_quick_panel
show_panel( items=items, on_select=(yield Callback), flags=flags,
selected_index=selected_index )
ix = (yield AwaitCallback)
if ix == -1 and cancel is Finish:
yield Finish
else:
return (-1, None) if ix == -1 else (ix, items[ix])
################################################################################