WasteBasket.lua: Code reorg.

This commit is contained in:
Ole Tange 2017-02-12 01:26:14 +01:00
parent 5853ad5398
commit 3c5accdf5e

View file

@ -23,7 +23,7 @@ function descriptor()
.. "<br>This will NOT change your playlist, it will move the file itself. " .. "<br>This will NOT change your playlist, it will move the file itself. "
.. "<br>Wastebasket will search for a dir called .waste " .. "<br>Wastebasket will search for a dir called .waste "
.. "in the dir of the file and all parent dirs of that."; .. "in the dir of the file and all parent dirs of that.";
url = "https://gitlab.com/ole.tange/tangetools/wastebasket" url = "https://gitlab.com/ole.tange/tangetools/tree/master/wastebasket"
} }
end end
@ -31,17 +31,7 @@ end
-- Activation hook -- Activation hook
function activate() function activate()
-- get the current playing file local filename,dst,wdir = filename_dst_wastedir()
item = vlc.input.item()
-- extract its URI
uri = item:uri()
-- decode %foo stuff from the URI
filename = vlc.strings.decode_uri(uri)
-- remove 'file://' prefix which is 7 chars long
filename = string.sub(filename,8)
-- find .waste in parent dirs
wdir = wastedir(dirname(filename))
if(directory_exists(wdir)) then if(directory_exists(wdir)) then
d = vlc.dialog("Wastebasket") d = vlc.dialog("Wastebasket")
d:add_label("Move <b>".. filename .. "</b> to <b>" .. wdir .. "</b>?") d:add_label("Move <b>".. filename .. "</b> to <b>" .. wdir .. "</b>?")
@ -57,17 +47,32 @@ function activate()
vlc.msg.dbg("[Wastebasket] Activated") vlc.msg.dbg("[Wastebasket] Activated")
end end
function filename_dst_wastedir()
-- get the current playing file
local item = vlc.input.item()
-- extract its URI
local uri = item:uri()
-- decode %foo stuff from the URI
local filename = vlc.strings.decode_uri(uri)
-- remove 'file://' prefix which is 7 chars long
filename = string.sub(filename,8)
-- find .waste in parent dirs
local wdir = wastedir(dirname(filename))
return filename,wdir .. "/" .. basename(filename),wdir
end
function wastedir(dir) function wastedir(dir)
-- recursively search for .waste in parent dir -- recursively search for .waste in parent dir
vlc.msg.dbg("[Wastebasket/wastedir] Looking at " .. dir) vlc.msg.dbg("[Wastebasket/wastedir] Looking at " .. dir)
wdir = dir .. "/" .. ".waste" local wdir = dir .. "/" .. ".waste"
if directory_exists(wdir) then if directory_exists(wdir) then
vlc.msg.dbg("[Wastebasket/wastedir] Found wastedir: " .. wdir) vlc.msg.dbg("[Wastebasket/wastedir] Found wastedir: " .. wdir)
return wdir return wdir
end end
-- try the parent dir -- try the parent dir
parent = dirname(dir) local parent = dirname(dir)
vlc.msg.dbg("[Wastebasket/wastedir] parent " .. parent) vlc.msg.dbg("[Wastebasket/wastedir] parent " .. parent)
if directory_exists(parent) then if directory_exists(parent) then
return wastedir(parent) return wastedir(parent)
@ -104,24 +109,12 @@ function dirname(str)
end end
function delete() function delete()
-- get the current playing file local filename,dst,wdir = filename_dst_wastedir()
item = vlc.input.item()
-- extract its URI
uri = item:uri()
-- decode %foo stuff from the URI
filename = vlc.strings.decode_uri(uri)
-- remove 'file://' prefix which is 7 chars long
filename = string.sub(filename,8)
-- find .waste in parent dirs
wdir = wastedir(dirname(filename))
if(directory_exists(wdir)) then if(directory_exists(wdir)) then
basena = basename(filename)
dst = wdir .. "/" .. basena
vlc.msg.dbg("[Wastebasket]: Move to " .. dst) vlc.msg.dbg("[Wastebasket]: Move to " .. dst)
retval, err = os.rename(filename,dst) local retval, err = os.rename(filename,dst)
if(retval == nil) then if(retval == nil) then
-- error handling; if deletion failed, print why -- error handling; if moving failed, print why
vlc.msg.dbg("[Wastebasket] error: " .. err) vlc.msg.dbg("[Wastebasket] error: " .. err)
end end
else else