-
Notifications
You must be signed in to change notification settings - Fork 0
/
fewest_moves.py
235 lines (214 loc) · 8.29 KB
/
fewest_moves.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import sublime
import sublime_plugin
import re
import subprocess as sp
from platform import system
from json import loads
from threading import Timer, Thread
from .move_transformer import normalize
phantom_name_line_end = 'line_move_count'
phantom_name_block = 'total_move_count'
startupinfo = None
if 'windows' in system().lower():
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
def is_fewest_moves(view):
return 'source.fm' in view.scope_name(0)
def remove_comments(text):
comment_pattern = re.compile(r'(?://|#).*(?=\n|$)')
text = re.sub(comment_pattern, '', text)
return text
def debounce(wait):
""" Decorator that will postpone a functions
execution until after wait seconds
have elapsed since the last time it was invoked. """
def decorator(fn):
def debounced(*args, **kwargs):
def call_it():
fn(*args, **kwargs)
try:
debounced.t.cancel()
except(AttributeError):
pass
debounced.t = Timer(wait, call_it)
debounced.t.start()
return debounced
return decorator
def get_scramble(view):
firstLine = view.line(sublime.Region(0, 0))
scramble = view.substr(firstLine)
scramble = remove_comments(scramble)
scramble = normalize(scramble)
return scramble
def get_skeleton(view):
sel = view.sel()
selection = sel[0].begin()
expanded = view.expand_by_class(selection, sublime.CLASS_EMPTY_LINE)
skeleton = view.substr(expanded)
skeleton = remove_comments(skeleton)
skeleton = normalize(skeleton)
return skeleton
class CallInsertionFinder(Thread):
def __init__(self, command, input_str, timeout, callback):
self.command = command
self.input_str = input_str
self.timeout = timeout
self.callback = callback
Thread.__init__(self)
def run(self):
result = None
error = None
try:
p = sp.Popen(self.command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, startupinfo=startupinfo)
result, error = p.communicate(self.input_str.encode(), self.timeout)
result = result.decode()
error = error.decode()
except OSError as e:
error = e.strerror
except sp.TimeoutExpired as e:
p.kill()
error = 'Timeout of {} seconds has expired.'.format(e.timeout)
except:
error = 'Unknown error'
self.callback(result, error)
class CountMovesCommand(sublime_plugin.TextCommand):
HTML_TEMPLATE = """
<body>
<style>
html, body {{
background-color: transparent;
color: {foreground};
font-style: {font_style};
margin: 0;
padding: 0;
}}
body {{
margin-left: {padding};
}}
span {{
color: color({foreground} blend(var(--foreground) 90%));
text-decoration: none;
}}
</style>
<span>{text}</span>
</body>
"""
def run(self, edit):
view = self.view
view.erase_phantoms(phantom_name_line_end)
view.erase_phantoms(phantom_name_block)
pattern = re.compile("([URFDLB]w?['2]?)")
region = sublime.Region(0, view.size())
lines = view.lines(region)
lineNumber = 0
total = 0
previousLine = None
for line in lines:
lineNumber += 1
text = view.substr(line)
text = remove_comments(text)
count = len(re.findall(pattern, text))
if count > 0:
moves = ('({} move)' if count == 1 else '({} moves)').format(count)
view.add_phantom(phantom_name_line_end, sublime.Region(line.end(), line.end()), self.HTML_TEMPLATE.format(foreground='#7f7c6a', font_style='italic', padding=20, text=moves), sublime.LAYOUT_INLINE)
total += count
if text == '':
if lineNumber > 2 and total > 0:
moves = ('(total: {} move)' if total == 1 else '(total: {} moves)').format(total)
view.add_phantom(phantom_name_block, previousLine, self.HTML_TEMPLATE.format(foreground='#7f7c6a', font_style='italic', padding=0, text=moves), sublime.LAYOUT_BLOCK)
total = 0
previousLine = line
if lineNumber > 2 and total > 0:
moves = ('(total: {} move)' if total == 1 else '(total: {} moves)').format(total)
view.add_phantom(phantom_name_block, previousLine, self.HTML_TEMPLATE.format(foreground='#7f7c6a', font_style='italic', padding=0, text=moves), sublime.LAYOUT_BLOCK)
total = 0
def is_enabled(self):
return is_fewest_moves(self.view)
class ShowSolutionCommand(sublime_plugin.TextCommand):
def run(self, edit, text):
self.view.insert(edit, 0, text)
self.view.set_read_only(True)
class FindInsertionCommand(sublime_plugin.TextCommand):
running = False
insertion_finder = 'insertionfinder'
@property
def check_cycles(self):
return [self.insertion_finder] + ['-v', '--json']
@property
def find_insertion(self):
return [self.insertion_finder] + ['-s', '--all-algs']
def run(self, edit):
view = self.view
settings = view.settings()
scramble = get_scramble(view)
skeleton = get_skeleton(view)
input_str = '\n'.join([scramble, skeleton])
self.insertion_finder = settings.get('insertion_finder', self.insertion_finder)
try:
p = sp.Popen(self.check_cycles, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, startupinfo=startupinfo)
except OSError as e:
sublime.error_message(e.strerror)
return
except:
sublime.error_message('Some error occurred')
return
result, error = p.communicate(input_str.encode())
if error:
sublime.error_message(error.decode())
return
result = result.decode()
result = loads(result)
total_cycles = result['corner_cycles'] + result['edge_cycles'] + result['center_cycles']
if result['parity']:
total_cycles += 1
max_cycles = settings.get('max_cycles', 4)
if max_cycles != 0 and total_cycles > max_cycles:
sublime.error_message('Too many cycles: {}'.format(total_cycles))
return
command = self.find_insertion[:]
algs_dir = settings.get('insertion_finder_algs_dir', False)
if algs_dir is not False:
command += ['--algs-dir', algs_dir]
max_threads = settings.get('max_threads', 2)
command += ['-j' + (str(max_threads) if max_threads != 0 else '')]
timeout = settings.get('insertion_finder_timeout', 300)
t = CallInsertionFinder(command, input_str, timeout, self.handle_result)
self.running = True
self.show_running(0, 1)
t.start()
def show_running(self, i, dir):
before = i % 8
after = 7 - before
dir = -1 if not after else dir
dir = 1 if not before else dir
i += dir
status_key = 'insertion_finder'
self.view.set_status(status_key, 'Finding insertions [{}={}]'.format(' ' * before, ' ' * after))
if self.running:
sublime.set_timeout_async(lambda: self.show_running(i, dir), 100)
else:
self.view.erase_status(status_key)
def handle_result(self, result, error):
self.running = False
if error:
sublime.error_message(error)
return
resultView = sublime.active_window().new_file()
resultView.set_scratch(True)
resultView.run_command('show_solution', {"text": result})
def is_enabled(self):
return is_fewest_moves(self.view)
class FewestMovesEventListener(sublime_plugin.EventListener):
def on_activated_async(self, view):
self.run_plugin(view)
def on_load_async(self, view):
self.run_plugin(view)
def on_modified_async(self, view):
view.erase_phantoms(phantom_name_line_end)
self.run_plugin(view)
@debounce(1)
def run_plugin(self, view):
if is_fewest_moves(view):
view.run_command('count_moves')
view.run_command('draw_scramble')