Skip to content

Commit

Permalink
select.lua: add this script
Browse files Browse the repository at this point in the history
This adds script messages to select playlist entries, tracks, chapters,
subtitle lines, bindings and properties using the newly introduced
mp.input.select().

This fully closes #13964.
  • Loading branch information
guidocella committed May 10, 2024
1 parent 1f0de12 commit c06650b
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 7 deletions.
11 changes: 11 additions & 0 deletions DOCS/man/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,14 @@ Configurable Options
The ratio of font height to font width.
Adjusts table width of completion suggestions.
Values in the range 1.8..2.5 make sense for common monospace fonts.

``select_font_size``
Default: 26

The font size when using the console through ``mp.input.select()``, which by
default is used by keybindings starting with ``g``.

``select_border_size``
Default: 3

The border size when using the console through ``mp.input.select()``.
28 changes: 28 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ Alt+2 (and Command+2 on macOS)
Command + f (macOS only)
Toggle fullscreen (see also ``--fs``).

(The following keybindings open a selector in the console that lets you choose
from a list of items by typing part of the desired item and/or by navigating
them with keybindings: ``Down`` and ``Ctrl+n`` go down, ``Up`` and ``Ctrl+p`` go
up, ``Page down`` and ``Ctrl+f`` scroll down one page, and ``Page up`` and
``Ctrl+b`` scroll up one page.)

g-p
Select a playlist entry.

g-t
Select a track.

g-j
Select a secondary subtitle.

g-c
Select a chapter.

g-s
Select a subtitle line to seek to. This requires ``ffmpeg`` in PATH, or in
the same folder as mpv on Windows.

g-b
Select a defined input binding.

g-r
Show the values of all properties.

(The following keys are valid if you have a keyboard with multimedia keys.)

PAUSE
Expand Down
4 changes: 4 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ Program Behavior
`Conditional auto profiles`_ for details. ``auto`` will load the script,
but immediately unload it if there are no conditional profiles.

``--load-select=<yes|no>``
Enable the builtin script that lets you select from lists of items (default:
yes). By default, its keybindings start with the ``g`` key.

``--player-operation-mode=<cplayer|pseudo-gui>``
For enabling "pseudo GUI mode", which means that the defaults for some
options are changed. This option should not normally be used directly, but
Expand Down
8 changes: 8 additions & 0 deletions etc/input.conf
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@
#ctrl+h cycle-values hwdec "auto-safe" "no" # toggle hardware decoding
#F8 show-text ${playlist} # show the playlist
#F9 show-text ${track-list} # show the list of video, audio and sub tracks
#g ignore
#g-p script-binding select/select-playlist
#g-t script-binding select/select-track
#g-j script-binding select/select-secondary-sub
#g-c script-binding select/select-chapter
#g-s script-binding select/sub-seek
#g-b script-binding select/select-binding
#g-r script-binding select/show-properties

#
# Legacy bindings (may or may not be removed in the future)
Expand Down
2 changes: 2 additions & 0 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ static const m_option_t mp_opts[] = {
{"load-auto-profiles",
OPT_CHOICE(lua_load_auto_profiles, {"no", 0}, {"yes", 1}, {"auto", -1}),
.flags = UPDATE_BUILTIN_SCRIPTS},
{"load-select", OPT_BOOL(lua_load_select), .flags = UPDATE_BUILTIN_SCRIPTS},
#endif

// ------------------------- stream options --------------------
Expand Down Expand Up @@ -969,6 +970,7 @@ static const struct MPOpts mp_default_opts = {
.lua_load_stats = true,
.lua_load_console = true,
.lua_load_auto_profiles = -1,
.lua_load_select = true,
#endif
.auto_load_scripts = true,
.loop_times = 1,
Expand Down
1 change: 1 addition & 0 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ typedef struct MPOpts {
bool lua_load_stats;
bool lua_load_console;
int lua_load_auto_profiles;
bool lua_load_select;

bool auto_load_scripts;

Expand Down
2 changes: 1 addition & 1 deletion player/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ typedef struct MPContext {

struct mp_ipc_ctx *ipc_ctx;

int64_t builtin_script_ids[5];
int64_t builtin_script_ids[6];

mp_mutex abort_lock;

Expand Down
3 changes: 3 additions & 0 deletions player/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ static const char * const builtin_lua_scripts[][2] = {
},
{"@auto_profiles.lua",
# include "player/lua/auto_profiles.lua.inc"
},
{"@select.lua",
# include "player/lua/select.lua.inc"
},
{0}
};
Expand Down
15 changes: 10 additions & 5 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ local opts = {
-- Adjusts table width of completion suggestions.
-- Values in the range 1.8..2.5 make sense for common monospace fonts.
font_hw_ratio = 'auto',
select_font_size = 26,
select_border_size = 3,
}

function detect_platform()
Expand Down Expand Up @@ -252,7 +254,7 @@ local function calculate_max_log_lines()
/ mp.get_property_native('display-hidpi-scale', 1)
/ opts.scale
* (1 - global_margins.t - global_margins.b)
/ opts.font_size
/ (opts[selectable_items and 'select_font_size' or 'font_size'])
- 1.5)
end

Expand Down Expand Up @@ -467,6 +469,9 @@ function update()
return
end

local font_size = opts[selectable_items and 'select_font_size' or 'font_size']
local border_size = opts[selectable_items and 'select_border_size' or 'border_size']

local coordinate_top = math.floor(global_margins.t * screeny + 0.5)
local clipping_coordinates = '0,' .. coordinate_top .. ',' ..
screenx .. ',' .. screeny
Expand All @@ -475,15 +480,15 @@ function update()
local style = '{\\r' ..
'\\1a&H00&\\3a&H00&\\1c&Heeeeee&\\3c&H111111&' ..
(has_shadow and '\\4a&H99&\\4c&H000000&' or '') ..
'\\fn' .. opts.font .. '\\fs' .. opts.font_size ..
'\\bord' .. opts.border_size .. '\\xshad0\\yshad1\\fsp0\\q1' ..
'\\fn' .. opts.font .. '\\fs' .. font_size ..
'\\bord' .. border_size .. '\\xshad0\\yshad1\\fsp0\\q1' ..
'\\clip(' .. clipping_coordinates .. ')}'
-- Create the cursor glyph as an ASS drawing. ASS will draw the cursor
-- inline with the surrounding text, but it sets the advance to the width
-- of the drawing. So the cursor doesn't affect layout too much, make it as
-- thin as possible and make it appear to be 1px wide by giving it 0.5px
-- horizontal borders.
local cheight = opts.font_size * 8
local cheight = font_size * 8
local cglyph = '{\\rDefault' ..
(mp.get_property_native('focused') == false
and '\\alpha&HFF&' or '\\1a&H44&\\3a&H44&\\4a&H99&') ..
Expand All @@ -499,7 +504,7 @@ function update()

local lines_max = calculate_max_log_lines()
-- Estimate how many characters fit in one line
local width_max = math.ceil(screenx / opts.font_size * get_font_hw_ratio())
local width_max = math.ceil(screenx / font_size * get_font_hw_ratio())

local suggestions, rows = format_table(suggestion_buffer, width_max, lines_max)
local suggestion_ass = style .. styles.suggestion .. suggestions
Expand Down
2 changes: 1 addition & 1 deletion player/lua/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lua_files = ['defaults.lua', 'assdraw.lua', 'options.lua', 'osc.lua',
'ytdl_hook.lua', 'stats.lua', 'console.lua', 'auto_profiles.lua',
'input.lua', 'fzy.lua']
'input.lua', 'fzy.lua', 'select.lua']
foreach file: lua_files
lua_file = custom_target(file,
input: join_paths(source_root, 'player', 'lua', file),
Expand Down
Loading

0 comments on commit c06650b

Please sign in to comment.