-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseparator.lua
132 lines (96 loc) · 3.49 KB
/
separator.lua
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
local mp = require "mp"
local msg = require "mp.msg"
local utils = require "mp.utils"
table.unpack = table.unpack or unpack -- Workaround for Lua 5.1
local OUTPUT_LOCATION = os.getenv("MPV_SEPARATOR_OUTPUT")
or mp.get_property("working-directory")
local BACKUP_LOCATION = ""
local FILES_SAVED = 0
local function notify(message, duration)
local duration = duration or 2
msg.info(message)
mp.osd_message(message, duration)
end
local function get_current_file_relative_path()
return mp.get_property("path")
end
local function execute_command(cmd, ...)
local args = {...}
local prepared_cmd = string.format(cmd, table.unpack(args))
return io.popen(prepared_cmd):read("*l")
end
local function create_backup_dir()
if #BACKUP_LOCATION == 0 then
BACKUP_LOCATION = execute_command(
'mktemp -d -p "%s" separator.XXXXXX',
OUTPUT_LOCATION
)
end
return BACKUP_LOCATION
end
local function operate_file(op, source, destination)
local result = execute_command('%s -nv "%s" "%s"', op, source, destination)
local skipped, _ = result:find('^skipped')
return result, skipped == 1
end
local function copy_file(source, destination)
return operate_file("cp", source, destination)
end
local function move_file(source, destination)
return operate_file("mv", source, destination)
end
local function increment_saved_files()
FILES_SAVED = FILES_SAVED + 1
end
local function copy_current_file()
local file = get_current_file_relative_path()
msg.info(string.format("Current file is '%s'", file))
local output_path = create_backup_dir()
notify("Copying current file...")
local copy_result, skipped = copy_file(file, output_path)
msg.info(copy_result)
if not skipped then
increment_saved_files()
end
msg.info(string.format("Output location is '%s'", output_path))
notify(string.format("Success! Total: %d", FILES_SAVED))
end
local function get_full_path(output_path, file)
local filename, _ = file:gsub(".*/(.*)", "%1")
return utils.join_path(output_path, filename)
end
local function update_current_file_path_on_playlist_entry(path)
local playlist_count = mp.get_property_number("playlist-count")
local curr_video_pos = mp.get_property_number("playlist-pos")
local time_pos = mp.get_property_number("time-pos")
mp.commandv("loadfile", path, "append")
mp.commandv("playlist-move", playlist_count, curr_video_pos)
mp.commandv("playlist-remove", curr_video_pos + 1)
mp.commandv("playlist-play-index", curr_video_pos)
local function action()
mp.set_property_number("time-pos", time_pos)
mp.unregister_event(action)
mp.unregister_event("file-loaded", action)
end
mp.register_event("file-loaded", action)
end
-- Almost identical functions for maintainability
local function move_current_file()
local file = get_current_file_relative_path()
msg.info(string.format("Current file is '%s'", file))
local output_path = create_backup_dir()
notify("Moving current file...")
move_result, skipped = move_file(file, output_path)
msg.info(move_result)
if not skipped then
local full_path = get_full_path(output_path, file)
update_current_file_path_on_playlist_entry(full_path)
increment_saved_files()
end
msg.info(string.format("Output location is '%s'", output_path))
notify(string.format("Success! Total: %d", FILES_SAVED))
end
mp.set_property("keep-open", "yes") -- Prevent mpv from exiting when the video ends
mp.set_property("quiet", "yes") -- Silence terminal.
mp.add_key_binding('Ctrl+m', "move_current_file", move_current_file)
mp.add_key_binding('Ctrl+d', "copy_current_file", copy_current_file)