Pass everything in the tables passed by mp.input callers to console.lua so new flags can be added without modifying these clients. In Lua, callbacks have to be excluded from the argument tables to not make utils.format_json() error, while with JSON.stringify they are automatically omitted.
Showing the changes to player/lua/input.lua (1 of 2 files). Show all
Unified · Split
| @@ -18,6 +18,18 @@ License along with mpv. If not, see <http://www.gnu.org/licenses/>. · 17 unmodified lines | |||
| 18 | local utils = require "mp.utils" | 18 | local utils = require "mp.utils" |
| 19 | local input = {} | 19 | local input = {} |
| 20 | 20 | ||
| 21 | local function get_non_callbacks(t) | ||
| 22 | local non_callbacks = {} | ||
| 23 | |||
| 24 | for key, value in pairs(t) do | ||
| 25 | if type(value) ~= "function" then | ||
| 26 | non_callbacks[key] = value | ||
| 27 | end | ||
| 28 | end | ||
| 29 | |||
| 30 | return non_callbacks | ||
| 31 | end | ||
| 32 | |||
| 21 | local function register_event_handler(t) | 33 | local function register_event_handler(t) |
| 22 | mp.register_script_message("input-event", function (type, args) | 34 | mp.register_script_message("input-event", function (type, args) |
| 23 | if t[type] then | 35 | if t[type] then |
| @@ -38,26 +50,12 @@ end · 14 unmodified lines | |||
| 38 | 50 | ||
| 39 | function input.get(t) | 51 | function input.get(t) |
| 40 | mp.commandv("script-message-to", "console", "get-input", | 52 | mp.commandv("script-message-to", "console", "get-input", |
| 41 | mp.get_script_name(), utils.format_json({ | 53 | mp.get_script_name(), utils.format_json(get_non_callbacks(t))) |
| 42 | prompt = t.prompt, | ||
| 43 | default_text = t.default_text, | ||
| 44 | cursor_position = t.cursor_position, | ||
| 45 | id = t.id, | ||
| 46 | })) | ||
| 47 | 54 | ||
| 48 | register_event_handler(t) | 55 | register_event_handler(t) |
| 49 | end | 56 | end |
| 50 | 57 | ||
| 51 | function input.select(t) | 58 | input.select = input.get |
| 52 | mp.commandv("script-message-to", "console", "get-input", | ||
| 53 | mp.get_script_name(), utils.format_json({ | ||
| 54 | prompt = t.prompt, | ||
| 55 | items = t.items, | ||
| 56 | default_item = t.default_item, | ||
| 57 | })) | ||
| 58 | |||
| 59 | register_event_handler(t) | ||
| 60 | end | ||
| 61 | 59 | ||
| 62 | function input.terminate() | 60 | function input.terminate() |
| 63 | mp.commandv("script-message-to", "console", "disable") | 61 | mp.commandv("script-message-to", "console", "disable") |