Commit Graph
251 Commits
Author SHA1 Message Date
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łowandGuido Cella 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
Guido Cella 686eb160e4 console.lua: clear the default item on input.get()
Clear the default item of the last input.select() call to prevent
command history search from highlighting that same default item.
2025-03-08 19:09:23 +00:00
Guido Cella a6024b562a console.lua: declare various variables only in free-form text mode
These are no longer used anywhere in select mode, except for 1 instance
of log_buffers[id].
2025-03-08 19:09:23 +00:00
Guido Cella 6f2ce8aac1 console.lua: don't duplicate the check to add to history 2025-03-08 19:09:23 +00:00
Guido Cella 0b3cc3a167 commands.lua: split this script out of console.lua
It was suggested by CogentRedTester in
https://github.com/mpv-player/mpv/pull/10282#issuecomment-1858727729 and
https://github.com/mpv-player/mpv/pull/10282#issuecomment-1858809580 and
by avih that making running commands an mp.input client is a better
architecture.

A practical advantage is that completions are calculated in a different
thread, which prevents hanging when completing slow/network filesystems.

script-binding console/enable becomes script-binding commands/open,
though the console one is kept as an alias. I took the opportunity to
rename this because open makes more sense for a graphical modal, and it
is the word used in mp.input and user-data.

script-message-to console type becomes script-message-to commands type,
though the console one is kept as an alias. It is also changed to
automatically close on submit without having to append '; keypress ESC'
as you don't need to keep the console open after running prefilled
commands.

Also convert to double quotes like other scripts and rename some
inconsistent functions.
2025-03-08 19:09:23 +00:00
Guido Cella ce6a52d40a console.lua: add history_path field to mp.input.get
This allows saving the history to all mp.input.get clients.

Needed to split running commands out of console.lua.
2025-03-08 19:09:23 +00:00
Guido Cella 05438fc081 console.lua: add autoselect_completion field to mp.input.get
This selects the first completion when pressing Enter when one wasn't
already manually selected.

Needed to split running commands out of console.lua.
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 6a1cbad0c7 console.lua: fix select memory leak
The overlay to calculate the max item width was never removed, so a new
one was added on each select call, adding 5-10 MBs of memory usage on
each menu opened.

Fix this by reusing the same overlay, which is also faster.
2025-03-01 08:01:20 +01:00
Guido Cella 80c9d7d8df console.lua: include more characters in selectable items
300 characters doesn't always fill the window.

Fixes #15944.
2025-02-25 01:29:26 +01:00
Guido Cella 996c098725 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.
2025-02-24 16:26:58 +01:00
Guido Cella 2d15431d11 console.lua: rename handle_enter() to submit()
Because it's also called on left click.
2025-02-24 16:12:53 +01:00
Guido Cella 9551de6385 console.lua: replace repl references
These are leftovers from the original repl.lua name. Also remove some
redundant comments.
2025-02-24 16:12:53 +01:00
Guido Cella 8e72ded1bb console.lua: increase the opacity of the default item background
This needs to strike a fine balance between being visible and having
contrast with the text color and not taking attention away from the
selected item. Make it slightly more opaque to differentiate it more
easily.

Fixes #15711.
2025-02-24 16:12:44 +01:00
Guido Cella f561efec40 console.lua: cache the command-list 2025-02-24 15:57:07 +01:00
Guido Cella d4dc19a6b4 console.lua: complete multiple flags separated by + 2025-02-24 15:57:07 +01:00
Guido Cella 72bb227bf1 console.lua: highlight matching parts of selectable items
fzy returns which characters matched the search for every item, so use
this to highlight them.

This defaults to a light blue color that is readable with both black and
white (selected) backgrounds.

The returned positions are just bytes, so we can't put ASS tags or
escape sequences around each byte because that would break displaying
multibyte UTF-8 characters. Only put them around sequences of matched
positions, and only when the sequences start and end at actual character
positions.
2025-02-24 15:56:20 +01:00
Guido Cella a5700484be console.lua: separate the log and matching items
The next commit will inject ASS within matching items to highlight
matching positions, which means that the text key of log lines can no
longer be used, and that matching items and log lines diverged enough
that it's simpler to separate them.

As a bonus, we can stop clearing the log and completions when searching
the command history, and restore them after the search.
2025-02-24 15:56:20 +01:00
Guido Cella 64531af454 console.lua: allow persisting the command history 2025-02-24 15:53:22 +01:00
Guido Cella 4efcfb9540 console.lua: preserve font styles in the counter
Fixes https://github.com/Samillion/ModernZ/issues/359
2025-02-24 15:52:20 +01:00
Guido Cella 12ef7077be console.lua: preserve the counter style when searching history
When searching the command history with outline-and-shadow, the counter
should preserve the border, shadow and blur. Only with background-box it
should disable the shadow to not overlap 2 rectangles. While disabling
border and blur is needed with the menu background+outline-and-shadow.
2025-02-24 15:52:20 +01:00
Guido Cella 41f2bab8c9 console.lua: complete frame-step flags 2025-02-21 15:47:06 +01:00
Kacper Michajłow ba31aa72ba console.lua: use the same opacity for menu as for everything else
There was a mistake of using hex (50) instead of decimal (80) it is
converted to %x later on.
2025-02-17 22:54:04 +01:00
nanahi 052870be9c console.lua: enable/disable IME on demand
This makes console and mp.input users enable IME when text input is
active and restore IME to the previous state when text input is inactive,
to ensure the best IME mode is selected based on input context.
2025-02-17 16:56:47 +00:00
Guido Cella 1329b50098 console.lua: give a different background color to the selected item
The default item also has the same background color but with
transparency.

Also stop bolding selections since inverted black and white backgrounds
should be visible even with color blindness. It was annoying with
proportional fonts because it misalignes similar strings.

As mpv's default text colors are white on black border or background,
--osd-selected-color's default of a bright yellow meant to be used with
a black border becomes unreadable with the inverted white background.

We could default to a dark --osd-selected-color and a a light
--osd-selected-outline-color and use --osd-selected-outline-color as the
selected back color. However in show-text commands having only the
selected item with a different white border doesn't look good.

This therefore adds indipendent selected_color and selected_back_color
script-opts. --osd-selected-color is only used for completions and for
the selected item when searching the command history with
outline-and-shadow.
2025-02-17 16:35:27 +00:00
Guido Cella 5dec4a2464 Revert "console.lua: close with right click"
This reverts commit ad0c29e5ac.

This should be unnecessary now that the menu has a visible horizontal
and vertical hitbox, and was not discoverable anyway. Right click can be
useful even while the console is open to pause or open a context menu.
Requested in
https://github.com/mpv-player/mpv/pull/15145#issuecomment-2428032616.
2025-02-17 16:35:27 +00:00
Guido Cella 24cd11d85c console.lua: always draw a background behind items
Draw a background behind selectable items even with outline-and-shadow.
This makes sense for a menu because you want it to be readable, select
something and move on. It doesn't stay open while watching like OSD
messages and subtitles, so it can cover more of the video. In fact this
was probably the only menu without a background by default. Also the
scrollbar without a background looked weird, and the background shows
the new horizontal hit box.

--osd-outline-color determines the background color with
outline-and-shadow, while --osd-back-color determines the background
color with background-box. Some transparency is added because using pure
black is not recommended because it causes eye strain; alternatively
--osd-outline-color could default to #222222.

Drawing the background ourselves also allows making the corners rounded.

Free-form text mode keeps using only background-box backgrounds if
configured as covering the whole screen while searching stats key
bindings would be bad. Searching history also doesn't add a background
to not change the layout abruptly. When searching history with
background-box, it preserves the alpha component of --osd-back-color.
2025-02-17 16:35:27 +00:00
Guido Cella 7d1d1cfcba console.lua: add a scrollbar
Add a scrollbar to give a visual representation of the size and position
in the items. It is not clickable.

The counter is moved above the scrollbar to free up a line for an
another item, simplify calculations, and prevent the selected item from
jumping from the second to the first line as you type.

In terminal output there is no easy way to draw a scrollbar to the right
due to Unicode widths. This just moves the counter into the prompt.
2025-02-17 16:35:27 +00:00
Guido Cella 4169b1ad1e console.lua: close when clicking outside of items horizontally 2025-02-17 16:35:27 +00:00