Skip to content

Commit

Permalink
refactor warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Apr 10, 2024
1 parent 4a5e15a commit 4a8bf48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 11 additions & 12 deletions platform/nix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions subs2srs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion subtitles/observer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4a8bf48

Please sign in to comment.