-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommand-timeout.lua
72 lines (59 loc) · 2.11 KB
/
command-timeout.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
--[[
This script sends an input command, but only after the specified delay
The last argument is the time in seconds before the command executes,
the preceding arguments are the command in json array format
example:
script-message command-timeout ["show-text" , "hello" , "2000"] 2
If the timeout is not given then it uses the global default set via script-opts
The command string can have spaces at any point but before/after the square brackets.
If the same timeout command is sent before the timer runs out, then the
timer is reset without sending the command.
available at: https://github.com/CogentRedTester/mpv-scripts
This script is no-longer supported, I hghly suggest using delay-command.lua instead:
https://github.com/CogentRedTester/mpv-scripts/blob/master/delay-command.lua
]]--
local mp = require 'mp'
local msg = require 'mp.msg'
local utils = require 'mp.utils'
local opt = require 'mp.options'
local o = {
timeout = 5,
}
opt.read_options(o, 'command_timeout')
local timers = {}
mp.register_script_message('command-timeout', function(...)
msg.verbose('recieved arguments: ' .. ...)
local args = {...}
if #args < 1 then
msg.error('not enough arguments, script requires at least 1')
end
local str = ""
for i = 1, #args do
str = str .. " " .. args[i]
end
if timers[str] then
msg.verbose('resetting time on timeout: ' .. str)
timers[str]:kill()
timers[str]:resume()
return
end
local cmd, error, timeout = utils.parse_json(str, true)
if timeout == "" then
msg.verbose('did not recieve timeout, using default')
timeout = o.timeout
else
timeout = tonumber(timeout)
end
if timeout == nil then
msg.error('did not recieve vaid timeout')
return
end
timers[str] = mp.add_timeout(timeout, function()
msg.verbose('sending command: ' .. str)
local def, error = mp.command_native(cmd, true)
if def then
msg.error('error sending command ' .. str)
msg.error(error)
end
end)
end)