mirror of
https://github.com/mpv-player/mpv
synced 2025-02-02 13:12:05 +00:00
eb4c6be630
I misunderstood CogentRedTester's review in https://github.com/mpv-player/mpv/pull/10282#discussion_r1428972352 as referring to the cursor_position in mp.input's arguments instead of the one received by the closed callback. The cursor_position passed to input.get doesn't need to be converted to a number because it was already JSON, while the cursor_position received by the closed callback is currently a string, and we need to pass JSON in input-event script messages to keep it as an integer to work around mpv converting integer script message arguments to string. This is more noticeable after implementing mp.input.select(): its submit argument currently receives the selected index as a string, and this makes Lua error if you use it as an index of a numerical table, e.g.: submit = function (id) mp.set_property(property, tracks[tonumber(id)].selected and "no" or tracks[tonumber(id)].id) ... end, This commit avoids having to call tonumber(id).
84 lines
2.6 KiB
Lua
84 lines
2.6 KiB
Lua
--[[
|
|
This file is part of mpv.
|
|
|
|
mpv is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
mpv is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
]]
|
|
|
|
local utils = require "mp.utils"
|
|
local input = {}
|
|
|
|
local function register_event_handler(t)
|
|
mp.register_script_message("input-event", function (type, args)
|
|
if t[type] then
|
|
local suggestions, completion_start_position =
|
|
t[type](unpack(utils.parse_json(args or "") or {}))
|
|
|
|
if type == "complete" and suggestions then
|
|
mp.commandv("script-message-to", "console", "complete",
|
|
utils.format_json(suggestions), completion_start_position)
|
|
end
|
|
end
|
|
|
|
if type == "closed" then
|
|
mp.unregister_script_message("input-event")
|
|
end
|
|
end)
|
|
end
|
|
|
|
function input.get(t)
|
|
mp.commandv("script-message-to", "console", "get-input",
|
|
mp.get_script_name(), utils.format_json({
|
|
prompt = t.prompt,
|
|
default_text = t.default_text,
|
|
cursor_position = t.cursor_position,
|
|
id = t.id,
|
|
}))
|
|
|
|
register_event_handler(t)
|
|
end
|
|
|
|
function input.select(t)
|
|
mp.commandv("script-message-to", "console", "get-input",
|
|
mp.get_script_name(), utils.format_json({
|
|
prompt = t.prompt,
|
|
items = t.items,
|
|
default_item = t.default_item,
|
|
}))
|
|
|
|
register_event_handler(t)
|
|
end
|
|
|
|
function input.terminate()
|
|
mp.commandv("script-message-to", "console", "disable")
|
|
end
|
|
|
|
function input.log(message, style, terminal_style)
|
|
mp.commandv("script-message-to", "console", "log", utils.format_json({
|
|
text = message,
|
|
style = style,
|
|
terminal_style = terminal_style,
|
|
}))
|
|
end
|
|
|
|
function input.log_error(message)
|
|
mp.commandv("script-message-to", "console", "log",
|
|
utils.format_json({ text = message, error = true }))
|
|
end
|
|
|
|
function input.set_log(log)
|
|
mp.commandv("script-message-to", "console", "set-log", utils.format_json(log))
|
|
end
|
|
|
|
return input
|