kdialog-open-files.lua和zenity-open-files.lua是分别使用KDE KDialog和Gnome's zenity在mpv中打开文件的两个项目。我希望使用fdfind和fzf来做同样的事情。
如这个堆叠溢出的答案中所示,以下工作将是我们的出发点:
fdfind . /path/to/Music | fzf -m | xargs -d "\n" mpv我们可以使用上面的命令选择一些文件并使用mpv播放它们。但是,我们希望为我们的文件选择创建一个对话框。为此,我们创建一个具有以下内容的bash脚本menu_fzf.sh:
#!/bin/bash
urxvt -name fzf_menu -e bash -c "fzf -m $* < /proc/$$/fd/0 > /proc/$$/fd/1"(urxvt只是一个例子,可以在上面使用任何其他合适的终端仿真器)。
现在如果我们跑:
fdfind . /path/to/Music | menu_fzf.sh然后打开一个新的终端,它允许您使用/path/to/Music从fzf中选择多个文件,并将选择作为输出返回到调用终端。因此,现在我们可以运行以下命令
fdfind . /path/to/Music | menu_fzf.sh | xargs -d "\n" mpv它将在mpv中播放选定的文件。到目前一切尚好!
现在棘手的部分是如何从lua内部使用mpv脚本来完成上述所有工作。通过查看kdialog-open-files.lua或zenity-open-files.lua,我似乎需要这样写:
utils = require 'mp.utils'
-- TODO: make the following work so that file_select correctly stores
-- the files selected using menu_fzf.sh:
file_select = utils.subprocess({
args = {'command', 'argument1', 'argument2', 'and so on'},
cancellable = false,
})
-- and then the rest of the code from kdialog-open-files.lua然而,我发现utils.subprocess是简洁和不足的。我创造性地尝试在上面选择command、argument1、argument2、and so on,但是似乎没有任何效果!
所以我有两个问题:
utils.subprocess的信息吗?有更详细的手册吗?源代码在哪里?备注:因为我以前从来没有用lua编程过,所以我一直在为解决上面的问题而挣扎,即使我有utils.subprocess的源代码,也很可能会继续挣扎。所以真正帮助解决问题-1是更优先考虑的问题。但是对问题2的任何帮助也是很棒的!
我的尝试到目前为止都失败了,
下面将将当前目录中的所有文件附加到播放列表中:
file_select = utils.subprocess({
args = {'fdfind', '.', directory},
cancellable = false,
})下列情况失败:
file_select = utils.subprocess({
args = {'fdfind', '.', directory, '|', 'fzf'},
cancellable = false,
})带有错误信息:
[open_files] [fd error]: Search path '|' is not a directory.
[open_files] [fd error]: Search path 'fzf' is not a directory.以下允许从$HOME目录中选择多个文件,但(如预期的)不返回所选内容:
file_select = utils.subprocess({
args = {'urxvt','-e','fzf', '-m'},
cancellable = false,
})以下内容不起作用:
file_select = utils.subprocess({
args = {'urxvt','-e','fzf', '-m', '>', '/proc/$$/fd/1'},
cancellable = false,
})下面使用fzf打开一个新对话框,但没有什么可供选择的:
file_select = utils.subprocess({
args = {'menu_fzf.sh'},
cancellable = false,
})发布于 2022-08-30 16:13:35
创建具有以下内容的bash脚本mpv_fzf_menu.sh:
#!/bin/bash
urxvt -name fzf_menu -e bash -c "fdfind . "$1" | fzf -m > /proc/$$/fd/1"创建具有以下内容的lua文件open-files.lua:
utils = require 'mp.utils'
function select_files_dmenu_fzf()
if mp.get_property("path") == nill then
directory = ""
else
directory = utils.split_path(utils.join_path(mp.get_property("working-directory"), mp.get_property("path")))
end
file_select = utils.subprocess({
args = {'mpv_fzf_menu.sh', directory},
cancellable = false,
})
end
function add_files()
select_files_dmenu_fzf()
if (file_select.status ~= 0) then return end
local first_file = true
for filename in string.gmatch(file_select.stdout, '[^\n]+') do
mp.commandv('loadfile', filename, first_file and 'replace' or 'append')
first_file = false
end
end
mp.add_key_binding("Ctrl+f", "add-files-kdialog", add_files)(我假设您知道下一步要做什么:使bash脚本可执行,并将其放在bash在其$PATH中找到它的某个地方。将lua脚本放入.config/mpv/scripts中,一切都会正常工作,即当您按下mpv中的Ctrl+f时,应该打开一个对话框,让您选择文件)。
备注:kdialog-open-files.lua的所有功能都没有实现。但是上面的lua脚本可以很容易地扩展。您还可以通过使用fdfind对具有指定扩展名的文件进行过滤,从而使事情变得更加复杂。您还可以使用独立于bash脚本的lua脚本。如果您配置了--preview选项的fzf,那么您可以在进行选择之前获得文件预览。
https://stackoverflow.com/questions/73542777
复制相似问题