diff --git a/custom-keybinds.md b/custom-keybinds.md index c2c5ffb..7094781 100644 --- a/custom-keybinds.md +++ b/custom-keybinds.md @@ -173,7 +173,8 @@ To avoid conflicts custom keybinds use the format: `file_browser/dynamic/custom/ Expressions are used to evaluate Lua code into a string that can be used for commands. These behave similarly to those used for [`profile-cond`](https://mpv.io/manual/master/#conditional-auto-profiles) values. In an expression the `mp`, `mp.msg`, and `mp.utils` modules are available as `mp`, `msg`, and `utils` respectively. -Additionally the file-browser [addon API](addons/addons.md#the-api) is available as `fb`. +Additionally the file-browser [addon API](addons/addons.md#the-api) is available as `fb` and if [mpv-user-input](https://github.com/CogentRedTester/mpv-user-input) +is installed then user-input API will be available in `input`. This example only runs the keybind if the browser is in the Windows C drive or if the selected item is a matroska file: @@ -284,6 +285,41 @@ This example replaces all `/` characters in the path with `\` } ``` +### `run-statement ` + +Runs the following string a as a Lua statement. This is similar to an [expression](#expressions), +but instead of the code evaluating to a value it must run a series of statements. Basically it allows +for function bodies to be embedded into custom keybinds. All the same modules are available. +If multiple strings are sent to the script-message then they will be concatenated together with newlines. + +The following keybind will use [mpv-user-input](https://github.com/CogentRedTester/mpv-user-input) to +rename items in file-browser: + +```json +{ + "key": "KP1", + "command": ["script-message", "run-statement", + "assert(input, 'install mpv-user-input!')", + + "local line, err = input.get_user_input_co({", + "id = 'rename-file',", + "source = 'custom-keybind',", + "request_text = 'rename file:',", + "queueable = true,", + "default_input = %N,", + "cursor_pos = #(%N) - #fb.get_extension(%N, '')", + "})", + + "if not line then return end", + "os.rename(%F, utils.join_path(%P, line))", + + "fb.rescan()" + ], + "parser": "file", + "multiselect": true +} +``` + ## Examples See [here](file-browser-keybinds.json). diff --git a/file-browser.lua b/file-browser.lua index de1f7ff..3d2948a 100644 --- a/file-browser.lua +++ b/file-browser.lua @@ -126,6 +126,10 @@ local o = { opt.read_options(o, 'file_browser') utils.shared_script_property_set("file_browser-open", "no") +package.path = mp.command_native({"expand-path", o.module_directory}).."/?.lua;"..package.path +local success, input = pcall(require, "user-input-module") +if not success then input = nil end + -------------------------------------------------------------------------------------------------------- @@ -161,8 +165,6 @@ local parse_state_API = {} local ass = mp.create_osd_overlay("ass-events") if not ass then return msg.error("Script requires minimum mpv version 0.31") end -package.path = mp.command_native({"expand-path", o.module_directory}).."/?.lua;"..package.path - local style = { global = o.alignment == 0 and "" or ([[{\an%d}]]):format(o.alignment), @@ -579,6 +581,7 @@ function API.evaluate_string(str, name) env.msg = API.redirect_table(msg) env.utils = API.redirect_table(utils) env.fb = API.redirect_table(API) + env.input = input and API.redirect_table(input) local chunk, err if setfenv then @@ -2167,12 +2170,11 @@ local function scan_directory_json(directory, response_str) mp.commandv("script-message", response_str, list or "", opts or "") end -pcall(function() - local input = require "user-input-module" +if input then mp.add_key_binding("Alt+o", "browse-directory/get-user-input", function() input.get_user_input(browse_directory, {request_text = "open directory:"}) end) -end) +end @@ -2228,6 +2230,14 @@ mp.register_script_message('evaluate-expressions', function(...) end) end) +--a helper function for custom-keybinds +--concatenates the command arguments with newlines and runs the +--string as a statement of code +mp.register_script_message('run-statement', function(...) + local statement = table.concat(table.pack(...), '\n') + API.coroutine.run(API.evaluate_string, statement) +end) + --allows keybinds/other scripts to auto-open specific directories mp.register_script_message('browse-directory', browse_directory)