History

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).

Guido Cella committed 2 years ago

Commit eb4c6be1 parent 994a08f4 files · +13 −10Browse files

Unified · Split

4 files
  1. .luacheckrc +2 −1
  2. player/javascript/defaults.js +3 −2
  3. player/lua/console.lua +5 −5
  4. player/lua/input.lua +3 −2
.luacheckrc+2−1
@@ -63,7 +63,8 @@ local mp_globals = { · 62 unmodified lines
6363 set_mouse_area = {},
6464 set_osd_ass = {},
6565 }
66− }
66+ },
67+ unpack = {},
6768 }
6869
6970 local mp_internal = {
player/javascript/defaults.js+3−2
@@ -647,9 +647,10 @@ mp.options = { read_options: read_options }; · 646 unmodified lines
647647 * input
648648 *********************************************************************/
649649 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) {
651651 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]);
653654
654655 if (type == "complete" && result) {
655656 mp.commandv("script-message-to", "console", "complete",
player/lua/console.lua+5−5
@@ -566,7 +566,7 @@ function set_active(active) · 565 unmodified lines
566566
567567 if input_caller then
568568 mp.commandv('script-message-to', input_caller, 'input-event',
569− 'closed', line, cursor)
569+ 'closed', utils.format_json({line, cursor}))
570570 input_caller = nil
571571 line = ''
572572 cursor = 1
@@ -648,7 +648,7 @@ local function handle_edit() · 75 unmodified lines
648648
649649 if input_caller then
650650 mp.commandv('script-message-to', input_caller, 'input-event', 'edited',
651− line)
651+ utils.format_json({line}))
652652 end
653653 end
654654
@@ -787,7 +787,7 @@ function handle_enter() · 132 unmodified lines
787787
788788 if input_caller then
789789 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}))
791791 else
792792 -- match "help [<text>]", return <text> or "", strip all whitespace
793793 local help = line:match('^%s*help%s+(.-)%s*$') or
@@ -1168,7 +1168,7 @@ function complete(backwards) · 374 unmodified lines
11681168 completion_old_line = line
11691169 completion_old_cursor = cursor
11701170 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)}))
11721172 return
11731173 end
11741174
@@ -1586,7 +1586,7 @@ mp.register_script_message('get-input', function (script_name, args) · 411 unmodified lines
15861586 args = utils.parse_json(args)
15871587 prompt = args.prompt or default_prompt
15881588 line = args.default_text or ''
1589− cursor = tonumber(args.cursor_position) or line:len() + 1
1589+ cursor = args.cursor_position or line:len() + 1
15901590 id = args.id or script_name .. prompt
15911591 if histories[id] == nil then
15921592 histories[id] = {}
player/lua/input.lua+3−2
@@ -19,9 +19,10 @@ local utils = require "mp.utils" · 18 unmodified lines
1919 local input = {}
2020
2121 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)
2323 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 {}))
2526
2627 if type == "complete" and suggestions then
2728 mp.commandv("script-message-to", "console", "complete",

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t