279 Commits

Author SHA1 Message Date
Hubert Głuchowski ac3b6044d0 console.lua: use terminal_display_width when picking longest menu item
Previously this function would just count bytes which was very easily
broken by incantations like `Ő̶̧͍̹͔̮̮̦͌̈̌̂́̊͜K͖̮̼̯͖͆͋̓̀̕` which take up 52 bytes while being just
2 normal width characters visually.

`terminal_display_width` correctly counts `Ő̶̧͍̹͔̮̮̦͌̈̌̂́̊͜K͖̮̼̯͖͆͋̓̀̕` as 2 visual characters.

Note: GitHub strips all these in their web UI but there are many Unicode
combining characters after each letter of the `OK`s above that produce
a "cursed" text effect.

Fixes #17772
2026-04-21 18:17:22 +02:00
nanahi 59d7315fe2 console.lua: use update-clipboard command
This command is now required when the default has no clipboard monitoring.
2026-03-21 16:05:36 +01:00
CogentRedTester 429fdcf21d mp.input: allow clients to override console.lua script-opts
This commit allows a table of script-opt overrides to be passed to
`mp.input.get()` and `mp.input.select()`, which console.lua will use
instead of the its own.

To minimise required changes, metatables are used to allow console.lua
to seamlessly fall back on the original script-opts if the client does
not provide an override.
To prevent exceptions, the incoming opts must be of the same type.
2026-03-07 12:31:50 +01:00
CogentRedTester a96bbf5c17 mp.input: sanitise client inputs
This commit should remove any unexpected behaviour or crashes
that
may be caused by users passing unexpected values to mp.input functions.

String and numerical options are now explicitly converted to strings and
numbers. Values which should be tables are set to empty tables, and
warning messages are printed to inform the user.
2026-02-27 09:51:14 +01:00
CogentRedTester 4d26c4e42c console.lua: guard against invalid arguments to script-messages
All but one of these guards should never be tripped
if people are using the mp.input
module. However, it could happen if non-lua/js clients were to use the
JSON API directly.

Currently, without these guards, calling these script messages with
incorrect arguments will almost certainly crash `console.lua`, hence
preventing any client from requesting input.

An error message has also been added for each guard to provide some
feedback as to why the arguments were invalid.
2026-02-27 09:51:14 +01:00
CogentRedTester 4bb048c227 console.lua: prevent outdated completions from being selected
This commit flags when console.lua is waiting for completions, and
disables the completion cycle key until up-to-date completions are
received.

While this is happening, completions are not printed when in terminal
output mode. In normal OSD mode, the completions are still printed, but
at reduced opacity. This is to avoid distracting and ugly flickering
from completions being rapidly erased and redrawn.

The completion results are only dimmed after a short (sub-second) delay.
This is to avoid distracting flickers from the opacity
rapidly changing for clients that return completions almost immediately,
e.g., commands.lua.
2026-02-24 20:19:24 +01:00
CogentRedTester 5a1700a456 mp.input: avoid displaying outdated completions from the same client
Previously, completion responses from the same client would be displayed
even if the user had since changed the input, as long as a newer
complete event had already been sent. This meant that, if clients took a
sufficiently long time to return completions, then
console.lua would display completions for previous versions of the line
which were no longer valid.

This commit has the client send back the version of the line their
completions apply to, ensuring that `console.lua` displays the
correct completions.
2026-02-24 20:19:24 +01:00
CogentRedTester ae965e7a7e console.lua: remove undocumented dont_bind_up_down option
This undocumented option was used by `stats.lua` to prevent the page
scroll keys from being overridden by `console.lua`. The previous commits
have removed the need for this hack, so it is now safe to remove.
2026-02-24 20:12:57 +01:00
CogentRedTester 0af0c137bb console.lua: flush keybinds before sending mp.input opened events
This commit uses the undocumented `mp.flush_keybindings()` method
to ensure that all `console.lua` keybinds are set before
sending the `opened` event to `mp.input` clients.

Previously, the keybinds would only be flushed when `console.lua` went
idle. As the `opened` event is sent before this happens, clients
processing the event may end up flushing their own keybinds before this
happens. This makes it unreliable to use the `opened` event to override
keybinds set by `console.lua`; for example, adding different
behaviour to `input.select` when using `Shift+Enter` instead of `Enter`.

This commit allows the `opened` callback to be used to safely override
`console.lua` keybinds.

.luacheckrc: move flush_keybindings to globals
2026-02-24 20:12:57 +01:00
Guido Cella ee9a219600 console.lua: deduplicate code between get-input and handle_cursor_move 2026-02-23 17:40:11 +01:00
verygoodlee c5c8e8a39a console.lua: allow scrolling up the log until only one line remains
Currently, if some log lines have very long text that actually
renders across multiple lines, even if you scroll to the top,
the topmost content will be pushed out of the screen by these
lines and will never be visible.

In STATS page 2/4/5/0, you can scroll down until there is one
line left on the screen, we can keep the scrolling behavior of
the log consistent with it.
2026-01-30 10:53:07 +01:00
CogentRedTester 2aa727bc3d mp.input: send buffer ids with logs to avoid race conditions
This commit modifies the log methods in mp.input to always send the id
of the latest `input.get()` request with log entries.

Previously, the log methods applied to whichever input request happened
to be open when the log message was received. Even when scripts used
these methods correctly, there was the risk of sending a log to the
wrong log buffer if the active input request changed while the log
message was in transit; a race condition.

Now the id of the latest `input.get()` request is sent alongside the log
messages, preventing data races between scripts while also preventing
those logs from being discarded.
2026-01-22 00:04:35 +01:00
CogentRedTester 3ed9b798bb mp.input: avoid rare race condition when supplying completions
It was previously possible for completion messages to be received by a
new input request, if one was created while the message was in transit.

Since it is trivial to avoid this by passing the script name and
existing handle_id value, we may as well do so and guarantee that there
will not be any data races.
2026-01-22 00:04:35 +01:00
CogentRedTester 5d469e6bf5 mp.input: remove race conditions when calling input.terminate()
This commit ensures that `input.terminate()` can only close input
requests made by the same script, and prevents any in-transit events
for old input requests from being processed.

Previously, there was no way to guarantee that the input request being
terminated was the one intended; the asynchronous nature of the API
meant that it was always possible (though unlikely) that another client
may have activated its own input request while the termination request
was in transit.
This commit removes the race condition between different scripts calling
`input.terminate()` by sending the script name alongside the termination
message.

In addition, when a script overwrites one of its own input requests,
there may be incoming events still in transit. Some of these events may
have a decent chance of calling `input.terminate()` if they are
processed (e.g., `submit`). This commit avoids this issue by only
processing `closed` requests once a new `input.get()` request is made.
2026-01-22 00:04:35 +01:00
CogentRedTester 17551b2d43 mp.input: use unique event handlers for input.get requests
This makes changes to mp.input and console.lua so that every input.get
request uses a unique script-message to handle input events.

Previously, making new input.get requests shortly after the termination
of a previous request made by the same script could cause a race
condition where the input handler was closed but the new request
was still being drawn in the UI. This was caused by the `closed` event
for the previous request being received only after the new request was
registered, hence closing the event handler for the new request instead.

In addition, this commit makes the behaviour of calling input.get while
another request is active more consistent. When a new request is
received it overwrites the in-progress request, sending a `closed`
event. However, previously, the `closed` event could not be sent if both
requests came from the same script, as the new request would have
overwritten the event handler. Now, the `closed` event is called
regardless of where the new request comes from.
2026-01-22 00:04:35 +01:00
Guido Cella 85bf9f4ff4 console.lua: only clip items between UTF-8 characters
Since with the new horizontal scrolling the end of lines can be visible,
ensure not to clip lines in the middle of UTF-8 characters to not show
mojibake, also adding … to show when items are clipped.

Also replace the existing use of ⋯  with …
2026-01-07 01:09:00 +01:00
Guido Cella a385c0635b console.lua: implement horizontal scroll
Bind Shift+LEFT, Shift+RIGHT, Shift+WHEEL_DOWN, Shift+WHEEL_UP,
WHEEL_LEFT, WHEEL_RIGHT to scroll the menu horizontally.

This lets you see long items that get clipped, like long history entries
and key bindings.
2026-01-07 01:09:00 +01:00
Guido Cella 86466b5745 console.lua: bind Shift+UP and DOWN to scroll the log
We don't know how many log lines wrap, so scrolling the log with page up
and down with wrapped lines would skip lines, and those are also already
bound to history navigation.

This therefore only implements scrolling of one item at a time. While
not perfect because when the item is wrapped it scrolls multiple lines,
it is usable.

The number of log lines kept in memory is increased from 100 to 10000.

Closes https://github.com/mpv-player/mpv/discussions/14718.
2026-01-07 01:08:00 +01:00
verygoodlee 3315ef8f32 console.lua: avoid empty lines at the bottom after resizing window
with `scale_with_window=no` script option, `first_match_to_print`
increases when shrinking the window and no longer decreases when
expanding the window, causing empty lines at the bottom of menu
and a chain reaction of the script crashing due to the mouse wheel
scrolling up.

this commit trys to decrease `first_match_to_print` when its value
is abnormal.

Closes #16682
2026-01-07 01:01:39 +01:00
Guido Cella 5cc1094ad6 console.lua: remove selected_color and selected_back_color
42660a8c23 renamed selected_color and selected_back_color to
focused_color and focused_back_color. The new names have been in a
release so remove the backwards compatibility check.
2025-12-30 17:03:48 +01:00
Guido Cella 52f2c670d5 console.lua: don't error if compute_bounds doesn't return dimensions
compute_bounds doesn't return any field if the rectangle is empty: its
docs say "If set to true, attempt to determine bounds and write them to
the command's result value as x0, x1, y0, y1 rectangle (default:
false). If the rectangle is empty, not known, or somehow degenerate, it
is not set.". So shrinking mpv as much as possible with a menu open
caused a crash.

This doesn't happen on Xorg because of these lines in video/out/x11_common.c:

// Set minimum height/width to 4 to avoid off-by-one errors.
hint->flags |= PMinSize;
hint->min_width = hint->min_height = 4;

nor on macOS because of

minSize = NSSize(width: 160, height: 90)

in video/out/mac/window.swift.

But this is needed on Wayland and Windows.

Fixes #16682 along with the diff posted there.
2025-12-27 15:41:44 +01:00
Guido Cella 1abd2a05f1 console.lua: don't clip items if OSD dimensions are not initialized
If you open the menu at startup, e.g. with --input-commands, it rarely
clips items completely because OSD dimensions are still 0, or as if they
were 100 because those are the default values before receiving property
notifications. Avoid this.
2025-10-31 12:53:31 +01:00
Guido Cella e2bd1c6a88 {console/context_menu}.lua: don't apply --osd-blur to menu backgrounds 2025-10-29 15:36:35 +01:00
nanahi 7b660a5de9 console.lua: use clipboard property for x11 2025-10-27 20:01:47 +01:00
Guido Cella eac9ab275d console.lua: add gap script-opt 2025-09-27 19:26:54 +02:00
Guido Cella d8123d81bd console.lua: increase the gap between menu items
Make it less cluterred.
2025-09-27 19:26:54 +02:00
Guido Cella b9ceaf243d console.lua: don't autoselect the wrong completion
The first completion is automatically inserted when Tab was not pressed,
but this is unwanted when you already typed one of the completions which
is not the first one, move back the cursor and press Enter.

e.g. if the line is "set vid", you place the cursor before "v" and press
Enter, it runs "set ab-loop-avid".

Fix this by aborting when the text in front of the cursor matches one of
the completions.
2025-09-02 21:43:20 +02:00
Guido Cella 614e3c4223 console.lua: bind Ctrl+y to copy
Make Ctrl+y copy the input line to the clipboard, or the currently
selected item in the menu.

The vi key to copy is used because Ctrl+c is already bound.

Alternative bindings are:
Ctrl+x to cut, but this also clears the line.
Ctrl+shift+c like in terminals, but this is harder to press, and this
commit works differently from terminals anyway since you don't manually
highlight characters, it just copies the whole line.
Alt+w, the emacs copy binding, but unless you use emacs this is weird
and unfamiliar.
2025-07-05 01:20:02 +02:00
Guido Cella d702e5fec8 console.lua: set compute_bounds and hidden at startup
There is no need to set them every time the width is calculated.
2025-05-03 22:45:10 +02:00
Guido Cella 4194522344 console.lua: rename option_color_to_ass to color_option_to_ass
It is more correct English.
2025-05-03 22:45:10 +02:00
Guido Cella 3600c716ea console.lua: fix crash when pressing Ctrl+l
Pressing Ctrl+l with selectable items without having called input.get()
earlier caused a crash.

Fixes a6024b562a.
2025-04-18 12:02:47 +02:00
Guido Cella 498a9bf464 console.lua: center correctly within OSC margins
Center in the middle of the rectangle surrounded by the OSC instead of
in the center of the window.
2025-04-17 00:17:01 +02:00
Guido Cella 4dfe92fa27 console.lua: replace font script-opt with monospace_font
Allow configuring the monospace font without also changing the
proportional font. The proportional font is configured with the standard
--osd-font.
2025-04-17 00:13:19 +02:00
Guido Cella 42660a8c23 console.lua: say focused instead of selected
The selected term was causing confusion e.g. in
https://github.com/mpv-player/mpv/pull/15711#issuecomment-2676060139.
Saying either focused or current makes more sense since the item is not
actually selected until you click or press Enter.
2025-04-17 00:08:16 +02:00
Guido Cella d059f648fa console.lua: repurpose case_sensitive to only affect exact searches
case_sensitive originally only affected Tab completion. It defaulted to
yes outside of Windows just to emulate the bad defaults of bash and zsh
(not fish, which has better defaults and defaults to case-insensitive
completion). This was already silly, but then it affected the fuzzy
autocompletion and case-sensitive fuzzy completion is even more silly.
And since 0b3cc3a167, it accidentally stopped affecting even that,
because the completion via script message never checked for the option.
Nobody even noticed this, meaning that nobody was relying on fuzzy
autocompletion being case-sensitive.

Just make case_sensitive only affect the new exact search, and make it
default to no on all platforms. Though IMO the search could just always
be case-insensitive.
2025-04-17 00:01:27 +02:00
Guido Cella 4cb5f7aa1f console.lua: implement exact matching
Allow matching items exactly instead of fuzzily when prefixing the
search with ' (like in fzf) or with
--script-opt=console-exact_match=yes.

This is mainly useful to filter history entries chronologically.

You can also specify multiple search terms delimited by spaces. But '
makes every term an exact match, unlike fzf where you need to place it
before every word, since we can't hook into fzy's algorithm.

Closes #14587.
2025-04-17 00:01:27 +02:00
showergum 44816ef8ff console.lua: default prompt to an empty string
Calling input.select() without a prompt argument was failing due to
attempting to concatenate a nil value.

Before mpv 0.40 the prompt defaulted to ">" so this is expected to
work as evident from the example code.
2025-04-12 20:55:34 +02:00
Guido Cella ba80e447a6 console.lua: use a variadic function 2025-03-26 14:48:37 +01:00
Guido Cella 6ec0d06aa9 console.lua: use double quotes
Make it consistent with other scripts.
2025-03-26 02:53:01 +01:00
Dudemanguy 6c42182522 Revert "console.lua: disable cursor autohide while selector is open"
In retrospect, this isn't so great because not neccesarily everyone
would want the mouse to show with the menu and said menu can be
navigated with the keyboard anyway.

This reverts commit a8f5beb5a3.
2025-03-15 05:19:43 +00:00
verygoodlee a8f5beb5a3 console.lua: disable cursor autohide while selector is open
This is an improvement on #15145 .
If the cursor is automatically hidden, users may not know whether
they are clicking on the inside or outside of selectable items,
the result after clicking is unclear to them.
2025-03-14 15:55:55 +00:00
Guido Cella 4c26e7d2ed console.lua: fix crash when pressing Ctrl+c with select
Pressing ctrl+c in select mode crashes if console wasn't opened in
free-form text mode before.

This also makes the type of the history variable clear.

Fixes a6024b562a.
2025-03-10 17:06:02 +01:00
Kacper Michajłow d37521b5ef console.lua: cache observed properties to avoid unnecessary fetching
Those properties are already observed and trigger update, remember the
value to avoid re-fetching them.

This fixes delay caused by properties that synchronize GUI thread like
`focused` which takes 30+ ms in certain cases.

Co-authored-by: Guido Cella <guido@guidocella.xyz>
2025-03-09 11:45:30 +01:00
Guido Cella 8385c2b5e8 console.lua: convert cursor_position to number
script-message-to commands type was passing cursor_position as a string,
which caused a crash if you e.g. moved the cursor afterwards. Convert it
to number in console so commands.lua and similar clients don't have to
manually convert it.
2025-03-08 19:09:23 +00:00
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 d738e787a0 console.lua: instantly show completions with the type script message
With script-message-to commands type, instantly show completions if
available. Useful e.g. with loadfile or apply-profile.

This also shows completions if you just close the console with a filled
line and reopen it.
2025-03-08 19:09:23 +00:00
Guido Cella 415078ba2d console.lua: fix the line end position to filter completions
Completions were not being shown if a character in front of the cursor
was not in the completions, e.g. with loadfile '...'
2025-03-08 19:09:23 +00:00
Guido Cella 10bb083ab4 console.lua: don't retrieve --input-ime at startup
This is already retrieved when opening the console.
2025-03-08 19:09:23 +00:00
Guido Cella a839738d1c console.lua: reduce indentation in 2 functions
Nest the shorter blocks instead of the longer ones.
2025-03-08 19:09:23 +00:00
Guido Cella b23bd34921 console.lua: inline log_add()
As it's now used in a single place.
2025-03-08 19:09:23 +00:00