-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinput-command.lua
88 lines (70 loc) · 2.85 KB
/
input-command.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
--[[
A script to allow users to define commands which will query mpv-user-input for values.
Available at: https://github.com/CogentRedTester/mpv-scripts
The syntax is:
script-message input-command [command string] [arg1 options] [arg2 options] ...
Some examples:
script-message input-command "loadfile %1" "encapsulate=yes"
script-message input-command "seek %1 %2" "" "default=absolute"
script-message input-command "screenshot-to-file %1" "encapsulate=yes|default=${screenshot-directory:~/Pictures/Screenshots}/${filename/no-ext}.png"
This is very much still a work in progress, some of the options above may not work properly
]]
local mp = require "mp"
local utils = require "mp.utils"
package.path = mp.command_native({"expand-path", "~~/script-modules/?.lua;"}) .. package.path
local input = require "user-input-module"
local function substitute_arg(command, arg, text, opts)
return command:gsub( "%%%%*"..arg, function(str)
local prepended_hashes = str:match("^%%+"):len()
local number = str:sub(prepended_hashes+1)
if number == "" then return false end
str = str:sub(1, math.floor(prepended_hashes/2))
if prepended_hashes % 2 == 0 then return str..number end
if opts.encapsulate then text = string.format("%q", text) end
return str..text
end)
end
local commands = {}
local function main(command, ...)
local num_args, opts
if commands[command] then
num_args = commands[command].num_args
opts = commands[command].opts
else
commands[command] = {}
opts = {...}
for i, opt in ipairs(opts) do
opts[i] = {}
for str in opt:gmatch("[^|]+") do
local key = str:match("^[^=]+")
opts[i][key] = str:sub(key:len()+2)
end
end
commands[command].opts = opts
local args = {}
for str in command:gmatch("%%%%*[%d]*") do
local prepended_hashes = str:match("^%%+"):len()
local number = str:sub(prepended_hashes+1)
if number ~= "" and prepended_hashes % 2 == 1 then
local num = tonumber(number)
args[num] = true
end
end
num_args = #args
commands[command].num_args = num_args
end
local command_copy = command
for i = 1, num_args, 1 do
input.get_user_input(function(text)
if not text then return end
command = substitute_arg(command, i, text, opts[i])
if i == num_args then mp.command(command) end
end, {
id = command..'/'..tostring(i),
queueable = true,
request_text = "Enter argument "..i.." for command: "..command_copy,
default_input = opts[i].default or ""
})
end
end
mp.register_script_message("input-command", main)