-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmemo.lua
1439 lines (1278 loc) · 48.2 KB
/
memo.lua
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- memo.lua
--
-- A recent files menu for mpv
local options = {
-- File path gets expanded, leave empty for in-memory history
history_path = "~~/memo-history.log",
-- How many entries to display in menu
entries = 10,
-- Display navigation to older/newer entries
pagination = true,
-- Display files only once
hide_duplicates = true,
-- Check if files still exist
hide_deleted = true,
-- Display only the latest file from each directory
hide_same_dir = false,
-- Date format https://www.lua.org/pil/22.1.html
timestamp_format = "%Y-%m-%d %H:%M:%S",
-- Display titles instead of filenames when available
use_titles = true,
-- Truncate titles to n characters, 0 to disable
truncate_titles = 60,
-- Meant for use in auto profiles
enabled = true,
-- Keybinds for vanilla menu
up_binding = "UP WHEEL_UP",
down_binding = "DOWN WHEEL_DOWN",
select_binding = "RIGHT ENTER",
append_binding = "Shift+RIGHT Shift+ENTER",
close_binding = "LEFT ESC",
-- Path prefixes for the recent directory menu
-- This can be used to restrict the parent directory relative to which the
-- directories are shown.
-- Syntax
-- Prefixes are separated by | and can use Lua patterns by prefixing
-- them with "pattern:", otherwise they will be treated as plain text.
-- Pattern syntax can be found here https://www.lua.org/manual/5.1/manual.html#5.4.1
-- Example
-- "path_prefixes=My-Movies|pattern:TV Shows/.-/|Anime" will show directories
-- that are direct subdirectories of directories named "My-Movies" as well as
-- "Anime", while for TV Shows the shown directories are one level below that.
-- Opening the file "/data/TV Shows/Comedy/Curb Your Enthusiasm/S4/E06.mkv" will
-- lead to "Curb Your Enthusiasm" to be shown in the directory menu. Opening
-- of that entry will then open that file again.
path_prefixes = "pattern:.*"
}
function parse_path_prefixes(path_prefixes)
local patterns = {}
for prefix in path_prefixes:gmatch("([^|]+)") do
if prefix:find("pattern:", 1, true) == 1 then
patterns[#patterns + 1] = {pattern = prefix:sub(9)}
else
patterns[#patterns + 1] = {pattern = prefix, plain = true}
end
end
return patterns
end
local script_name = mp.get_script_name()
mp.utils = require "mp.utils"
mp.options = require "mp.options"
mp.options.read_options(options, "memo", function(list)
if list.path_prefixes then
options.path_prefixes = parse_path_prefixes(options.path_prefixes)
end
end)
options.path_prefixes = parse_path_prefixes(options.path_prefixes)
local assdraw = require "mp.assdraw"
local osd = mp.create_osd_overlay("ass-events")
osd.z = 2000
local osd_update = nil
local width, height = 0, 0
local margin_top, margin_bottom = 0, 0
local font_size = mp.get_property_number("osd-font-size") or 55
local fakeio = {data = "", cursor = 0, offset = 0, file = nil}
function fakeio:setvbuf(mode) end
function fakeio:flush()
self.cursor = self.offset + #self.data
end
function fakeio:read(format)
local out = ""
if self.cursor < self.offset then
self.file:seek("set", self.cursor)
out = self.file:read(format)
format = format - #out
self.cursor = self.cursor + #out
end
if format > 0 then
out = out .. self.data:sub(self.cursor - self.offset, self.cursor - self.offset + format)
self.cursor = self.cursor + format
end
return out
end
function fakeio:seek(whence, offset)
local base = 0
offset = offset or 0
if whence == "end" then
base = self.offset + #self.data
end
self.cursor = base + offset
return self.cursor
end
function fakeio:write(...)
local args = {...}
for i, v in ipairs(args) do
self.data = self.data .. v
end
end
local history, history_path
if options.history_path ~= "" then
history_path = mp.command_native({"expand-path", options.history_path})
history = io.open(history_path, "a+b")
end
if history == nil then
if history_path then
mp.msg.warn("cannot write to history file " .. options.history_path .. ", new entries will not be saved to disk")
history = io.open(history_path, "rb")
if history then
fakeio.offset = history:seek("end")
fakeio.file = history
end
end
history = fakeio
end
history:setvbuf("full")
local event_loop_exhausted = false
local uosc_available = false
local dyn_menu = nil
local menu_shown = false
local last_state = nil
local menu_data = nil
local palette = false
local search_words = nil
local search_query = nil
local dir_menu = false
local dir_menu_prefixes = nil
local new_loadfile = nil
local normalize_path = nil
local data_protocols = {
edl = true,
data = true,
null = true,
memory = true,
hex = true,
fd = true,
fdclose = true,
mf = true
}
local stacked_protocols = {
ffmpeg = true,
lavf = true,
appending = true,
file = true,
archive = true,
slice = true
}
local device_protocols = {
bd = true,
br = true,
bluray = true,
cdda = true,
dvb = true,
dvd = true,
dvdnav = true
}
function utf8_char_bytes(str, i)
local char_byte = str:byte(i)
local max_bytes = #str - i + 1
if char_byte < 0xC0 then
return math.min(max_bytes, 1)
elseif char_byte < 0xE0 then
return math.min(max_bytes, 2)
elseif char_byte < 0xF0 then
return math.min(max_bytes, 3)
elseif char_byte < 0xF8 then
return math.min(max_bytes, 4)
else
return math.min(max_bytes, 1)
end
end
function utf8_iter(str)
local byte_start = 1
return function()
local start = byte_start
if #str < start then return nil end
local byte_count = utf8_char_bytes(str, start)
byte_start = start + byte_count
return start, str:sub(start, byte_start - 1)
end
end
function utf8_table(str)
local t = {}
local width = 0
for _, char in utf8_iter(str) do
width = width + (#char > 2 and 2 or 1)
table.insert(t, char)
end
return t, width
end
function utf8_subwidth(t, start_index, end_index)
local index = 1
local substr = ""
for _, char in ipairs(t) do
if start_index <= index and index <= end_index then
local width = #char > 2 and 2 or 1
index = index + width
substr = substr .. char
end
end
return substr, index
end
function utf8_subwidth_back(t, num_chars)
local index = 0
local substr = ""
for i = #t, 1, -1 do
if num_chars > index then
local width = #t[i] > 2 and 2 or 1
index = index + width
substr = t[i] .. substr
end
end
return substr
end
function utf8_to_unicode(str, i)
local byte_count = utf8_char_bytes(str, i)
local char_byte = str:byte(i)
local unicode = char_byte
if byte_count ~= 1 then
local shift = 2 ^ (8 - byte_count)
char_byte = char_byte - math.floor(0xFF / shift) * shift
unicode = char_byte * (2 ^ 6) ^ (byte_count - 1)
end
for j = 2, byte_count do
char_byte = str:byte(i + j - 1) - 0x80
unicode = unicode + char_byte * (2 ^ 6) ^ (byte_count - j)
end
return math.floor(unicode + 0.5)
end
function ass_clean(str)
str = str:gsub("\\", "\\\239\187\191")
str = str:gsub("{", "\\{")
str = str:gsub("}", "\\}")
return str
end
-- Extended from https://stackoverflow.com/a/73283799 with zero-width handling from uosc
function unaccent(str)
local unimask = "[%z\1-\127\194-\244][\128-\191]*"
-- "Basic Latin".."Latin-1 Supplement".."Latin Extended-A".."Latin Extended-B"
local charmap =
"AÀÁÂÃÄÅĀĂĄǍǞǠǺȀȂȦȺAEÆǢǼ"..
"BßƁƂƄɃ"..
"CÇĆĈĊČƆƇȻ"..
"DÐĎĐƉƊDZƻDŽDZDzDžDz"..
"EÈÉÊËĒĔĖĘĚƎƏƐȄȆȨɆ"..
"FƑ"..
"GĜĞĠĢƓǤǦǴ"..
"HĤĦȞHuǶ"..
"IÌÍÎÏĨĪĬĮİƖƗǏȈȊIJIJ"..
"JĴɈ"..
"KĶƘǨ"..
"LĹĻĽĿŁȽLJLJLjLj"..
"NÑŃŅŇŊƝǸȠNJNJNjNj"..
"OÒÓÔÕÖØŌŎŐƟƠǑǪǬǾȌȎȪȬȮȰOEŒOIƢOUȢ"..
"PÞƤǷ"..
"QɊ"..
"RŔŖŘȐȒɌ"..
"SŚŜŞŠƧƩƪƼȘ"..
"TŢŤŦƬƮȚȾ"..
"UÙÚÛÜŨŪŬŮŰŲƯƱƲȔȖɄǓǕǗǙǛ"..
"VɅ"..
"WŴƜ"..
"YÝŶŸƳȜȲɎ"..
"ZŹŻŽƵƷƸǮȤ"..
"aàáâãäåāăąǎǟǡǻȁȃȧaeæǣǽ"..
"bƀƃƅ"..
"cçćĉċčƈȼ"..
"dðƌƋƍȡďđdbȸdzdždz"..
"eèéêëēĕėęěǝȅȇȩɇ"..
"fƒ"..
"gĝğġģƔǥǧǵ"..
"hĥħȟhvƕ"..
"iìíîïĩīĭįıǐȉȋijij"..
"jĵǰȷɉ"..
"kķĸƙǩ"..
"lĺļľŀłƚƛȴljlj"..
"nñńņňʼnŋƞǹȵnjnj"..
"oòóôõöøōŏőơǒǫǭǿȍȏȫȭȯȱoeœoiƣouȣ"..
"pþƥƿ"..
"qɋqpȹ"..
"rŕŗřƦȑȓɍ"..
"sśŝşšſƨƽșȿ"..
"tţťŧƫƭțȶtsƾ"..
"uùúûüũūŭůűųưǔǖǘǚǜȕȗ"..
"wŵ"..
"yýÿŷƴȝȳɏ"..
"zźżžƶƹƺǯȥɀ"
local zero_width_blocks = {
{0x0000, 0x001F}, -- C0
{0x007F, 0x009F}, -- Delete + C1
{0x034F, 0x034F}, -- combining grapheme joiner
{0x061C, 0x061C}, -- Arabic Letter Strong
{0x200B, 0x200F}, -- {zero-width space, zero-width non-joiner, zero-width joiner, left-to-right mark, right-to-left mark}
{0x2028, 0x202E}, -- {line separator, paragraph separator, Left-to-Right Embedding, Right-to-Left Embedding, Pop Directional Format, Left-to-Right Override, Right-to-Left Override}
{0x2060, 0x2060}, -- word joiner
{0x2066, 0x2069}, -- {Left-to-Right Isolate, Right-to-Left Isolate, First Strong Isolate, Pop Directional Isolate}
{0xFEFF, 0xFEFF}, -- zero-width non-breaking space
-- Some other characters can also be combined https://en.wikipedia.org/wiki/Combining_character
{0x0300, 0x036F}, -- Combining Diacritical Marks 0 BMP Inherited
{0x1AB0, 0x1AFF}, -- Combining Diacritical Marks Extended 0 BMP Inherited
{0x1DC0, 0x1DFF}, -- Combining Diacritical Marks Supplement 0 BMP Inherited
{0x20D0, 0x20FF}, -- Combining Diacritical Marks for Symbols 0 BMP Inherited
{0xFE20, 0xFE2F}, -- Combining Half Marks 0 BMP Cyrillic (2 characters), Inherited (14 characters)
-- Egyptian Hieroglyph Format Controls and Shorthand format Controls
{0x13430, 0x1345F}, -- Egyptian Hieroglyph Format Controls 1 SMP Egyptian Hieroglyphs
{0x1BCA0, 0x1BCAF}, -- Shorthand Format Controls 1 SMP Common
-- not sure how to deal with those https://en.wikipedia.org/wiki/Spacing_Modifier_Letters
{0x02B0, 0x02FF}, -- Spacing Modifier Letters 0 BMP Bopomofo (2 characters), Latin (14 characters), Common (64 characters)
}
return str:gsub(unimask, function(unichar)
local unicode = utf8_to_unicode(unichar, 1)
for _, block in ipairs(zero_width_blocks) do
if unicode >= block[1] and unicode <= block[2] then
return ""
end
end
return unichar:match("%a") or charmap:match("(%a+)[^%a]-"..(unichar:gsub("[%(%)%.%%%+%-%*%?%[%^%$]", "%%%1")))
end)
end
function shallow_copy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
function has_protocol(path)
return path:find("^%a[%w.+-]-://") or path:find("^%a[%w.+-]-:%?")
end
function normalize(path)
if normalize_path ~= nil then
if normalize_path then
-- don't normalize magnet-style paths
local protocol_start, protocol_end, protocol = path:find("^(%a[%w.+-]-):%?")
if not protocol_end then
path = mp.command_native({"normalize-path", path})
end
else
-- TODO: implement the basics of path normalization ourselves for mpv 0.38.0 and under
local directory = mp.get_property("working-directory", "")
if not has_protocol(path) then
path = mp.utils.join_path(directory, path)
end
end
return path
end
normalize_path = false
local commands = mp.get_property_native("command-list", {})
for _, command in ipairs(commands) do
if command.name == "loadfile" then
for _, arg in ipairs(command.args) do
if arg.name == "index" then
new_loadfile = true
break
end
end
end
if command.name == "normalize-path" then
normalize_path = true
break
end
end
return normalize(path)
end
function loadfile_compat(path)
if new_loadfile ~= nil then
if new_loadfile then
return {"-1", path}
end
return {path}
end
new_loadfile = false
local commands = mp.get_property_native("command-list", {})
for _, command in ipairs(commands) do
if command.name == "loadfile" then
for _, arg in ipairs(command.args) do
if arg.name == "index" then
new_loadfile = true
return {"-1", path}
end
end
return {path}
end
end
return {path}
end
function menu_json(menu_items, page)
local title = (search_query or (dir_menu and "Directories" or "History")) .. " (memo)"
if options.pagination or page ~= 1 then
title = title .. " - Page " .. page
end
local menu = {
type = "memo-history",
title = title,
items = menu_items,
on_search = {"script-message-to", script_name, "memo-search-uosc:"},
on_close = {"script-message-to", script_name, "memo-clear"},
palette = palette, -- TODO: remove on next uosc release
search_style = palette and "palette" or nil
}
return menu
end
function uosc_update()
local json = mp.utils.format_json(menu_data) or "{}"
mp.commandv("script-message-to", "uosc", menu_shown and "update-menu" or "open-menu", json)
end
function update_dimensions()
width, height = mp.get_osd_size()
osd.res_x = width
osd.res_y = height
draw_menu()
end
if mp.utils.shared_script_property_set then
function update_margins()
local shared_props = mp.get_property_native("shared-script-properties")
local val = shared_props["osc-margins"]
if val then
-- formatted as "%f,%f,%f,%f" with left, right, top, bottom, each
-- value being the border size as ratio of the window size (0.0-1.0)
local vals = {}
for v in string.gmatch(val, "[^,]+") do
vals[#vals + 1] = tonumber(v)
end
margin_top = vals[3] -- top
margin_bottom = vals[4] -- bottom
else
margin_top = 0
margin_bottom = 0
end
draw_menu()
end
else
function update_margins()
local val = mp.get_property_native("user-data/osc/margins")
if val then
margin_top = val.t
margin_bottom = val.b
else
margin_top = 0
margin_bottom = 0
end
draw_menu()
end
end
function bind_keys(keys, name, func, opts)
if not keys then
mp.add_forced_key_binding(keys, name, func, opts)
return
end
local i = 1
for key in keys:gmatch("[^%s]+") do
local prefix = i == 1 and "" or i
mp.add_forced_key_binding(key, name .. prefix, func, opts)
i = i + 1
end
end
function unbind_keys(keys, name)
if not keys then
mp.remove_key_binding(name)
return
end
local i = 1
for key in keys:gmatch("[^%s]+") do
local prefix = i == 1 and "" or i
mp.remove_key_binding(name .. prefix)
i = i + 1
end
end
function close_menu()
mp.unobserve_property(update_dimensions)
mp.unobserve_property(update_margins)
unbind_keys(options.up_binding, "move_up")
unbind_keys(options.down_binding, "move_down")
unbind_keys(options.select_binding, "select")
unbind_keys(options.append_binding, "append")
unbind_keys(options.close_binding, "close")
last_state = nil
menu_data = nil
search_words = nil
search_query = nil
dir_menu = false
menu_shown = false
palette = false
osd:update()
osd.hidden = true
osd:update()
end
function open_menu()
menu_shown = true
update_dimensions()
mp.observe_property("osd-dimensions", "native", update_dimensions)
mp.observe_property("video-out-params", "native", update_dimensions)
local margin_prop = mp.utils.shared_script_property_set and "shared-script-properties" or "user-data/osc/margins"
mp.observe_property(margin_prop, "native", update_margins)
local function select_item(append)
local item = menu_data.items[last_state.selected_index]
if not item then return end
if not item.keep_open then
close_menu()
end
if append and item.value[1] == "loadfile" then
-- bail if file is already in playlist
local playlist = mp.get_property_native("playlist", {})
for i = 1, #playlist do
local playlist_file = playlist[i].filename
local display_path, save_path, effective_path, effective_protocol, is_remote, file_options = path_info(playlist_file)
if not is_remote then
playlist_file = normalize(save_path)
end
if item.value[2] == playlist_file then
return
end
end
item.value[3] = "append-play"
end
mp.commandv(unpack(item.value))
end
bind_keys(options.up_binding, "move_up", function()
last_state.selected_index = math.max(last_state.selected_index - 1, 1)
draw_menu()
end, { repeatable = true })
bind_keys(options.down_binding, "move_down", function()
last_state.selected_index = math.min(last_state.selected_index + 1, #menu_data.items)
draw_menu()
end, { repeatable = true })
bind_keys(options.select_binding, "select", select_item)
bind_keys(options.append_binding, "append", function()
select_item(true)
end)
bind_keys(options.close_binding, "close", close_menu)
osd.hidden = false
draw_menu()
end
function draw_menu()
if not menu_data then return end
if not menu_shown then
open_menu()
end
local num_options = #menu_data.items > 0 and #menu_data.items + 1 or 1
last_state.selected_index = math.min(last_state.selected_index, #menu_data.items)
local function get_scrolled_lines()
local output_height = height - margin_top * height - margin_bottom * height - 0.2 * font_size + 0.5
local screen_lines = math.max(math.floor(output_height / font_size), 1)
local max_scroll = math.max(num_options - screen_lines, 0)
return math.min(math.max(last_state.selected_index - math.ceil(screen_lines / 2), 0), max_scroll) - 1
end
local ass = assdraw.ass_new()
local curtain_opacity = 0.7
local alpha = 255 - math.ceil(255 * curtain_opacity)
ass.text = string.format("{\\pos(0,0)\\rDefault\\an7\\1c&H000000&\\alpha&H%X&}", alpha)
ass:draw_start()
ass:rect_cw(0, 0, width, height)
ass:draw_stop()
ass:new_event()
ass:append("{\\rDefault\\pos("..(0.3 * font_size).."," .. (margin_top * height + 0.1 * font_size) .. ")\\an7\\fs" .. font_size .. "\\bord2\\q2\\b1}" .. ass_clean(menu_data.title) .. "{\\b0}")
ass:new_event()
local scrolled_lines = get_scrolled_lines() - 1
local pos_y = margin_top * height - scrolled_lines * font_size + 0.2 * font_size + 0.5
local clip_top = math.floor(margin_top * height + font_size + 0.2 * font_size + 0.5)
local clip_bottom = math.floor((1 - margin_bottom) * height + 0.5)
local clipping_coordinates = "0," .. clip_top .. "," .. width .. "," .. clip_bottom
if #menu_data.items > 0 then
local menu_index = 0
for i = 1, #menu_data.items do
local item = menu_data.items[i]
if item.title then
local icon
local separator = last_state.selected_index == i and "{\\alpha&HFF&}●{\\alpha&H00&} - " or "{\\alpha&HFF&}●{\\alpha&H00&} - "
if item.icon == "spinner" then
separator = "⟳ "
elseif item.icon == "navigate_next" then
icon = last_state.selected_index == i and "▶" or "▷"
elseif item.icon == "navigate_before" then
icon = last_state.selected_index == i and "◀" or "◁"
else
icon = last_state.selected_index == i and "●" or "○"
end
ass:new_event()
ass:pos(0.3 * font_size, pos_y + menu_index * font_size)
ass:append("{\\rDefault\\fnmonospace\\an1\\fs" .. font_size .. "\\bord2\\q2\\clip(" .. clipping_coordinates .. ")}"..separator.."{\\rDefault\\an7\\fs" .. font_size .. "\\bord2\\q2}" .. ass_clean(item.title))
if icon then
ass:new_event()
ass:pos(0.6 * font_size, pos_y + menu_index * font_size)
ass:append("{\\rDefault\\fnmonospace\\an2\\fs" .. font_size .. "\\bord2\\q2\\clip(" .. clipping_coordinates .. ")}" .. icon)
end
menu_index = menu_index + 1
end
end
else
ass:pos(0.3 * font_size, pos_y)
ass:append("{\\rDefault\\an1\\fs" .. font_size .. "\\bord2\\q2\\clip(" .. clipping_coordinates .. ")}")
ass:append("No entries")
end
osd_update = nil
osd.data = ass.text
osd:update()
end
function get_full_path()
local path = mp.get_property("path")
if path == nil or path == "-" or path == "/dev/stdin" then return end
local display_path, save_path, effective_path, effective_protocol, is_remote, file_options = path_info(path)
if not is_remote then
path = normalize(save_path)
end
return path, display_path, save_path, effective_path, effective_protocol, is_remote, file_options
end
function path_info(full_path)
local function resolve(effective_path, save_path, display_path, last_protocol, is_remote)
local protocol_start, protocol_end, protocol = display_path:find("^(%a[%w.+-]-)://")
if protocol == "ytdl" then
-- for direct video access ytdl://videoID and ytsearch:
is_remote = true
elseif protocol and not stacked_protocols[protocol] then
local input_path, file_options
if device_protocols[protocol] then
input_path, file_options = display_path:match("(.-) %-%-opt=(.+)")
effective_path = file_options and file_options:match(".+=(.*)")
if protocol == "dvb" then
is_remote = true
if not effective_path then
effective_path = display_path
input_path = display_path:sub(protocol_end + 1)
end
end
display_path = input_path or display_path
else
is_remote = true
display_path = display_path:sub(protocol_end + 1)
end
return display_path, save_path, effective_path, protocol, is_remote, file_options
end
if not protocol_end then
if last_protocol == "ytdl" then
display_path = "ytdl://" .. display_path
end
return display_path, save_path, effective_path, last_protocol, is_remote, nil
end
display_path = display_path:sub(protocol_end + 1)
if protocol == "archive" then
local main_path, archive_path, filename = display_path:gsub("%%7C", "|"):match("(.-)(|.-[\\/])(.+)")
if not main_path then
local main_path = display_path:match("(.-)|")
effective_path = normalize(main_path or display_path)
_, save_path, effective_path, protocol, is_remote, file_options = resolve(effective_path, save_path, display_path, protocol, is_remote)
effective_path = normalize(effective_path)
save_path = "archive://" .. (save_path or effective_path)
if main_path then
save_path = save_path .. display_path:match("|(.-)")
end
else
display_path, save_path, _, protocol, is_remote, file_options = resolve(main_path, save_path, main_path, protocol, is_remote)
effective_path = normalize(display_path)
save_path = save_path or effective_path
save_path = "archive://" .. save_path .. (save_path:find("archive://") and archive_path:gsub("|", "%%7C") or archive_path) .. filename
_, main_path = mp.utils.split_path(main_path)
_, filename = mp.utils.split_path(filename)
display_path = main_path .. ": " .. filename
end
elseif protocol == "slice" then
if effective_path then
effective_path = effective_path:match(".-@(.*)") or effective_path
end
display_path = display_path:match(".-@(.*)") or display_path
end
return resolve(effective_path, save_path, display_path, protocol, is_remote)
end
-- don't resolve magnet-style paths
local protocol_start, protocol_end, protocol = full_path:find("^(%a[%w.+-]-):%?")
if protocol_end then
return full_path, full_path, protocol, true, nil
end
local display_path, save_path, effective_path, effective_protocol, is_remote, file_options = resolve(nil, nil, full_path, nil, false)
effective_path = effective_path or display_path
save_path = save_path or effective_path
if is_remote and not file_options then
display_path = display_path:gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
end
return display_path, save_path, effective_path, effective_protocol, is_remote, file_options
end
function write_history(display)
local full_path, display_path, save_path, effective_path, effective_protocol, is_remote, file_options = get_full_path()
if full_path == nil then
mp.msg.debug("cannot get full path to file")
if display then
mp.osd_message("[memo] cannot get full path to file")
end
return
end
if data_protocols[effective_protocol] then
mp.msg.debug("not logging file with " .. effective_protocol .. " protocol")
if display then
mp.osd_message("[memo] not logging file with " .. effective_protocol .. " protocol")
end
return
end
if effective_protocol == "bd" or effective_protocol == "br" or effective_protocol == "bluray" then
full_path = full_path .. " --opt=bluray-device=" .. mp.get_property("bluray-device", "")
elseif effective_protocol == "cdda" then
full_path = full_path .. " --opt=cdrom-device=" .. mp.get_property("cdrom-device", "")
elseif effective_protocol == "dvb" then
local dvb_program = mp.get_property("dvbin-prog", "")
if dvb_program ~= "" then
full_path = full_path .. " --opt=dvbin-prog=" .. dvb_program
end
elseif effective_protocol == "dvd" or effective_protocol == "dvdnav" then
full_path = full_path .. " --opt=dvd-angle=" .. mp.get_property("dvd-angle", "1") .. ",dvd-device=" .. mp.get_property("dvd-device", "")
end
mp.msg.debug("logging file " .. full_path)
if display then
mp.osd_message("[memo] logging file " .. full_path)
end
local playlist_pos = mp.get_property_number("playlist-pos") or -1
local title = playlist_pos > -1 and mp.get_property("playlist/"..playlist_pos.."/title") or ""
local title_length = #title
local timestamp = os.time()
-- format: <timestamp>,<title length>,<title>,<path>,<entry length>
local entry = timestamp .. "," .. (title_length > 0 and title_length or "") .. "," .. title .. "," .. full_path
local entry_length = #entry
history:seek("end")
history:write(entry .. "," .. entry_length, "\n")
history:flush()
if dyn_menu then
dyn_menu_update()
end
end
function show_history(entries, next_page, prev_page, update, return_items)
if event_loop_exhausted then return end
event_loop_exhausted = true
local should_close = menu_shown and not prev_page and not next_page and not update
if should_close then
memo_close()
if not return_items then
return
end
end
local max_digits_length = 4 + 2
local retry_offset = 512
local menu_items = {}
local state = (prev_page or next_page) and last_state or {
known_dirs = {},
known_files = {},
existing_files = {},
cursor = history:seek("end"),
retry = 0,
pages = {},
current_page = 1,
selected_index = 1
}
if update then
state.pages = {}
end
if last_state then
if prev_page then
if state.current_page == 1 then return end
state.current_page = state.current_page - 1
elseif next_page then
if state.cursor == 0 and not state.pages[state.current_page + 1] then return end
if options.entries < 1 then return end
state.current_page = state.current_page + 1
end
end
last_state = state
if state.pages[state.current_page] then
menu_data = menu_json(state.pages[state.current_page], state.current_page)
if uosc_available then
uosc_update()
else
draw_menu()
end
return
end
local function find_path_prefix(path, path_prefixes)
for _, prefix in ipairs(path_prefixes) do
local start, stop = path:find(prefix.pattern, 1, prefix.plain)
if start then
return start, stop
end
end
end
-- all of these error cases can only happen if the user messes with the history file externally
local function read_line()
history:seek("set", state.cursor - max_digits_length)
local tail = history:read(max_digits_length)
if not tail then
mp.msg.debug("error could not read entry length @ " .. state.cursor - max_digits_length)
return
end
local entry_length_str, whitespace = tail:match("(%d+)(%s*)$")
if not entry_length_str then
mp.msg.debug("invalid entry length @ " .. state.cursor)
state.cursor = math.max(state.cursor - retry_offset, 0)
history:seek("set", state.cursor)
local retry = history:read(retry_offset)
if not retry then
mp.msg.debug("retry failed @ " .. state.cursor)
state.cursor = 0
return
end
local last_valid = string.match(retry, ".*(%d+\n.*)")
local offset = last_valid and #last_valid or retry_offset
state.cursor = state.cursor + retry_offset - offset + 1
if state.cursor == state.retry then
mp.msg.debug("bailing")
state.cursor = 0
return
end
state.retry = state.cursor
mp.msg.debug("retrying @ " .. state.cursor)
return
end
local entry_length = tonumber(entry_length_str)
state.cursor = state.cursor - entry_length - #entry_length_str - #whitespace - 1
history:seek("set", state.cursor)
local entry = history:read(entry_length)
if not entry then
mp.msg.debug("unreadable entry data @ " .. state.cursor)
return
end
local timestamp_str, title_length_str, file_info = entry:match("([^,]*),(%d*),(.*)")
if not timestamp_str then
mp.msg.debug("invalid entry data @ " .. state.cursor)
return
end
local timestamp = tonumber(timestamp_str)
timestamp = timestamp and os.date(options.timestamp_format, timestamp) or timestamp_str
local title_length = title_length_str ~= "" and tonumber(title_length_str) or 0
local full_path = file_info:sub(title_length + 2)
local display_path, save_path, effective_path, effective_protocol, is_remote, file_options = path_info(full_path)
local cache_key = effective_path .. display_path .. (file_options or "")
if options.hide_duplicates and state.known_files[cache_key] then
return
end
if dir_menu and is_remote then
return
end
if search_words and not options.use_titles then
for _, word in ipairs(search_words) do
if unaccent(display_path):lower():find(word, 1, true) == nil then
return
end
end
end
local dirname, basename
if is_remote then
state.existing_files[cache_key] = true
state.known_files[cache_key] = true
elseif options.hide_same_dir or dir_menu then
dirname, basename = mp.utils.split_path(display_path)
if dir_menu then
if dirname == "." then return end
local unix_dirname = dirname:gsub("\\", "/")
local parent, _ = mp.utils.split_path(unix_dirname:sub(1, -2))
local start, stop = find_path_prefix(parent, dir_menu_prefixes)
if not start then
return
end
basename = unix_dirname:match("/(.-)/", stop)
if basename == nil then return end
start, stop = dirname:find(basename, stop, true)
dirname = dirname:sub(1, stop + 1)
end
if state.known_dirs[dirname] then
return
end
if dirname ~= "." then
state.known_dirs[dirname] = true
end
end
if options.hide_deleted and not (search_words and options.use_titles) then
if state.known_files[cache_key] and not state.existing_files[cache_key] then
return
end
if not state.known_files[cache_key] then
local stat = mp.utils.file_info(effective_path)
if stat then
state.existing_files[cache_key] = true
elseif dir_menu then
state.known_files[cache_key] = true
local dir = mp.utils.split_path(effective_path)
if dir == "." then
return