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
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
69local mp_internal = {70local mp_internal = {
player/javascript/defaults.js+3−2
@@ -647,9 +647,10 @@ mp.options = { read_options: read_options }; · 646 unmodified lines
647* input647* input
648*********************************************************************/648*********************************************************************/
649function register_event_handler(t) {649function 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",
player/lua/console.lua+5−5
@@ -566,7 +566,7 @@ function set_active(active) · 565 unmodified lines
566 566
567 if input_caller then567 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 = nil570 input_caller = nil
571 line = ''571 line = ''
572 cursor = 1572 cursor = 1
@@ -648,7 +648,7 @@ local function handle_edit() · 75 unmodified lines
648 648
649 if input_caller then649 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 end652 end
653end653end
654 654
@@ -787,7 +787,7 @@ function handle_enter() · 132 unmodified lines
787 787
788 if input_caller then788 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 else791 else
792 -- match "help [<text>]", return <text> or "", strip all whitespace792 -- match "help [<text>]", return <text> or "", strip all whitespace
793 local help = line:match('^%s*help%s+(.-)%s*$') or793 local help = line:match('^%s*help%s+(.-)%s*$') or
@@ -1168,7 +1168,7 @@ function complete(backwards) · 374 unmodified lines
1168 completion_old_line = line1168 completion_old_line = line
1169 completion_old_cursor = cursor1169 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 return1172 return
1173 end1173 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_prompt1587 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() + 11589 cursor = args.cursor_position or line:len() + 1
1590 id = args.id or script_name .. prompt1590 id = args.id or script_name .. prompt
1591 if histories[id] == nil then1591 if histories[id] == nil then
1592 histories[id] = {}1592 histories[id] = {}
player/lua/input.lua+3−2
@@ -19,9 +19,10 @@ local utils = require "mp.utils" · 18 unmodified lines
19local input = {}19local input = {}
20 20
21local function register_event_handler(t)21local 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] then23 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 then27 if type == "complete" and suggestions then
27 mp.commandv("script-message-to", "console", "complete",28 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