Skip to content

Instantly share code, notes, and snippets.

@hkdsun
Created June 1, 2020 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdsun/89c9a7c181e2f5f40b22325c20c72dfb to your computer and use it in GitHub Desktop.
Save hkdsun/89c9a7c181e2f5f40b22325c20c72dfb to your computer and use it in GitHub Desktop.
-- paths.lua
local hardlink_cmd = "cp -val"
local mv_cmd = "mv"
local function split(source, sep)
local result, i = {}, 1
while true do
local a, b = source:find(sep)
if not a then break end
local candidat = source:sub(1, a - 1)
if candidat ~= "" then
result[i] = candidat
end i=i+1
source = source:sub(b + 1)
end
if source ~= "" then
result[i] = source
end
return result
end
local function join(tble, join_str, first_idx, last_idx)
local res = ""
for i=first_idx,last_idx do
res = res .. tostring(tble[i])
if i ~= last_idx then
res = res .. join_str
end
end
return res
end
function cur_track_first_parent()
local res = split(cur_track_path(), "/")
return join(res, "/", 1, #res-1)
end
function cur_track_second_parent()
local res = split(cur_track_path(), "/")
return join(res, "/", 1, #res-2)
end
function cur_track_second_dir()
local dir = cur_track_path()
first_slash = string.find(dir, "/")
second_slash = string.find(dir, "/", first_slash+1)
dir = string.sub(dir,0,second_slash)
return dir
end
function cur_track_first_dir()
local dir = cur_track_path()
first_slash = string.find(dir, "/")
dir = string.sub(dir,0,first_slash)
return dir
end
function cur_track_path()
local playpath = mp.get_property("path")
local firstchar = string.sub(playpath, 1, 1)
if firstchar == "/" then -- convert to relative path
local work_dir = mp.get_property("working-directory")
playpath = string.sub(playpath, #work_dir+2, #playpath)
end
return playpath
end
function run_cmd(cmd, origin, dest)
local success = os.execute(cmd .. " \"" .. origin .. "\" \"" .. dest .. "\"")
if success then
mp.osd_message(cmd .. " successful: " .. origin .. " to: " .. dest)
else
mp.osd_message("Failed to " .. cmd)
end
print("'" .. origin .. "' " .. cmd .. "ed")
return success
end
-- crate.lua
local settings = {
ln_cmd = "cp -val",
mv_cmd = "mv",
start_from_percent = 30,
trash_dir = '/home/anon/Music/trash',
crate_dir = '/home/anon/Music/crate',
}
local menu_items = {}
local active = false
local show_osd = true
local function crate(path)
return function()
run_cmd(settings.ln_cmd, path, settings.crate_dir)
end
end
local function trash(path)
return function()
run_cmd(settings.mv_cmd, path, settings.trash_dir)
mp.command("playlist-next")
end
end
local function menu_remove_keybinds()
for i = 1, #menu_items do
key = menu_items[i][2]
mp.remove_key_binding(key)
end
end
local function menu_add_keybinds()
for i = 1, #menu_items do
menu_item = menu_items[i][1]
key = menu_items[i][2]
fn = menu_items[i][3]
mp.add_forced_key_binding(key, fn)
end
end
local function update_menu()
local l0 = cur_track_path()
local l1 = cur_track_first_parent()
local l2 = cur_track_second_parent()
menu_items = {
{ 'Trash l0: ' .. l0 , 'x' , trash(l0) } ,
{ 'Crate l0: ' .. l0 , 'c' , crate(l0) } ,
{ 'Trash l1: ' .. l1 , 's' , trash(l1) } ,
{ 'Crate l1: ' .. l1 , 'd' , crate(l1) } ,
{ 'Trash l2: ' .. l2 , 'w' , trash(l2) } ,
{ 'Crate l2: ' .. l2 , 'e' , crate(l2) } ,
}
menu_remove_keybinds()
menu_add_keybinds()
msg = ""
for i = 1, #menu_items do
category = menu_items[i][1]
key = menu_items[i][2]
msg = msg .. "[" .. key .. "]. " .. category .. "\n"
end
print(msg)
end
local function activate()
if active then
menu_remove_keybinds()
mp.osd_message("Disabled crater", 1)
else
update_menu()
mp.osd_message("Enabled crater", 1)
end
active = not active
end
local function on_file_loaded()
if active then
update_menu()
mp.set_property_number("percent-pos", settings.start_from_percent)
end
end
mp.add_key_binding("`", activate)
mp.register_event("file-loaded", on_file_loaded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment