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).
Unified · Split
| @@ -63,7 +63,8 @@ local mp_globals = { · 62 unmodified lines | |||
| 63 | set_mouse_area = {}, | 63 | set_mouse_area = {}, |
| 64 | set_osd_ass = {}, | 64 | set_osd_ass = {}, |
| 65 | } | 65 | } |
| 66 | } | 66 | }, |
| 67 | unpack = {}, | ||
| 67 | } | 68 | } |
| 68 | 69 | ||
| 69 | local mp_internal = { | 70 | local mp_internal = { |
| @@ -647,9 +647,10 @@ mp.options = { read_options: read_options }; · 646 unmodified lines | |||
| 647 | * input | 647 | * input |
| 648 | *********************************************************************/ | 648 | *********************************************************************/ |
| 649 | function register_event_handler(t) { | 649 | function register_event_handler(t) { |
| 650 | mp.register_script_message("input-event", function (type, text, cursor_position) { | 650 | mp.register_script_message("input-event", function (type, args) { |
| 651 | if (t[type]) { | 651 | if (t[type]) { |
| 652 | var result = t[type](text, cursor_position); | 652 | args = JSON.parse(args) |
| 653 | var result = t[type](args[0], args[1]); | ||
| 653 | 654 | ||
| 654 | if (type == "complete" && result) { | 655 | if (type == "complete" && result) { |
| 655 | mp.commandv("script-message-to", "console", "complete", | 656 | mp.commandv("script-message-to", "console", "complete", |
| @@ -566,7 +566,7 @@ function set_active(active) · 565 unmodified lines | |||
| 566 | 566 | ||
| 567 | if input_caller then | 567 | if input_caller then |
| 568 | mp.commandv('script-message-to', input_caller, 'input-event', | 568 | mp.commandv('script-message-to', input_caller, 'input-event', |
| 569 | 'closed', line, cursor) | 569 | 'closed', utils.format_json({line, cursor})) |
| 570 | input_caller = nil | 570 | input_caller = nil |
| 571 | line = '' | 571 | line = '' |
| 572 | cursor = 1 | 572 | cursor = 1 |
| @@ -648,7 +648,7 @@ local function handle_edit() · 75 unmodified lines | |||
| 648 | 648 | ||
| 649 | if input_caller then | 649 | if input_caller then |
| 650 | mp.commandv('script-message-to', input_caller, 'input-event', 'edited', | 650 | mp.commandv('script-message-to', input_caller, 'input-event', 'edited', |
| 651 | line) | 651 | utils.format_json({line})) |
| 652 | end | 652 | end |
| 653 | end | 653 | end |
| 654 | 654 | ||
| @@ -787,7 +787,7 @@ function handle_enter() · 132 unmodified lines | |||
| 787 | 787 | ||
| 788 | if input_caller then | 788 | if input_caller then |
| 789 | mp.commandv('script-message-to', input_caller, 'input-event', 'submit', | 789 | mp.commandv('script-message-to', input_caller, 'input-event', 'submit', |
| 790 | selectable_items and matches[selected_match].index or line) | 790 | utils.format_json({selectable_items and matches[selected_match].index or line})) |
| 791 | else | 791 | else |
| 792 | -- match "help [<text>]", return <text> or "", strip all whitespace | 792 | -- match "help [<text>]", return <text> or "", strip all whitespace |
| 793 | local help = line:match('^%s*help%s+(.-)%s*$') or | 793 | local help = line:match('^%s*help%s+(.-)%s*$') or |
| @@ -1168,7 +1168,7 @@ function complete(backwards) · 374 unmodified lines | |||
| 1168 | completion_old_line = line | 1168 | completion_old_line = line |
| 1169 | completion_old_cursor = cursor | 1169 | completion_old_cursor = cursor |
| 1170 | mp.commandv('script-message-to', input_caller, 'input-event', | 1170 | mp.commandv('script-message-to', input_caller, 'input-event', |
| 1171 | 'complete', line:sub(1, cursor - 1)) | 1171 | 'complete', utils.format_json({line:sub(1, cursor - 1)})) |
| 1172 | return | 1172 | return |
| 1173 | end | 1173 | end |
| 1174 | 1174 | ||
| @@ -1586,7 +1586,7 @@ mp.register_script_message('get-input', function (script_name, args) · 411 unmodified lines | |||
| 1586 | args = utils.parse_json(args) | 1586 | args = utils.parse_json(args) |
| 1587 | prompt = args.prompt or default_prompt | 1587 | prompt = args.prompt or default_prompt |
| 1588 | line = args.default_text or '' | 1588 | line = args.default_text or '' |
| 1589 | cursor = tonumber(args.cursor_position) or line:len() + 1 | 1589 | cursor = args.cursor_position or line:len() + 1 |
| 1590 | id = args.id or script_name .. prompt | 1590 | id = args.id or script_name .. prompt |
| 1591 | if histories[id] == nil then | 1591 | if histories[id] == nil then |
| 1592 | histories[id] = {} | 1592 | histories[id] = {} |
| @@ -19,9 +19,10 @@ local utils = require "mp.utils" · 18 unmodified lines | |||
| 19 | local input = {} | 19 | local input = {} |
| 20 | 20 | ||
| 21 | local function register_event_handler(t) | 21 | local function register_event_handler(t) |
| 22 | mp.register_script_message("input-event", function (type, text, cursor_position) | 22 | mp.register_script_message("input-event", function (type, args) |
| 23 | if t[type] then | 23 | if t[type] then |
| 24 | local suggestions, completion_start_position = t[type](text, cursor_position) | 24 | local suggestions, completion_start_position = |
| 25 | t[type](unpack(utils.parse_json(args or "") or {})) | ||
| 25 | 26 | ||
| 26 | if type == "complete" and suggestions then | 27 | if type == "complete" and suggestions then |
| 27 | mp.commandv("script-message-to", "console", "complete", | 28 | mp.commandv("script-message-to", "console", "complete", |