summaryrefslogtreecommitdiff
path: root/mpv/lua-modules/user-input-module.lua
diff options
context:
space:
mode:
authorilotterytea <ilotterytea@proton.me>2024-05-26 20:10:26 +0500
committerilotterytea <ilotterytea@proton.me>2024-05-26 20:10:26 +0500
commit036c889c4a4f7f59d1e1a592586b54c5c5e93005 (patch)
treeaa76d678790abc79f24edf83c17a564eb2c6f65d /mpv/lua-modules/user-input-module.lua
initial commitHEADmaster
Diffstat (limited to 'mpv/lua-modules/user-input-module.lua')
-rw-r--r--mpv/lua-modules/user-input-module.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/mpv/lua-modules/user-input-module.lua b/mpv/lua-modules/user-input-module.lua
new file mode 100644
index 0000000..2c25d42
--- /dev/null
+++ b/mpv/lua-modules/user-input-module.lua
@@ -0,0 +1,49 @@
+--[[
+ 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 mp = require 'mp'
+local mod = {}
+
+local name = mp.get_script_name()
+local counter = 1
+
+-- 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
+
+ -- create a callback for user-input to respond to
+ mp.register_script_message(response_string, function(input, err)
+ mp.unregister_script_message(response_string)
+ fn(err == "" and input or nil, err)
+ end)
+
+ -- send the input command
+ mp.commandv("script-message-to", "user_input", "request-user-input",
+ response_string,
+ name .. '/' .. (options.id or ""), -- id code for the request
+ options.request_text or options.text or (name.." is requesting user input:"),
+ options.default_input or "",
+ options.queueable and "1" or "",
+ options.replace and "1" or ""
+ )
+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)
+end
+
+return mod