Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select.lua: add this script #14087

Merged
merged 7 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ local mp_globals = {
set_mouse_area = {},
set_osd_ass = {},
}
}
},
unpack = {},
}

local mp_internal = {
Expand Down
6 changes: 3 additions & 3 deletions DOCS/man/lua.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -984,7 +985,6 @@ REPL.
},
submit = function (id)
mp.commandv("playlist-play-index", id - 1)
input.terminate()
end,
})

Expand Down
40 changes: 40 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,46 @@ 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-s
Select a subtitle track.

g-S
Select a secondary subtitle track.

g-a
Select an audio track.

g-v
Select a video track.

g-t
Select a track of any type.

g-c
Select a chapter.

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

g-d
Select an audio device.

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
12 changes: 12 additions & 0 deletions etc/input.conf
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@
#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-s script-binding select/select-sid
#g-S script-binding select/select-secondary-sid
#g-a script-binding select/select-aid
#g-v script-binding select/select-vid
#g-t script-binding select/select-track
#g-c script-binding select/select-chapter
#g-l script-binding select/select-subtitle-line
#g-d script-binding select/select-audio-device
#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
4 changes: 1 addition & 3 deletions input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ static struct cmd_bind *find_bind_for_key_section(struct input_ctx *ictx,
for (int builtin = 0; builtin < 2; builtin++) {
guidocella marked this conversation as resolved.
Show resolved Hide resolved
if (builtin && !ictx->opts->default_bindings)
break;
if (best)
break;
for (int n = 0; n < bs->num_binds; n++) {
if (bs->binds[n].is_builtin == (bool)builtin) {
struct cmd_bind *b = &bs->binds[n];
Expand All @@ -418,7 +416,7 @@ static struct cmd_bind *find_bind_for_key_section(struct input_ctx *ictx,
if (b->keys[i] != keys[b->num_keys - 1 - i])
goto skip;
}
if (!best || b->num_keys >= best->num_keys)
if (!best || b->num_keys > best->num_keys)
best = b;
skip: ;
}
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
5 changes: 3 additions & 2 deletions player/javascript/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,10 @@ mp.options = { read_options: read_options };
* input
*********************************************************************/
function register_event_handler(t) {
mp.register_script_message("input-event", function (type, text, cursor_position) {
mp.register_script_message("input-event", function (type, args) {
if (t[type]) {
var result = t[type](text, cursor_position);
args = JSON.parse(args)
var result = t[type](args[0], args[1]);

if (type == "complete" && result) {
mp.commandv("script-message-to", "console", "complete",
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
53 changes: 40 additions & 13 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ local function fuzzy_find(needle, haystacks)
return result
end

local function populate_log_with_matches()
if not selectable_items then
local function populate_log_with_matches(max_width)
if not selectable_items or selected_match == 0 then
return
end

Expand Down Expand Up @@ -393,7 +393,7 @@ local function populate_log_with_matches()

for i = first_match_to_print, last_match_to_print do
log[#log + 1] = {
text = matches[i].text .. '\n',
text = truncate_utf8(matches[i].text, max_width) .. '\n',
style = i == selected_match and styles.selected_suggestion or '',
terminal_style = i == selected_match and terminal_styles.selected_suggestion or '',
}
Expand All @@ -415,7 +415,7 @@ local function print_to_terminal()
return
end

populate_log_with_matches()
populate_log_with_matches(mp.get_property_native('term-size/w', 80))

local log = ''
for _, log_line in ipairs(log_buffers[id]) do
Expand Down Expand Up @@ -507,7 +507,7 @@ function update()
local suggestions, rows = format_table(suggestion_buffer, width_max, lines_max)
local suggestion_ass = style .. styles.suggestion .. suggestions

populate_log_with_matches()
populate_log_with_matches(width_max)

local log_ass = ''
local log_buffer = log_buffers[id]
Expand Down Expand Up @@ -567,7 +567,7 @@ function set_active(active)

if input_caller then
mp.commandv('script-message-to', input_caller, 'input-event',
'closed', line, cursor)
'closed', utils.format_json({line, cursor}))
input_caller = nil
line = ''
cursor = 1
Expand Down Expand Up @@ -634,6 +634,19 @@ function len_utf8(str)
return len
end

function truncate_utf8(str, max_length)
local len = 0
local pos = 1
while pos <= #str do
pos = next_utf8(str, pos)
len = len + 1
if len == max_length - 1 then
return str:sub(1, pos - 1) .. '⋯'
end
end
return str
end

local function handle_edit()
if selectable_items then
matches = {}
Expand All @@ -649,7 +662,7 @@ local function handle_edit()

if input_caller then
mp.commandv('script-message-to', input_caller, 'input-event', 'edited',
line)
utils.format_json({line}))
end
end

Expand Down Expand Up @@ -786,9 +799,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',
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
Expand Down Expand Up @@ -878,11 +897,19 @@ function handle_pgdown()
end

local function page_up_or_prev_char()
return selectable_items and handle_pgup() or prev_char()
if selectable_items then
handle_pgup()
else
prev_char()
end
end

local function page_down_or_next_char()
return selectable_items and handle_pgdown() or next_char()
if selectable_items then
handle_pgdown()
else
next_char()
end
end

-- Move to the start of the current word, or if already at the start, the start
Expand Down Expand Up @@ -1202,7 +1229,7 @@ function complete(backwards)
completion_old_line = line
completion_old_cursor = cursor
mp.commandv('script-message-to', input_caller, 'input-event',
'complete', line:sub(1, cursor - 1))
'complete', utils.format_json({line:sub(1, cursor - 1)}))
return
end

Expand Down Expand Up @@ -1512,7 +1539,7 @@ mp.register_script_message('get-input', function (script_name, args)
args = utils.parse_json(args)
prompt = args.prompt or default_prompt
line = args.default_text or ''
cursor = tonumber(args.cursor_position) or line:len() + 1
cursor = args.cursor_position or line:len() + 1
id = args.id or script_name .. prompt
if histories[id] == nil then
histories[id] = {}
Expand Down
5 changes: 3 additions & 2 deletions player/lua/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ local utils = require "mp.utils"
local input = {}

local function register_event_handler(t)
mp.register_script_message("input-event", function (type, text, cursor_position)
mp.register_script_message("input-event", function (type, args)
if t[type] then
local suggestions, completion_start_position = t[type](text, cursor_position)
local suggestions, completion_start_position =
t[type](unpack(utils.parse_json(args or "") or {}))

if type == "complete" and suggestions then
mp.commandv("script-message-to", "console", "complete",
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