CogentRedTester / mpv-user-input

API to allow mpv scripts to request user text input

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Would this be possible?

Kayizoku opened this issue · comments

commented

This is exactly what I need for a script I have been looking for forever. I simply want to be able to rename my current playing file using a hotkey. Afterward, an input field should pop up, and once entered in the new name it should refresh MPV and catch the current filename. Would that be possible?

Theoretically yes. This API would work very well for that.

Are you asking me to write it for you?

commented

Are you asking me to write it for you?

If it's not too long, I would appreciate it.
If it's a bigger project then I am imagining.
I would appreciate few pointers and where to start ^^

local mp = require "mp"
local msg = require "mp.msg"
local utils = require "mp.utils"

package.path = mp.command_native({"expand-path", "~~/script-modules/?.lua;"})..package.path
local input = require "user-input-module"

local function rename(text, error)
    if not text then return msg.warn(error) end

    local filepath = mp.get_property("path")
    if filepath == nil then return end

    local directory, filename = utils.split_path(filepath)
    local name, extension = filename:match("^(.*)%.([^%./]+)$")
    local newfilepath = directory..text..'.'..extension

    msg.info( string.format("renaming '%s.%s' to '%s.%s'", name, extension, text, extension) )
    local success, error = os.rename(filepath, newfilepath)
    if not success then msg.error(error) end

    -- adding the new path to the playlist, and restarting the file with the correct path
    mp.commandv("loadfile", newfilepath, "append")
    mp.commandv("playlist-move", mp.get_property_number("playlist-count", 2) - 1, mp.get_property_number("playlist-pos", 1) + 1)
    mp.commandv("playlist-remove", "current")
end

--if the file closes while renaming then cancel the operation
--this removes any ambiguity over which file will be renamed
mp.register_event("end-file", function()
    input.cancel_user_input()
end)

mp.add_key_binding("f2", "rename-file", function()
    input.cancel_user_input()
    input.get_user_input(rename, { text = "Enter new name of file:" })
end)

I can't guarantee this is free of bugs and that it'll work in all situations. But it should provide a good base if you want to improve it.

commented

I can't guarantee this is free of bugs and that it'll work in all situations. But it should provide a good base if you want to improve it.

Thank you so much for this, I got an idea of how to do it now. Do you have any tips for me on where I should check for creating Lua scripts and an efficient way on how to test them along the way? I am also impressed you were able to write so much in a short time. How did you know what to use? I would appreciate some pointers. As for the script itself, I am unable to get it to work but I will try my hands and see if I can get it to function as I wanted.

The script is definitely working on my side, what is not working for you?

commented

The script is definitely working on my side, what is not working for you?

Oh, wow. The script does actually function quite well. It seems to just conflict with the OSC or some script I am using so I had to disable them all. So just have to figure out which one specifically. I still got ideas so going to try and implement them. Would appreciate answers to my previous comment. And once again, thank you so much for this. It will literally save me so much time

Do you have any tips for me on where I should check for creating Lua scripts and an efficient way on how to test them along the way?

My answer to this depends on how much coding knowledge you have. I learnt Lua by reading through some existing mpv scripts in order to make my own. Lua is very readable, and any methods or structures that are unfamiliar to you can be easily found in the Lua reference manual. However, I already knew how to code in other languages, so I was already familiar with most of the concepts and constructs. Programming in Lua might be a good place to start, this tutorial site also has a large section for Lua.

If you're making mpv scripts then you will want to read the mpv reference manual, specifically the sections on Lua Scripting, which details the mpv Lua API, and the command-interface.

As for testing, print statements are often the best way to allow you to check what values are being passed around your script. Start mpv from the console, instead of using the application shortcut, that way you can monitor console output. The inbuilt console script will also allow you to send commands at runtime, which can potentially make testing easier.

I am also impressed you were able to write so much in a short time. How did you know what to use? I would appreciate some pointers.

Practice, that's the main reason. I have been writing mpv scripts for several years, so I have a lot of experience. I have read the entire Lua and mpv manuals more than once, and I can remember almost every API function that is available. Once you understand all of the building blocks you can then practice putting them together into a full solution. I came up with the basic outline of that script in less than 5 minutes, because I knew that these API functions could easily be combined to perform this behaviour.

I'm closing the issue, but feel free to ask for any more advice on modifying that script.

commented

I will take you up on that kind offer as I got a question right now. How do you interact with the input field?

image

You type with the keyboard?

commented

You type with the keyboard?

haha, I should've made myself more clearer, my bad. Say I wanted the current filename to appear there the second I press F2, so I can edit the same file without having to write everything and then make small adjustments if there is no need for bigger changes in the first place. I already have an idea of how to make it work, but I am unable to interact with the input field through code.

You add a new field to the options table that you pass to input.get_user_input. You can see the options in the documentation here. The default_input field should be what you're looking for.

commented

You add a new field to the options table that you pass to input.get_user_input. You can see the options in the documentation here. The default_input field should be what you're looking for.

I knew it, that's what I am trying to do but without result. I don't know how to attach the property even with the example. I would appreciate an example. This is how I got my input method atm: input.get_user_input(rename,{ text = "Enter new filename for file: "..filename, "default_input" })

You add a new field to the options table that you pass to input.get_user_input. You can see the options in the documentation here. The default_input field should be what you're looking for.

I knew it, that's what I am trying to do but without result. I don't know how to attach the property even with the example. I would appreciate an example. This is how I got my input method atm: input.get_user_input(rename,{ text = "Enter new filename for file: "..filename, "default_input" })

 input.get_user_input(rename,{ text = "Enter new filename for file: "..filename, default_input = "default input" })

Or to make it more readable:

    input.get_user_input(rename, {
        text = "Enter new filename for file: "..filename,
        default_input = "default input"
    })
commented

Thank you once again. I cannot believe it was that simple. It's working exactly as i wanted