diff options
Diffstat (limited to 'config/mpv/scripts/subs2srs/platform')
| -rw-r--r-- | config/mpv/scripts/subs2srs/platform/init.lua | 14 | ||||
| -rw-r--r-- | config/mpv/scripts/subs2srs/platform/nix.lua | 49 | ||||
| -rw-r--r-- | config/mpv/scripts/subs2srs/platform/win.lua | 72 |
3 files changed, 135 insertions, 0 deletions
diff --git a/config/mpv/scripts/subs2srs/platform/init.lua b/config/mpv/scripts/subs2srs/platform/init.lua new file mode 100644 index 0000000..825d8d4 --- /dev/null +++ b/config/mpv/scripts/subs2srs/platform/init.lua @@ -0,0 +1,14 @@ +--[[ +Copyright: Ren Tatsumoto and contributors +License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html + +Platform-specific functions. +]] + +local h = require('helpers') + +if h.is_win() then + return require('platform.win') +else + return require('platform.nix') +end diff --git a/config/mpv/scripts/subs2srs/platform/nix.lua b/config/mpv/scripts/subs2srs/platform/nix.lua new file mode 100644 index 0000000..cbf6c85 --- /dev/null +++ b/config/mpv/scripts/subs2srs/platform/nix.lua @@ -0,0 +1,49 @@ +--[[ +Copyright: Ren Tatsumoto and contributors +License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html + +Platform-specific functions for *nix systems. +]] + +local h = require('helpers') +local self = { healthy = true, clip_util = "", clip_cmd = "", } + +if h.is_mac() then + self.clip_util = "pbcopy" + self.clip_cmd = "LANG=en_US.UTF-8 " .. self.clip_util +elseif h.is_wayland() then + local function is_wl_copy_installed() + local handle = h.subprocess { 'wl-copy', '--version' } + return handle.status == 0 and handle.stdout:match("wl%-clipboard") ~= nil + end + + self.clip_util = "wl-copy" + self.clip_cmd = self.clip_util + self.healthy = is_wl_copy_installed() +else + local function is_xclip_installed() + local handle = h.subprocess { 'xclip', '-version' } + return handle.status == 0 and handle.stderr:match("xclip version") ~= nil + end + + self.clip_util = "xclip" + self.clip_cmd = self.clip_util .. " -i -selection clipboard" + self.healthy = is_xclip_installed() +end + +self.tmp_dir = function() + return os.getenv("TMPDIR") or '/tmp' +end + +self.copy_to_clipboard = function(text) + local handle = io.popen(self.clip_cmd, 'w') + handle:write(text) + handle:close() +end + +self.curl_request = function(url, request_json, completion_fn) + local args = { 'curl', '-s', url, '-X', 'POST', '-d', request_json } + return h.subprocess(args, completion_fn) +end + +return self diff --git a/config/mpv/scripts/subs2srs/platform/win.lua b/config/mpv/scripts/subs2srs/platform/win.lua new file mode 100644 index 0000000..e40dd7a --- /dev/null +++ b/config/mpv/scripts/subs2srs/platform/win.lua @@ -0,0 +1,72 @@ +--[[ +Copyright: Ren Tatsumoto and contributors +License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html + +Platform-specific functions for Windows. +]] + +local mp = require('mp') +local h = require('helpers') +local utils = require('mp.utils') +local base64 = require('utils.base64') +local self = { windows = true, healthy = true, clip_util = "cmd", } +local tmp_files = {} + +mp.register_event('shutdown', function() + for _, file in ipairs(tmp_files) do + os.remove(file) + end +end) + +self.tmp_dir = function() + return os.getenv('TEMP') +end + +self.copy_to_clipboard = function(text) + local args = { + "powershell", "-NoLogo", "-NoProfile", "-WindowStyle", "Hidden", "-Command", + string.format( + "Set-Clipboard ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('%s')))", + base64.enc(text) + ) + } + return h.subprocess_detached( + args, + function() + end + ) +end + +self.gen_random_tmp_file_path = function() + return utils.join_path(self.tmp_dir(), string.format('curl_tmp_%d.txt', math.random(10 ^ 9))) +end + +self.gen_unique_tmp_file_path = function() + local curl_tmpfile_path = self.gen_random_tmp_file_path() + while h.file_exists(curl_tmpfile_path) do + curl_tmpfile_path = self.gen_random_tmp_file_path() + end + return curl_tmpfile_path +end + +self.curl_request = function(url, request_json, completion_fn) + local curl_tmpfile_path = self.gen_unique_tmp_file_path() + local handle = io.open(curl_tmpfile_path, "w") + handle:write(request_json) + handle:close() + table.insert(tmp_files, curl_tmpfile_path) + local args = { + 'curl', + '-s', + url, + '-H', + 'Content-Type: application/json; charset=UTF-8', + '-X', + 'POST', + '--data-binary', + table.concat { '@', curl_tmpfile_path } + } + return h.subprocess(args, completion_fn) +end + +return self |
