From 996c0987257ea369c0ef532bb0e63edce51994b8 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Sun, 23 Feb 2025 15:37:40 +0100 Subject: [PATCH] console.lua: add a keep_open flag to mp.input With input.select this allows building nested submenus without the flicker of console quickly closing and reopening, and also doing multiple selections, to e.g. cycle through different values of a property, or increase a property multiple times. With input.get this is used to change the default behavior to closing by default on submit. It was argued by avih that this is more useful, and indeed Github code search shows that everybody is calling input.terminate() unconditionally on submit, so the impact of the change should be low, and restoring the old behavior is as easy as passing keep_open = true, which is just ignored in older mpv versions. --- DOCS/interface-changes/mp.input.txt | 2 ++ DOCS/man/lua.rst | 14 ++++++++------ player/lua/console.lua | 9 +++++++-- player/lua/stats.lua | 1 - 4 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 DOCS/interface-changes/mp.input.txt diff --git a/DOCS/interface-changes/mp.input.txt b/DOCS/interface-changes/mp.input.txt new file mode 100644 index 0000000000..b6a1119b6d --- /dev/null +++ b/DOCS/interface-changes/mp.input.txt @@ -0,0 +1,2 @@ +add `keep_open` flag to `mp.input.get()` and `mp.input.select()` +`mp.input.get()` now closes automatically on submit, pass `keep_open = true` to restore the old behavior diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst index 51f684c4f9..bb883ced18 100644 --- a/DOCS/man/lua.rst +++ b/DOCS/man/lua.rst @@ -926,9 +926,10 @@ REPL. ``submit`` A callback invoked when the user presses Enter. The first argument is - the text in the console. You can close the console from within the - callback by calling ``input.terminate()``. If you don't, the console - stays open and the user can input more text. + the text in the console. + + ``keep_open`` + Whether to keep the console open on submit. Defaults to ``false``. ``opened`` A callback invoked when the console is shown. This can be used to @@ -1013,9 +1014,10 @@ REPL. ``submit`` The callback invoked when the user presses Enter. The first argument is - the 1-based index of the selected item. Unlike with ``input.get()``, the - console is automatically closed on submit without having to call - ``input.terminate()``. + the 1-based index of the selected item. + + ``keep_open`` + Whether to keep the console open on submit. Defaults to ``false``. Example: diff --git a/player/lua/console.lua b/player/lua/console.lua index b64b54f0a9..2a6a8d3863 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -104,6 +104,7 @@ local dont_bind_up_down = false local overlay = mp.create_osd_overlay('ass-events') local global_margins = { t = 0, b = 0 } local input_caller +local keep_open = false local completion_buffer = {} local selected_completion_index @@ -1093,7 +1094,6 @@ local function submit() mp.commandv('script-message-to', input_caller, 'input-event', 'submit', utils.format_json({matches[selected_match].index})) end - set_active(false) elseif input_caller then mp.commandv('script-message-to', input_caller, 'input-event', 'submit', utils.format_json({line})) @@ -1116,7 +1116,11 @@ local function submit() history_add(line) end - clear() + if input_caller and not keep_open then + set_active(false) + elseif not selectable_items then + clear() + end end local function determine_hovered_item() @@ -2107,6 +2111,7 @@ mp.register_script_message('get-input', function (script_name, args) line = args.default_text or '' cursor = args.cursor_position or line:len() + 1 id = args.id or script_name .. prompt + keep_open = args.keep_open dont_bind_up_down = args.dont_bind_up_down if histories[id] == nil then histories[id] = {} diff --git a/player/lua/stats.lua b/player/lua/stats.lua index dd3bb24ce2..c8d74b2f5a 100644 --- a/player/lua/stats.lua +++ b/player/lua/stats.lua @@ -1562,7 +1562,6 @@ local function filter_bindings() display_timer:resume() end end, - submit = input.terminate, closed = function () searched_text = nil if display_timer:is_enabled() then