-
Notifications
You must be signed in to change notification settings - Fork 6
/
user-input-module.lua
126 lines (104 loc) · 4.17 KB
/
user-input-module.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
--[[
This is a module designed to interface with mpv-user-input
https://github.com/CogentRedTester/mpv-user-input
Loading this script as a module will return a table with two functions to format
requests to get and cancel user-input requests. See the README for details.
Alternatively, developers can just paste these functions directly into their script,
however this is not recommended as there is no guarantee that the formatting of
these requests will remain the same for future versions of user-input.
]]
local API_VERSION = "0.1.0"
local mp = require 'mp'
local msg = require "mp.msg"
local utils = require 'mp.utils'
local mod = {}
local name = mp.get_script_name()
local counter = 1
local function pack(...)
local t = {...}
t.n = select("#", ...)
return t
end
local request_mt = {}
-- ensures the option tables are correctly formatted based on the input
local function format_options(options, response_string)
return {
response = response_string,
version = API_VERSION,
id = name..'/'..(options.id or ""),
source = name,
request_text = ("[%s] %s"):format(options.source or name, options.request_text or options.text or "requesting user input:"),
default_input = options.default_input,
cursor_pos = tonumber(options.cursor_pos),
queueable = options.queueable and true,
replace = options.replace and true
}
end
-- cancels the request
function request_mt:cancel()
assert(self.uid, "request object missing UID")
mp.commandv("script-message-to", "user_input", "cancel-user-input/uid", self.uid)
end
-- updates the options for the request
function request_mt:update(options)
assert(self.uid, "request object missing UID")
options = utils.format_json( format_options(options) )
mp.commandv("script-message-to", "user_input", "update-user-input/uid", self.uid, options)
end
-- sends a request to ask the user for input using formatted options provided
-- creates a script message to recieve the response and call fn
function mod.get_user_input(fn, options, ...)
options = options or {}
local response_string = name.."/__user_input_request/"..counter
counter = counter + 1
local request = {
uid = response_string,
passthrough_args = pack(...),
callback = fn,
pending = true
}
-- create a callback for user-input to respond to
mp.register_script_message(response_string, function(response)
mp.unregister_script_message(response_string)
request.pending = false
response = utils.parse_json(response)
request.callback(response.line, response.err, unpack(request.passthrough_args, 1, request.passthrough_args.n))
end)
-- send the input command
options = utils.format_json( format_options(options, response_string) )
mp.commandv("script-message-to", "user_input", "request-user-input", options)
return setmetatable(request, { __index = request_mt })
end
-- runs the request synchronously using coroutines
-- takes the option table and an optional coroutine resume function
function mod.get_user_input_co(options, co_resume)
local co, main = coroutine.running()
assert(not main and co, "get_user_input_co must be run from within a coroutine")
local uid = {}
local request = mod.get_user_input(function(line, err)
if co_resume then
co_resume(uid, line, err)
else
local success, er = coroutine.resume(co, uid, line, err)
if not success then
msg.warn(debug.traceback(co))
msg.error(er)
end
end
end, options)
-- if the uid was not sent then the coroutine was resumed by the user.
-- we will treat this as a cancellation request
local success, line, err = coroutine.yield(request)
if success ~= uid then
request:cancel()
request.callback = function() end
return nil, "cancelled"
end
return line, err
end
-- sends a request to cancel all input requests with the given id
function mod.cancel_user_input(id)
id = name .. '/' .. (id or "")
mp.commandv("script-message-to", "user_input", "cancel-user-input/id", id)
end
return mod