Repository navigation
Expand file tree
/
Copy pathplatform.lua
More file actions
107 lines (100 loc) 路 3.22 KB
/
Copy pathplatform.lua
File metadata and controls
107 lines (100 loc) 路 3.22 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
--[[
Copyright: Ren Tatsumoto and contributors
License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html
OS-related constants and functions.
]]
local h = require('helpers')
local mp = require('mp')
local utils = require('mp.utils')
local this = {}
local function get_fallback_video_dir()
return utils.join_path(
h.query_user_home_dir(),
(this.platform == this.Platform.macos and "Movies" or "Videos")
)
end
local function get_fallback_music_dir()
return utils.join_path(h.query_user_home_dir(), "Music")
end
this.Platform = {
gnu_linux = "gnu_linux",
macos = "macos",
windows = "windows",
}
this.platform = (
h.is_win() and this.Platform.windows
or h.is_mac() and this.Platform.macos
or this.Platform.gnu_linux
)
this.default_video_folder = (function()
if this.platform == this.Platform.gnu_linux then
return h.query_xdg_user_dir("VIDEOS") or get_fallback_video_dir()
else
return get_fallback_video_dir()
end
end)()
this.default_audio_folder = (function()
if this.platform == this.Platform.gnu_linux then
return h.query_xdg_user_dir("MUSIC") or get_fallback_music_dir()
else
return get_fallback_music_dir()
end
end)()
this.curl_exe = (this.platform == this.Platform.windows and 'curl.exe' or 'curl')
this.open_utility = (
this.platform == this.Platform.windows and 'explorer.exe'
or this.platform == this.Platform.macos and 'open'
or this.platform == this.Platform.gnu_linux and 'xdg-open'
)
this.open = function(file_or_url)
return mp.commandv('run', this.open_utility, file_or_url)
end
this.clipboard = (function()
local self = {}
if this.platform == this.Platform.windows then
self.clip_exe = "powershell.exe"
self.copy = function(text)
return h.subprocess({ self.clip_exe, '-command', 'Set-Clipboard -Value ' .. text })
end
else
if this.platform == this.Platform.macos then
self.clip_exe = "pbcopy"
self.clip_cmd = "LANG=en_US.UTF-8 pbcopy"
elseif h.is_wayland() then
self.clip_exe = "wl-copy"
self.clip_cmd = "wl-copy"
else
self.clip_exe = "xclip"
self.clip_cmd = "xclip -i -selection clipboard"
end
self.copy = function(text)
local handle = io.popen(self.clip_cmd, 'w')
if handle then
handle:write(text)
local success, status, signal = handle:close()
if success then
status = 0
end
return { status = status }
else
return { status = 1 }
end
end
end
return self
end)()
this.copy_or_open_url = function(url)
local cb = this.clipboard.copy(url)
if cb.status ~= 0 then
local msg = string.format(
"Failed to copy URL to clipboard, trying to open in browser instead. Make sure %s is installed.",
this.clipboard.clip_exe
)
h.notify_error(msg, "warn", 4)
this.open(url)
else
h.notify("Done! Copied URL to clipboard.", "info", 2)
end
return cb
end
return this