-
Notifications
You must be signed in to change notification settings - Fork 1
/
LyricEditor.py
303 lines (250 loc) · 8.55 KB
/
LyricEditor.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import sublime
import sublime_plugin
import time
import datetime
import re
from functools import partial
status = {
'idle': 'doing nothing',
'tagging': 'tagging...',
'pause': 'paused...',
'playing': 'playing...',
'done': 'done',
}
settings = None
def log(line):
if settings and settings.get('debug', False):
print(line)
def view():
return sublime.active_window().active_view()
def status_message(name):
return '[lyric editor] ' + status.get(name)
def set_status(name):
if name not in status.keys():
name = 'idle'
log('set status to:%s' % name)
return view().set_status('lyric editor', status_message(name))
def get_status():
message = view().get_status('lyric editor')
for s in status:
if status_message(s) == message:
return s
return None
class Timer():
_start = None
_pause = None
def reset(self):
self._start = None
self._pause = None
def start(self):
self._start = time.time()
self._pause = None
log('timer: start %s' % self._start)
def start_at(self, at):
self._start = time.time() - float(at)
self._pause = None
log('timer: start at %s' % self._start)
def started(self):
return self._start is not None
def delta(self):
return time.time() - self._start
def pause(self):
# multiple pause only count once
if not self._pause:
self._pause = time.time()
log('timer: pause at %s' % self._pause)
def resume(self):
if not self._pause:
sublime.error_message('already running')
return False
self._start = time.time() - (self._pause - self._start)
self._pause = None
t = Timer()
def timer():
return globals()['t']
def plugin_loaded():
globals()['settings'] = sublime.load_settings('LyricEditor.sublime-settings')
set_status('idle')
class LyricInsertMetaCommand(sublime_plugin.TextCommand):
def run(self, edit):
metas = settings.get('meta', {})
log('meta:%s' % metas)
snippet = ''
i = 1
for name, placeholder in metas.items():
snippet += '[%s: ${%d:%s}]\n' % (name, i, placeholder)
i += 1
if snippet:
v = view()
v.run_command('move_to', {'to': 'bof'})
v.run_command('insert_snippet', {'contents': snippet})
def toggle_distraction_free(on):
options = {
'line_numbers': False,
'gutter': True,
'draw_centered': True,
'scroll_past_end': True,
'highlight_line': True
}
settings = view().settings()
if not on:
for o in options:
settings.erase(o)
else:
for o, v in options.items():
settings.set(o, v)
class StartAtInputHandler(sublime_plugin.TextInputHandler):
pattern = re.compile(r'^\d+:\d\d(.\d\d)?$')
def initial_text(self):
tags = []
view().find_all(r'^\[(\d+:\d\d\.\d\d.*)\]', 0, '$1', tags)
return tags.pop() if tags else '00:00.00'
def placeholder(self):
return 'something like 55:42.31'
def validate(self, text):
return bool(self.pattern.match(text))
def preview(self, text):
if text == '':
return None
if self.pattern.match(text):
return text
return 'invalid'
class LyricStartAtCommand(sublime_plugin.TextCommand):
def run(self, edit, start_at):
minutes, seconds = start_at.split(':')
timer().start_at(int(minutes) * 60 + float(seconds))
toggle_distraction_free(True)
set_status('tagging')
def input(self, args):
return StartAtInputHandler()
def input_description(self):
return 'Set Start Time'
class LyricStartCommand(sublime_plugin.TextCommand):
def run(self, edit):
if (timer().started()):
reset = sublime.ok_cancel_dialog('Tagging already started, reset timer?', 'Reset')
if not reset:
return False
toggle_distraction_free(True)
timer().start()
set_status('tagging')
def make_tag():
passed = timer().delta()
minutes, seconds = divmod(passed, 60)
return '[%.2d:%05.2f]' % (minutes, seconds)
class LyricTagAndNextCommand(sublime_plugin.TextCommand):
def run(self, edit):
print('status:%s' % get_status())
v = view()
if get_status() == 'pause':
timer().resume()
set_status('tagging')
if get_status() != 'tagging':
sublime.error_message("[lyric editor]: please start first")
return False
# find next non tag line
pattern = r'^(?!\[.+:.*\]).*$'
if settings.get('skip_empty_line', True):
log('skip empty line')
pattern = r'^(?!\[.+:.+\]).+$'
next_line = v.find(pattern, 0)
log('next line:%s' % next_line)
if next_line.a < 0:
sublime.message_dialog("Tagging complete")
set_status('done')
timer().reset()
toggle_distraction_free(False)
else:
v.sel().clear()
v.sel().add(next_line.a)
v.insert(edit, next_line.a, make_tag())
class LyricTogglePauseResumeCommand(sublime_plugin.TextCommand):
def run(self, edit):
v = view()
status = get_status()
if status == 'tagging':
timer().pause()
set_status('pause')
if status == 'pause':
timer().resume()
set_status('tagging')
class LyricOffsetCommand(sublime_plugin.TextCommand):
def run(self, edit, updown='up'):
log('--offseting--')
v = view()
step = float(settings.get('offset_step', 0.5))
sels = v.sel()
tag_pattern = re.compile(r'^\[(\d+:\d\d\.\d\d)\]')
for s in sels:
for line in v.lines(s):
log(' line:%s' % v.substr(line))
match = tag_pattern.match(v.substr(line))
if not match:
continue
tag = match.group(1)
log(' old tag:%s' % tag)
minute, second = tag.split(':')
if updown == 'up':
second = float(second) + step
else:
second = float(second) - step
minute_plus, new_second = divmod(second, 60)
new_minute = float(minute) + minute_plus
new_tag = '%.2d:%05.2f' % (new_minute, new_second)
log(' new tag:%s' % new_tag)
v.replace(edit, sublime.Region(line.a+1, line.a+len(tag)+1), new_tag)
class LyricPlayCommand(sublime_plugin.TextCommand):
def run(self, edit):
if get_status() == 'playing':
sublime.error_message('Already playing...')
return False
v = view()
tags = []
tag_regions = v.find_all(r'^\[(\d+:\d\d\.\d\d)\]', 0, '$1', tags)
log('tags:%s' % tags)
if not tags:
sublime.error_message('No time tag found')
return False
v.set_read_only(True)
toggle_distraction_free(True)
set_status('playing')
i = 0
for tag in tags:
minutes, seconds = tag.split(':')
timeout = (int(minutes)*60 + float(seconds)) * 1000
log('timeout:%s' % timeout)
is_last = (len(tags) - 1) == i
roll = partial(self.highlight, v, tag_regions[i].a, is_last)
sublime.set_timeout_async(
partial(
self.highlight,
v,
partial(
v.run_command,
'lyric_roll_next',
{'pos': tag_regions[i].a, 'is_last': is_last}
)
),
timeout)
i += 1
def highlight(self, v, roll):
if v.is_valid():
roll()
class EscapePlayContext(sublime_plugin.EventListener):
def on_query_context(self, view, key, op, operand, match_all):
if not key.startswith('lyric_editor'):
return None
return get_status() == 'playing'
class LyricRollNextCommand(sublime_plugin.TextCommand):
def run(self, edit, pos, is_last):
if not get_status() == 'playing':
return False
log('pos:%s' % pos)
log('is_last:%s' % is_last)
self.view.sel().clear()
self.view.sel().add(sublime.Region(pos, pos))
self.view.show_at_center(pos)
if is_last:
set_status('done')
toggle_distraction_free(False)
self.view.set_read_only(False)