-
Notifications
You must be signed in to change notification settings - Fork 4
/
locatefile.lua
146 lines (134 loc) · 3.86 KB
/
locatefile.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- DEBUGGING
--
-- Debug messages will be printed to stdout with mpv command line option
-- `--msg-level='locatefile=debug'`
local msg = require('mp.msg')
local mputils = require('mp.utils')
-- for ubuntu
url_browser_linux_cmd = "xdg-open \"$url\""
file_browser_linux_cmd = "dbus-send --print-reply --dest=org.freedesktop.FileManager1 /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:\"file:$path\" string:\"\""
-- for macos
url_browser_macos_cmd = "open \"$url\""
-- file_browser_macos_cmd = "osascript -e 'tell application \"Finder\"' -e 'set frontmost to true' -e 'reveal (POSIX file \"$path\")' -e 'end tell'"
file_browser_macos_cmd = "open -a Finder -R \"$path\""
-- for windows
url_browser_windows_cmd = "explorer \"$url\""
file_browser_windows_cmd = "explorer /select,\"$path\""
--// check if it's a url/stream
function is_url(path)
if path ~= nil and string.sub(path,1,4) == "http" then
return true
else
return false
end
end
--// check if macos
function is_macos()
local homedir = os.getenv("HOME")
if homedir ~= nil and string.sub(homedir,1,6) == "/Users" then
return true
else
return false
end
end
--// check if windows
function is_windows()
local windir = os.getenv("windir")
if windir~=nil then
return true
else
return false
end
end
--// create temporary script
function create_temp_file(content)
local tmp_filename = os.tmpname()
local tmp_file = io.open(tmp_filename, "wb")
tmp_file:write(content)
io.close(tmp_file)
return tmp_filename
end
--// path separator stuffs
function path_sep()
if is_windows() then
return "\\"
else
return "/"
end
end
function split_by_separator(filepath)
local t = {}
local part_pattern = string.format("([^%s]+)", path_sep())
for str in filepath:gmatch(part_pattern) do
table.insert(t, str)
end
return t
end
function path_root()
if path_sep() == "/" then
return "/"
else
return ""
end
end
--// Extract file dir from url
function normalize(relative_path, base_dir)
base_dir = base_dir or mputils.getcwd()
local full_path = mputils.join_path(base_dir, relative_path)
local parts = split_by_separator(full_path)
local idx = 1
repeat
if parts[idx] == ".." then
table.remove(parts, idx)
table.remove(parts, idx - 1)
idx = idx - 2
elseif parts[idx] == "." then
table.remove(parts, idx)
idx = idx - 1
end
idx = idx + 1
until idx > #parts
return path_root() .. table.concat(parts, path_sep())
end
--// handle "locate-current-file" function triggered by a key in "input.conf"
function locate_current_file()
local path = mp.get_property("path")
if path ~= nil then
local cmd = ""
if is_url(path) then
msg.debug("Url detected '" .. path .. "', your OS web browser will be launched.")
if is_windows() then
msg.debug("Windows detected.")
cmd = url_browser_windows_cmd
elseif is_macos() then
msg.debug("macOS detected.")
cmd = url_browser_macos_cmd
else
msg.debug("Linux detected.")
cmd = url_browser_linux_cmd
end
cmd = cmd:gsub("$url", path)
else
msg.debug("File detected '" .. path .. "', your OS file browser will be launched.")
if is_windows() then
msg.debug("Windows detected.")
cmd = file_browser_windows_cmd
path = path:gsub("/", "\\")
elseif is_macos() then
msg.debug("macOS detected.")
cmd = file_browser_macos_cmd
else
msg.debug("Linux detected.")
cmd = file_browser_linux_cmd
end
path = normalize(path)
cmd = cmd:gsub("$path", path)
end
msg.debug("Command to be executed: '" .. cmd .. "'")
mp.osd_message('Browse \n' .. path)
os.execute(cmd)
else
msg.debug("'path' property was empty, no media has been loaded.")
end
end
mp.add_key_binding(nil, "locate-current-file", locate_current_file)