Commit Graph
9 Commits
Author SHA1 Message Date
Guido Cella e28c6ceea3 console.lua: default to --osd-font without completions
Currently --osd-font is the default for select, and a monospace font is
the default for free-form text mode. We can also default to the
proportional --osd-font for mp.input.get clients that don't specify
completions and thus don't need alignment, which is most clients.

In builtin scripts, this is the case for filtering stats key bindings.
2025-03-08 19:09:23 +00:00
Guido Cella 3c175f0114 console.lua: let the complete callback return a character to append
Let the complete callback return the character to append to the selected
completion.

I didn't want to document this because it's specifically meant for
properties and unlikely to be needed by other scripts, but silently
interpreting the 3rd return value can screw you over if you do something
like

return candidates, code:find('[%w_]*$')

I was doing this in my lua-repl script, and find() returns the end
position as the 2rd argument, this becomes the 3rd return value and gets
appended to completions. So you have to do

return candidates, code:find('[%w_]*$'), nil

Needed to split running commands out of console.lua.
2025-03-08 19:09:23 +00:00
Guido Cella bd9d5e3184 input.lua/defaults.js: unregister input-even again
Partially revert bede44618b. This was removed to allow nested
input.select() calls, but it is better to use the new keep_open flag for
this, because it avoids the flicker of console quickly closing and
reopening.

Unregistering the script message allows garbage collection of variables
defined in the scope that called mp.input, this is useful e.g. if you
were selecting from 10k history entries.
2025-02-24 16:26:58 +01:00
Guido Cella bede44618b {console,input}.lua: allow subsequent mp.input calls
Currently using mp.input while the console is open doesn't do anything.
We can allow using mp.input multiple times in a row by keeping the
console open, and sending a closed event only when the previous calling
script is different from the current one. If it is the same, sending a
closed event would unregister the new input-event listener, so it is
just never sent in this case as there is no sane way to do it. This is
useful e.g. to try out select script bindings in the console, or to
switch to a different mp.input script binding with a key binding without
having to close the current one.

Calling input.select() again within input.select()'s submit handler
needs to be fixed separately because with select the console is
immediately closed on submit (367a6b561a), so input.lua receives submit,
requests a new selection, and receives closed wich unregisters the
handler of the new selection. Just not calling
mp.unregister_script_message("input-event") fixes it; its only purpose
was to allow garbage collection of variables defined in the scope using
mp.input.

Fixes #15795.
2025-02-17 02:32:43 +00:00
Guido Cella b3f8464fa9 input.lua,defaults.js: don't hardcode mp.input arguments
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.
2024-10-04 21:13:29 +03:00
Guido Cella eb4c6be630 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).
2024-05-12 23:13:48 +02:00
Guido Cella 2354d876da scripting: add mp.input.select()
This allows scripts to make the user choose from a list of entries by
typing part of their text and/or by navigating them with keybindings,
like dmenu or fzf.

Closes #13964.
2024-05-07 22:34:22 +02:00
Guido Cella dfecc9f083 console.lua: style log lines in the terminal
When running the console in the terminal, style log lines with the same
escape sequences as msg.c.

mp.input can also specify terminal escape sequences, e.g. a script to
select a playlist entry can invert the color of the selection.

Also add a missing newline to help's error message.
2024-01-14 23:26:07 +01:00
Guido Cella 871f7a152a scripting: add mp.input
This lets scripts get textual input from the user using console.lua.
2024-01-13 02:53:08 +00:00