-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbookmark_editor.lua
More file actions
853 lines (559 loc) · 18.6 KB
/
bookmark_editor.lua
File metadata and controls
853 lines (559 loc) · 18.6 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
function descriptor()
return {
title = "bookmarks editor",
version = "1.0",
author = "devin",
url = 'https://github.com/No-notion/bookmarks-captures',
shortdesc = "bookmarks editor",
description = "",
capabilities = {"menu", "playing-listener"}
}
end
dest = nil
item_name = nil
bookmarks = {}
current_item = 1
mode = 0 -- 0 for normal mode ; 1 for edit mode
layout_config = '0' -- 0 - vertical 1 - horizontal
user_datadir = nil
os_type = nil -- windows, linux, mac
local d
function activate()
if xpcall(get_media_info,errorHandle) then
dbg_print("Welcome")
check_system()
init_data()
init_mainUI()
listen_event()
else
local err_dialog = vlc.dialog("warning")
err_dialog:add_label("<div style='color:RoyalBlue;'>please open one media before running this extension</div>",1,1,1,1)
end
end
function deactivate()
if d then -- If the dialog exists
d:delete() -- Delete the dialog
end
end
function listen_event()
dbg_print("listen media changed")
vlc.event.attach(vlc.playlist, "playing", function()
dbg_print("Media changed.")
reload_data()
end)
end
function init_data()
get_media_info()
load_preference()
load_bookmarks()
end
function get_media_info()
input = vlc.object.input()
item_name = vlc.input.item():name()
end
function load_preference()
user_datadir = vlc.config.userdatadir()
pref_path = user_datadir .. '/' .. 'pref' .. ".txt"
local can_open_file = can_open_file(pref_path)
local prefers = {}
if (can_open_file ) then
for line in io.lines(pref_path) do
prefers[#prefers + 1] = line
end
layout_config = prefers[1]
user_datadir = prefers[2]
-- jump_autoplay = prefers[3]
else
xpcall(write_file(pref_path,preference_string()),errorHandle)
end
dest = user_datadir .. '/' .. item_name .. '.txt'
end
--This adds a bookmark with a timestamp only. --
function add_bookmark(dec_string)
current_item = #bookmarks + 1
bookmarks[current_item] = dec_string
xpcall(vlc_player_pause(),errorHandle)
dbg_print("add book mark")
end
-- refresh bookmark view and save to hard disk --
function refresh_bookmark()
refresh_bookmarks_ui()
write_bookmarks_to_drive()
end
function capture_bookmark()
if (mode ==1) then return end
mode = 1
local format_string = current_time_string()
local dec_string = '[[' .. format_string .. ']]'
dbg_print('create bookmark')
add_bookmark(dec_string)
change_mode_ui()
update_message("Add the " .. abbr_from_number(#bookmarks) .." bookmark ---Press the save button to save")
end
function capture_untitle()
if (mode ==1) then return end
local format_string = current_time_string()
local dec_string = '[[' .. format_string .. ']]' .. "Untitled"
add_bookmark(dec_string)
refresh_bookmark()
if (cap_checkbox:get_checked() == true) then xpcall(vlc_player_resume(),errorHandle) end
update_message("Add the " .. #bookmarks .."th bookmark ---Press the save button to save")
end
--use auto-incremented numbers as bookmark--
function capture_auto()
if (mode ==1) then return end
local format_string = current_time_string()
local dec_string = '[[' .. format_string .. ']]' .. #bookmarks
add_bookmark(dec_string)
refresh_bookmark()
if (cap_checkbox:get_checked() == true) then xpcall(vlc_player_resume(),errorHandle) end
update_message("Add the " .. #bookmarks .."th bookmark ---Press the save button to save")
end
function save_bookmark()
if (mode == 0) then return end
mode = 0;
if (cap_checkbox:get_checked() == true) then xpcall(vlc_player_resume(),errorHandle) end
update_bookmarks_from_input()
change_mode_ui()
write_bookmarks_to_drive()
end
function edit_bookmark()
mode = 1
xpcall(vlc_player_pause(),errorHandle)
current_item = selection_item()
change_mode_ui()
update_message("Edit " ..current_item .."th bookmark ---" .. bookmarks[current_item] .." ---Press the save button to save")
end
function remove_bookmark()
if (mode == 1) then return end
local selects = bookmarks_list:get_selection()
local sels = {}
for i,v in pairs(selects) do
table.insert(sels, i)
end
table.sort(sels, function (o1, o2) return o1 > o2 end )
for i,v in pairs(sels) do table.remove(bookmarks,v) end
bookmarks_list:clear()
show_bookmarks()
write_bookmarks_to_drive()
if #sels < 2 then
update_message("Remove " .. sels[1] .."th " .."bookmarks")
else
update_message("Remove " .. sels[#sels] .. "th ~ " .. sels[1] .."th " .."bookmarks")
end
end
function clear_bookmarks()
if (mode == 1) then return end
bookmarks = {}
bookmarks_list:clear()
show_bookmarks()
write_bookmarks_to_drive()
update_message("Remove all bookmarks")
end
function update_bookmarks_from_input()
local text = text_input:get_text()
if text then
if (user_type_nothing(text)) then text = text .. 'Untitled' end
bookmarks[current_item] = text
end
end
function selection_item()
local selections = bookmarks_list:get_selection()
for i,v in pairs(selections) do
return i ;
end
return -1 ;
end
function jump_prev()
current_item = current_item - 1;
if current_item < 1 then
current_item = #bookmarks
end
jump_to_item(current_item)
end
function jump_to()
local item = selection_item()
if (item ~= -1) then
current_item = item
jump_to_item(current_item)
end
end
function jump_next()
current_item = current_item + 1
if current_item > #bookmarks then
current_item = 1
end
jump_to_item(current_item)
end
function jump_to_item(item)
vlc_player_pause()
local line = bookmarks[current_item]
local format_string = string.gmatch(line, "(%d+:%d+:%d+)")
for time in format_string do
stamp = stamp_from_time(time)
xpcall(jump_to_stamp(stamp),errorHandle)
end
vlc_player_resume()
update_message("Jump to ".. current_item .. "th bookmark")
end
function jump_to_stamp(stamp)
if stamp then
dbg_print("jump to " .. stamp)
vlc.var.set(input,"time",stamp)
if jump_checkbox:get_checked() == true then vlc_player_resume() end
end
end
function edit_with_editor()
local command_str = nil
if (os_type == 'windows') then
command_str = string.format('start Notepad "%s"',dest)
elseif(os_type == 'mac') then
command_str = string.format('open "%s"',dest)
else
command_str = string.format('gedit "%s"',dest)
end
update_message("Click the reload button to allow the changes edited using the third editor to take effect. ")
command_exe(command_str)
end
function reload_data()
init_data()
bookmarks_list:clear()
show_bookmarks()
update_message(string.format("Reload %s", dest))
end
function update_preference()
user_datadir = location_label:get_text()
xpcall(write_file(pref_path, preference_string()),errorHandle)
end
function goto_userdir()
local location = current_location()
local open_cmd = 'open '
if (os_type == 'windows') then
open_cmd = 'start explorer'
elseif(os_type == 'linux') then
open_cmd = 'nautilus '
end
local open_dir_command = string.format('%s "%s"',open_cmd, location)
command_exe(open_dir_command)
end
function load_bookmarks()
bookmarks = {}
local can_open = can_open_file(dest)
if (can_open == true) then
for line in io.lines(dest) do bookmarks[#bookmarks+1] = line end
end
end
function show_bookmarks()
if (bookmarks_list) then
if (bookmarks and #bookmarks > 0) then
for row =1,#bookmarks do
local line = bookmarks[row]
if (layout_config == '0') then
bookmarks_list:add_value(string.sub(line, 13), row)
else
bookmarks_list:add_value(line, row)
end
end
end
end
end
function change_mode_ui()
if (mode == 1) then
local dec_string = bookmarks[current_item]
if text_input then
text_input:set_text(dec_string)
else
local frame = frame_with_layout(layout_config)
local x,y,w,h = frame['edit_text'][1],frame['edit_text'][2],frame['edit_text'][3],frame['edit_text'][4]
text_input = mainUI:add_text_input(dec_string,x,y,w,z)
end
else
if text_input then
mainUI:del_widget(text_input)
text_input = nil
end
refresh_bookmarks_ui()
end
end
function change_layout_ui()
local text = layout_btn:get_text()
if (text == 'Horizontal') then
layout_config = '1'
else
layout_config = '0'
update_message("switch to vertical mode and restart this extension to take effect")
end
xpcall(write_file(pref_path,preference_string()),errorHandle)
end
function refresh_bookmarks_ui()
if bookmarks_list then
bookmarks_list:clear()
show_bookmarks()
end
end
function init_mainUI()
local layout = frame_with_layout(layout)
mainUI = vlc.dialog('bookmarks editor')
d = mainUI
local x,y,w,h = layout['top_lay'][1],layout['top_lay'][2],layout['top_lay'][3],layout['top_lay'][4]
mainUI:add_label('</br>',x,y,w,z)
x,y,w,h = layout['bookmarks'][1],layout['bookmarks'][2],layout['bookmarks'][3],layout['bookmarks'][4]
bookmarks_list = mainUI:add_list(x,y,w,h)
if (os_type == 'mac') then
x,y,w,h = layout['edit_text'][1],layout['edit_text'][2],layout['edit_text'][3],layout['edit_text'][4]
edit_text = mainUI:add_label('</br>', x,y,w,h)
end
x,y,w,h = layout['edit_label'][1],layout['edit_label'][2],layout['edit_label'][3],layout['edit_label'][4]
edit_label_2 = mainUI:add_label('<b style="color:RoyalBlue;">Edit : </b>', x,y,w,h)
x,y,w,h = layout['capture'][1],layout['capture'][2],layout['capture'][3],layout['capture'][4]
capture_btn = mainUI:add_button('Caputure',capture_bookmark,x,y,w,h)
x,y,w,h = layout['save'][1],layout['save'][2],layout['save'][3],layout['save'][4]
save_btn = mainUI:add_button('Save',save_bookmark,x,y,w,h)
x,y,w,h = layout['edit'][1],layout['edit'][2],layout['edit'][3],layout['edit'][4]
edit_btn = mainUI:add_button('Edit',edit_bookmark,x,y,w,h)
x,y,w,h = layout['remove'][1],layout['remove'][2],layout['remove'][3],layout['remove'][4]
remove_btn = mainUI:add_button('Remove',remove_bookmark,x,y,w,h)
x,y,w,h = layout['jump_label'][1],layout['jump_label'][2],layout['jump_label'][3],layout['jump_label'][4]
jump_label = mainUI:add_label('<b style="color:RoyalBlue;">Jump : </b>', x,y,w,h)
x,y,w,h = layout['jump'][1],layout['jump'][2],layout['jump'][3],layout['jump'][4]
jump_btn = mainUI:add_button('Jump To',jump_to,x,y,w,h)
x,y,w,h = layout['jump_pre'][1],layout['jump_pre'][2],layout['jump_pre'][3],layout['jump_pre'][4]
jump_pre = mainUI:add_button('Jump prev',jump_prev,x,y,w,h)
x,y,w,h = layout['jump_next'][1],layout['jump_next'][2],layout['jump_next'][3],layout['jump_next'][4]
jump_next = mainUI:add_button('Jump next',jump_next,x,y,w,h)
x,y,w,h = layout['prefer_label'][1],layout['prefer_label'][2],layout['prefer_label'][3],layout['prefer_label'][4]
prefer_label = mainUI:add_label('<b style="color:RoyalBlue;">Preference : </b>', x,y,w,h)
x,y,w,h = layout['layout_config'][1],layout['layout_config'][2],layout['layout_config'][3],layout['layout_config'][4]
local text = nil
if (layout_config == '1') then
text = 'Vertical'
else
text = 'Horizontal'
end
layout_btn = mainUI:add_button(text,change_layout_ui,x,y,w,h)
if (layout_config == '1') then
x,y,w,h = layout['clear'][1],layout['clear'][2],layout['clear'][3],layout['clear'][4]
clear_btn = mainUI:add_button('clear',clear_bookmarks,x,y,w,h)
x,y,w,h = layout['cap_untitled'][1],layout['cap_untitled'][2],layout['cap_untitled'][3],layout['cap_untitled'][4]
cap_untitle_btn = mainUI:add_button('Capt Untitled',capture_untitle,x,y,w,h)
x,y,w,h = layout['cap_auto'][1],layout['cap_auto'][2],layout['cap_auto'][3],layout['cap_auto'][4]
cap_auto_btn = mainUI:add_button('Capt Auto',capture_auto,x,y,w,h)
x,y,w,h = layout['cap_check'][1],layout['cap_check'][2],layout['cap_check'][3],layout['cap_check'][4]
cap_checkbox = mainUI:add_check_box("auto play",true, x,y,w,h)
x,y,w,h = layout['jump_check'][1],layout['jump_check'][2],layout['jump_check'][3],layout['jump_check'][4]
jump_checkbox = mainUI:add_check_box("auto play",false, x,y,w,h)
x,y,w,h = layout['location_label'][1],layout['location_label'][2],layout['location_label'][3],layout['location_label'][4]
mainUI:add_label('<b style="color:RoyalBlue;">Location : </b>', x,y,w,h)
x,y,w,h = layout['location_text'][1],layout['location_text'][2],layout['location_text'][3],layout['location_text'][4]
location_label = mainUI:add_text_input(user_datadir, x,y,w,h)
x,y,w,h = layout['edit_with_editor'][1],layout['edit_with_editor'][2],layout['edit_with_editor'][3],layout['edit_with_editor'][4]
mainUI:add_button('Editor',edit_with_editor, x,y,w,h)
x,y,w,h = layout['reload'][1],layout['reload'][2],layout['reload'][3],layout['reload'][4]
mainUI:add_button('Reload',reload_data, x,y,w,h)
x,y,w,h = layout['update'][1],layout['update'][2],layout['update'][3],layout['update'][4]
mainUI:add_button('Update',update_preference, x,y,w,h)
x,y,w,h = layout['go_to'][1],layout['go_to'][2],layout['go_to'][3],layout['go_to'][4]
mainUI:add_button('Go to',goto_userdir, x,y,w,h)
x,y,w,h = layout['message'][1],layout['message'][2],layout['message'][3],layout['message'][4]
mainUI:add_label('<b style="color:RoyalBlue;">Message : </b>', x,y,w,h)
x,y,w,h = layout['message_text'][1],layout['message_text'][2],layout['message_text'][3],layout['message_text'][4]
message_text = mainUI:add_label('', x,y,w,h)
else
x,y,w,h = layout['reload'][1],layout['reload'][2],layout['reload'][3],layout['reload'][4]
mainUI:add_button('Reload',reload_data, x,y,w,h)
end
show_bookmarks()
end
function frame_with_layout(lay)
if(layout_config == '0' ) then
local y = 4
if (os_type == 'mac') then y = 3 end
local frame = {
top_lay = {1,1,1,0},
bookmarks = {1,2,1,3},
edit_text = {1,y,1,1},
edit_label = {1,y+1,1,1},
capture = {1,y+2,1,1},
save = {1,y+3,1,1},
edit = {1,y+4,1,1},
remove = {1,y+5,1,1},
jump_label = {1,y+6,1,1},
jump = {1,y+7,1,1},
jump_next = {1,y+9,1,1},
jump_pre = {1,y+8,1,1},
prefer_label = {1,y+10,1,1},
layout_config = {1,y+12,1,1},
reload = {1,y+11,1,1}
}
return frame
else
local y = 6
if (os_type == 'mac') then y = 5 end
local frame = {
top_lay = {1,1,6,1},
bookmarks = {1,2,6,3},
edit_text = {1,y-2,6,1},
edit_label = {1,y-1,1,1},
capture = {2,y-1,1,1},
save = {3,y-1,1,1},
edit = {4,y-1,1,1},
remove = {5,y-1,1,1},
clear = {6,y-1,1,1},
cap_untitled = {2,y,1,1},
cap_auto = {3,y,1,1},
cap_check = {5,y,1,1},
jump_label = {1,y+1,1,1},
jump = {2,y+1,1,1},
jump_text = {3,y+1,1,1},
jump_next = {4,y+1,1,1},
jump_pre = {3,y+1,1,1},
jump_check = {5,y+1,1,1},
prefer_label = {1,y+2,1,1},
layout_config = {2,y+2,1,1},
reload = {4,y+2,1,1},
edit_with_editor = {3,y+2,1,1},
location_label = {1,y+3,1,1},
location_text = {2,y+3,3,1},
update = {6,y+3,1,1},
go_to = {5,y+3,1,1},
message = {1,y+4,1,1},
message_text = {2,y+4,5,1}
}
return frame
end
end
function update_message(contents)
message_text:set_text("<span style='color:RoyalBlue;opacity:70%;font-size:12px'>"..contents.."</span>")
end
function current_location()
return location_label:get_text()
end
function write_bookmarks_to_drive()
local contents = ""
if (bookmarks and #bookmarks > 0) then
for i,v in pairs(bookmarks) do contents = contents .. v .. '\n' end
xpcall(write_file(dest,contents),errorHandle)
else
remove_file(dest)
end
end
function can_open_file(name)
local file = io.open(name,'r')
local exist = false
if (file) then
exist = true
file:close()
end
return exist
end
function write_file(name,contents)
local file = io.open(name,'w')
if (file) then
file:write(contents)
file:close()
end
end
function remove_file(name)
xpcall(os.remove(name),errorHandle)
end
function user_type_nothing(text)
local type_nothing = false
if (#text < 13) then
type_nothing = true
end
return type_nothing
end
function preference_string()
return layout_config .. '\n' .. user_datadir .. '\n'
end
function current_time_string()
local stamp = vlc.var.get(input,"time")
local format_string = time_from_stamp(stamp)
return format_string
end
function time_from_stamp(stamp)
stamp = math.floor(stamp/1000000)
local hours = stamp/(60*60)
stamp= stamp%(60*60)
local minutes =stamp/60
local seconds =stamp%60
return string.format("%02d:%02d:%02d",hours,minutes,seconds)
end
-- with the format dd:dd:dd
function stamp_from_time(time)
local hours = string.sub(time, 0,2)
local minutes = string.sub(time, 4,5)
local seconds = string.sub(time, 7,8)
return (hours*60*60+minutes*60+seconds)*1000000
end
function vlc_player_pause()
if vlc.playlist.status() ~= "paused" then
vlc.playlist.pause()
end
end
function vlc_player_resume()
if vlc.playlist.status() == "paused" then
vlc.playlist.play()
end
end
function dbg_print(content)
content_type = type(content)
if type(content) == 'string' then
vlc.msg.dbg('[bookmarks Capture --]' .. content)
else
vlc.msg.dbg('[bookmarks Capture --]' .. 'can not print un-string')
end
end
function err_print(content)
if content_type == 'string' then
vlc.msg.error('[bookmarks Capture Error --]' .. content)
else
vlc.msg.error('[bookmarks Capture --]' .. 'can not print un-string')
end
end
function abbr_from_number(number)
x = ""
if number / 10 == 1 then
x = number .. "th "
else
num = number % 10
if num == 1 then
x = num .. "st"
elseif num == 2 then
x = num .. "nd"
elseif num == 3 then
x = num .. "rd"
else
x = num .. 'th'
end
end
return x
end
function errorHandle(error)
err_print(error)
end
function command_exe(command)
dbg_print('will execute command : ' .. command)
ret = os.execute(command)
dbg_print('command results : \n' .. ret)
end
function close()
vlc.deactivate()
end
function deactivate()
dbg_print('bye bye')
if mainUI then
mainUI:hide()
end
end
function check_system()
os_type = 'windows'
local user_dir = vlc.config.userdatadir()
local contains_linux_path = string.find(user_dir, '.local/share/vlc')
local contains_mac_path = string.find(user_dir, 'Library/Application')
if(contains_linux_path) then
os_type = 'linux'
elseif (contains_mac_path) then
os_type = 'mac'
end
end