From 4a8bf483e105e2d8064eba290cae9f044134fd83 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 10 Apr 2024 18:28:37 +0300 Subject: [PATCH] refactor warnings --- helpers.lua | 1 + platform/nix.lua | 23 +++++++++++------------ subs2srs.lua | 7 +++++++ subtitles/observer.lua | 5 ++++- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/helpers.lua b/helpers.lua index 74314d5..c38ccc8 100644 --- a/helpers.lua +++ b/helpers.lua @@ -80,6 +80,7 @@ this.subprocess = function(args, completion_fn) name = "subprocess", playback_only = false, capture_stdout = true, + capture_stderr = true, args = args } return command_native(command_table, completion_fn) diff --git a/platform/nix.lua b/platform/nix.lua index 276b259..c1d7783 100644 --- a/platform/nix.lua +++ b/platform/nix.lua @@ -8,25 +8,24 @@ Platform-specific functions for *nix systems. local h = require('helpers') local self = { healthy = true, clip_util = "", clip_cmd = "", } -local function is_installed(exe_name) - return os.execute("which " .. exe_name) == 0 -end - -local function is_xclip_installed() - local handle = io.popen("xclip -version 2>&1", "r") - local result = handle:read("*a") - handle:close() - return result:find("xclip version") ~= nil -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 + 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_installed(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() diff --git a/subs2srs.lua b/subs2srs.lua index 021d766..7eff85c 100644 --- a/subs2srs.lua +++ b/subs2srs.lua @@ -513,11 +513,18 @@ function menu:warn_formats(osd) end end +function menu:warn_clipboard(osd) + if subs_observer.autocopy_current_method() == "clipboard" and platform.healthy == false then + osd:red('warning: '):text(string.format("%s is not installed.", platform.clip_util)):newline() + end +end + function menu:print_legend(osd) osd:new_layer():size(config.menu_font_size):font(config.menu_font_name):align(4) self:print_header(osd) self:print_bindings(osd) self:warn_formats(osd) + self:warn_clipboard(osd) end function menu:print_selection(osd) diff --git a/subtitles/observer.lua b/subtitles/observer.lua index 91a9ff0..895d244 100644 --- a/subtitles/observer.lua +++ b/subtitles/observer.lua @@ -144,7 +144,6 @@ end 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)) @@ -265,6 +264,10 @@ self.autocopy_status_str = function() ) end +self.autocopy_current_method = function() + return autoclip_method.get() +end + local function notify_autocopy() if autoclip_enabled then copy_primary_sub()