-
Notifications
You must be signed in to change notification settings - Fork 42
/
playlistmanager-save-interactive.lua
71 lines (59 loc) · 2.4 KB
/
playlistmanager-save-interactive.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
-- This module enables typing a name for playlists when saving them in playlistmanager
-- This module requires playlistmanager to work
local msg = require("mp.msg")
-- reference https://github.com/NurioHin/mpv-bookmarker
local controls = {
ESC = function() deactivate() end,
ENTER = function() commit() end,
BS = function() typer("backspace") end,
SPACE = function() typer(" ") end
}
local keys = {
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9","0",
"!","@","$","%","^","&","*","(",")","-","_","=","+","[","]","{","}","\\","|",";",":","\"",",",".","<",">","/","?","`","~"
}
local illegal_char_set = {}
for _, ch in pairs({"<",">",":","\"","/","\\","|","?","*"}) do
illegal_char_set[ch] = true
end
local input = ""
function activate()
for key, func in pairs(controls) do
mp.add_forced_key_binding(key, "playlist-save-interactive-key-"..key, func, {repeatable=true})
end
for i, key in ipairs(keys) do
mp.add_forced_key_binding(key, "playlist-save-interactive-key-"..key, function() typer(key) end, {repeatable=true})
end
local date = os.date("*t")
input = ("%02d-%02d-%02d_%02d-%02d-%02d"):format(date.year, date.month, date.day, date.hour, date.min, date.sec)
typer("")
end
function commit()
deactivate()
mp.command("script-message playlistmanager save \""..input..".m3u\" \"save playlist with a custom name\"")
end
function deactivate()
mp.set_osd_ass(0, 0, "")
for key, _ in pairs(controls) do
mp.remove_key_binding("playlist-save-interactive-key-"..key)
end
for i, key in ipairs(keys) do
mp.remove_key_binding("playlist-save-interactive-key-"..key)
end
end
function typer(s)
if s == "backspace" then
input = input:sub(1, #input - 1)
elseif illegal_char_set[s] then
msg.info("Illegal filename char: "..s)
else
input = input..s
end
mp.set_osd_ass(0, 0, "Enter playlist name: "..input..".m3u")
end
-- this will be called from playlistmanager
mp.register_script_message("playlistmanager-save-interactive", activate)
-- this will enable the feature in playlistmanager
mp.command("script-message playlistmanager enable-interactive-save \"enable interactive filenaming in playlistmanager\"")