- Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdelete_file.lua
More file actions
Latest commit
137 lines (123 loc) · 3.88 KB
/
Copy pathdelete_file.lua
File metadata and controls
137 lines (123 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
localutils=require"mp.utils"
localmsg=require"mp.msg"
require"mp.options"
options= {}
options.MoveToFolder=false
ifpackage.config:sub(1,1) =="/" then
options.DeletedFilesPath=utils.join_path(os.getenv("HOME"), "delete_file")
ops="unix"
else
options.DeletedFilesPath=utils.join_path(os.getenv("USERPROFILE"), "delete_file")
ops="win"
end
read_options(options)
del_list= {}
functioncreateDirectory()
ifnotutils.file_info(options.DeletedFilesPath) then
ifnotos.execute(string.format('mkdir "%s"', options.DeletedFilesPath)) then
msg.error("failed to create folder for moving deleted files")
end
end
end
functioncontains_item(l, i)
local_, file_name=utils.split_path(i)
fork, vinpairs(l) do
ifv==ithen
mp.osd_message("undeleting current file")
msg.info("undeleting file: ", file_name)
l[k] =nil
returntrue
end
end
mp.osd_message("deleting current file")
msg.info("deleting file: ", file_name)
returnfalse
end
functionmark_delete()
localwork_dir=mp.get_property_native("working-directory")
localfile_path=mp.get_property_native("path")
locals=file_path:find(work_dir, 0, true)
localfinal_path
ifsands==0then
final_path=file_path
else
final_path=utils.join_path(work_dir, file_path)
end
msg.debug("final_path: ", final_path)
ifnotcontains_item(del_list, final_path) then
table.insert(del_list, final_path)
end
end
functiondelete()
ifoptions.MoveToFolderthen
--create folder if not exists
createDirectory()
end
fori, vinpairs(del_list) do
ifoptions.MoveToFolderthen
msg.info("moving: ", v)
local_, file_name=utils.split_path(v)
--this loop will add a number to the file name if it already exists in the directory
--But limit the number of iterations
fori=1,100do
ifi>1then
iffile_name:find("[.].+$") then
file_name=file_name:gsub("([.].+)$", string.format("_%d%%1", i))
else
file_name=string.format("%s_%d", file_name, i)
end
end
localmovedPath=utils.join_path(options.DeletedFilesPath, file_name)
localfileInfo=utils.file_info(movedPath)
ifnotfileInfothen
localok, err, code=os.rename(v, movedPath)
ifnotokthen
msg.error("could not move file: ", err, code)
end
break
else
msg.warn("File ("..file_name..") already exists")
end
end
else
msg.info("deleting: ", v)
localok, err, code=os.remove(v)
ifnotokthen
msg.error("failed deleting file: ", err, code)
end
end
end
end
functionshowList()
localdelString="Delete Marks:\n"
for_,vinpairs(del_list) do
localdFile=v:gsub("/","\\")
delString=delString..dFile:match("\\*([^\\]*)$").."; "
end
ifdelString:find(";") then
mp.osd_message(delString)
returndelString
elseifshowListTimerthen
showListTimer:kill()
end
end
showListTimer=mp.add_periodic_timer(1,showList)
showListTimer:kill()
functionlist_marks()
ifshowListTimer:is_enabled() then
showListTimer:kill()
mp.osd_message("",0)
else
localdelString=showList()
ifdelStringanddelString:find(";") then
showListTimer:resume()
msg.info(delString)
else
showListTimer:kill()
end
end
end
mp.add_key_binding("ctrl+DEL", "delete_file", mark_delete)
mp.add_key_binding("alt+DEL", "list_marks", list_marks)
mp.add_key_binding("ctrl+shift+DEL", "clear_list", function() mp.osd_message("un-delete all"); del_list= {}; end)
mp.register_event("shutdown", delete)