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

scripting: add mp.input.select() #13982

Merged
merged 1 commit into from
May 7, 2024

Conversation

guidocella
Copy link
Contributor

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.

This is simple by reusing console.lua's code and even works in the terminal. I'm not sure if we should go with this or implement a more graphical menu. Example usage:

local input = require 'mp.input'
local default_item = 1

mp.add_key_binding('F1', function ()
    local playlist = {}
    for i, entry in ipairs(mp.get_property_native('playlist')) do
        local _, filename = require 'mp.utils'.split_path(entry.filename)
        playlist[i] = filename

        if entry.playing then
            default_item = i
        end
    end

    input.select({
        items = playlist,
        default_item = default_item,
        submit = function (id)
            mp.commandv('playlist-play-index', id - 1)
            input.terminate()
        end,
    })
end)

@na-na-hi
Copy link
Contributor

na-na-hi commented Apr 24, 2024

I personally expect 500+ lines of vendored 3rd-party dependency to do more than just this one thing. For example, --audio-file-auto, --sub-auto, and --cover-art-auto can benefit from the added fuzzy match functions. Since the included files are not intended to be used as a library, some works need to be done, which means we are now responsible to maintain the added code.

@guidocella
Copy link
Contributor Author

guidocella commented Apr 24, 2024

I'm not sure fuzzy matching would help for --sub-auto since that's based on the media filename and not on text typed the user. That repository also has a pure lua version of fzy at https://github.com/swarn/fzy-lua/blob/main/src/fzy_lua.lua whose performance is fine so that can be used instead if preferred.

@na-na-hi
Copy link
Contributor

That repository also has a pure lua version of fzy at https://github.com/swarn/fzy-lua/blob/main/src/fzy_lua.lua whose performance is fine so that can be used instead if preferred.

If the performance is fine then I think it's better than the C version for this purpose. It's in a managed language and a lot less code to deal with.

Copy link

github-actions bot commented Apr 24, 2024

Download the artifacts for this pull request:

Windows
macOS

@guidocella guidocella force-pushed the console-select branch 4 times, most recently from ecc1cfe to 47a40fa Compare April 26, 2024 17:44
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 mpv-player#13964.
Copy link
Contributor

@kasper93 kasper93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works. Not much to say, nice addition.

@kasper93 kasper93 added this to the Release v0.39.0 milestone May 7, 2024
@kasper93 kasper93 merged commit 2354d87 into mpv-player:master May 7, 2024
16 checks passed
@guidocella guidocella deleted the console-select branch May 7, 2024 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support built-in command palette script/API (a searchable menu)
3 participants