diff options
author | Guido Cella <guido@guidocella.xyz> | 2024-05-12 21:17:46 +0200 |
---|---|---|
committer | Kacper Michajłow <kasper93@gmail.com> | 2024-05-12 23:13:48 +0200 |
commit | 367a6b561af42d425a97c9441a6dcb3ae41d2d73 (patch) | |
tree | 8795186d5d00992e5e88fe808e69b0737e778bc0 | |
parent | 30fd3c1a9600d1dadf6f0027c6294e296d325152 (diff) | |
download | mpv-367a6b561af42d425a97c9441a6dcb3ae41d2d73.tar.bz2 mpv-367a6b561af42d425a97c9441a6dcb3ae41d2d73.tar.xz |
console.lua: close when pressing enter with input.select
When you select an item, due to the submit handler being called
asynchronously, the default item list is redrawn before the console
closes, which is jarring. Fix this by always closing the console as soon
as enter is pressed, as keeping it open is unlikely to be useful with a
fuzzy selector (unlike with input.get() where it can be used e.g. to
implement a Lua REPL). If desired we can later add a close_on_submit
flag defaulting to true.
Also fix a crash when pressing enter without any match.
-rw-r--r-- | DOCS/man/lua.rst | 6 | ||||
-rw-r--r-- | player/lua/console.lua | 10 |
2 files changed, 11 insertions, 5 deletions
diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst index 6a58cba54d..e8065d9a1a 100644 --- a/DOCS/man/lua.rst +++ b/DOCS/man/lua.rst @@ -970,8 +970,9 @@ REPL. ``submit`` The callback invoked when the user presses Enter. The first argument is - the 1-based index of the selected item. You can close the console from - within the callback by calling ``input.terminate()``. + 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()``. Example: @@ -984,7 +985,6 @@ REPL. }, submit = function (id) mp.commandv("playlist-play-index", id - 1) - input.terminate() end, }) diff --git a/player/lua/console.lua b/player/lua/console.lua index 30e777e972..26c2e27988 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -798,9 +798,15 @@ function handle_enter() history_add(line) end - if input_caller then + if selectable_items then + if #matches > 0 then + 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({selectable_items and matches[selected_match].index or line})) + utils.format_json({line})) else -- match "help [<text>]", return <text> or "", strip all whitespace local help = line:match('^%s*help%s+(.-)%s*$') or |