diff --git a/platform/nix.lua b/platform/nix.lua index 8fc9edc..40d268d 100644 --- a/platform/nix.lua +++ b/platform/nix.lua @@ -6,23 +6,31 @@ Platform-specific functions for *nix systems. ]] local h = require('helpers') -local self = {} -local clip = (function() - if h.is_mac() then - return 'LANG=en_US.UTF-8 pbcopy' - elseif h.is_wayland() then - return 'wl-copy' - else - return 'xclip -i -selection clipboard' - end -end)() +local self = { healthy = true, clip_util = "", clip_cmd = "", } + +local function is_installed(exe_name) + return os.execute("which " .. exe_name) == 0 +end + +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 + self.clip_util = "wl-copy" + self.clip_cmd = self.clip_util + self.healthy = is_installed(self.clip_util) +else + self.clip_util = "xclip" + self.clip_cmd = self.clip_util .. " -i -selection clipboard" + self.healthy = is_installed(self.clip_util) +end self.tmp_dir = function() return os.getenv("TMPDIR") or '/tmp' end self.copy_to_clipboard = function(text) - local handle = io.popen(clip, 'w') + local handle = io.popen(self.clip_cmd, 'w') handle:write(text) handle:close() end diff --git a/platform/win.lua b/platform/win.lua index 8b6333b..38d489c 100644 --- a/platform/win.lua +++ b/platform/win.lua @@ -9,7 +9,7 @@ local mp = require('mp') local h = require('helpers') local utils = require('mp.utils') local curl_tmpfile_path = utils.join_path(os.getenv('TEMP'), 'curl_tmp.txt') -local self = { windows = true, } +local self = { windows = true, healthy = true, clip_util="cmd", } mp.register_event('shutdown', function() os.remove(curl_tmpfile_path) diff --git a/subtitles/observer.lua b/subtitles/observer.lua index 10e9392..91a9ff0 100644 --- a/subtitles/observer.lua +++ b/subtitles/observer.lua @@ -142,6 +142,10 @@ end -- public self.copy_to_clipboard = function(_, text) + if platform.healthy == false then + h.notify(platform.clip_util .. " is not installed.", "error", 5) + return + end if not h.is_empty(text) then platform.copy_to_clipboard(self.clipboard_prepare(text)) end