From eb4c6be6306ee17846cd88b50460e10967dea951 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Wed, 8 May 2024 10:22:39 +0200 Subject: [PATCH] console.lua: don't convert integers for mp.input to string 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). --- .luacheckrc | 3 ++- player/javascript/defaults.js | 5 +++-- player/lua/console.lua | 10 +++++----- player/lua/input.lua | 5 +++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index d403ee196f..34ccf1e507 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -63,7 +63,8 @@ local mp_globals = { set_mouse_area = {}, set_osd_ass = {}, } - } + }, + unpack = {}, } local mp_internal = { diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 9150cc8fa3..6493e38680 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -647,9 +647,10 @@ mp.options = { read_options: read_options }; * input *********************************************************************/ function register_event_handler(t) { - mp.register_script_message("input-event", function (type, text, cursor_position) { + mp.register_script_message("input-event", function (type, args) { if (t[type]) { - var result = t[type](text, cursor_position); + args = JSON.parse(args) + var result = t[type](args[0], args[1]); if (type == "complete" && result) { mp.commandv("script-message-to", "console", "complete", diff --git a/player/lua/console.lua b/player/lua/console.lua index 005a5d59f2..a670dace0a 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -566,7 +566,7 @@ function set_active(active) if input_caller then mp.commandv('script-message-to', input_caller, 'input-event', - 'closed', line, cursor) + 'closed', utils.format_json({line, cursor})) input_caller = nil line = '' cursor = 1 @@ -648,7 +648,7 @@ local function handle_edit() if input_caller then mp.commandv('script-message-to', input_caller, 'input-event', 'edited', - line) + utils.format_json({line})) end end @@ -787,7 +787,7 @@ function handle_enter() if input_caller then mp.commandv('script-message-to', input_caller, 'input-event', 'submit', - selectable_items and matches[selected_match].index or line) + utils.format_json({selectable_items and matches[selected_match].index or line})) else -- match "help []", return or "", strip all whitespace local help = line:match('^%s*help%s+(.-)%s*$') or @@ -1168,7 +1168,7 @@ function complete(backwards) completion_old_line = line completion_old_cursor = cursor mp.commandv('script-message-to', input_caller, 'input-event', - 'complete', line:sub(1, cursor - 1)) + 'complete', utils.format_json({line:sub(1, cursor - 1)})) return end @@ -1586,7 +1586,7 @@ mp.register_script_message('get-input', function (script_name, args) args = utils.parse_json(args) prompt = args.prompt or default_prompt line = args.default_text or '' - cursor = tonumber(args.cursor_position) or line:len() + 1 + cursor = args.cursor_position or line:len() + 1 id = args.id or script_name .. prompt if histories[id] == nil then histories[id] = {} diff --git a/player/lua/input.lua b/player/lua/input.lua index a29289a163..3c1584b2ef 100644 --- a/player/lua/input.lua +++ b/player/lua/input.lua @@ -19,9 +19,10 @@ local utils = require "mp.utils" local input = {} local function register_event_handler(t) - mp.register_script_message("input-event", function (type, text, cursor_position) + mp.register_script_message("input-event", function (type, args) if t[type] then - local suggestions, completion_start_position = t[type](text, cursor_position) + 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",