Skip to content

Commit 1e508d2

Browse files
refactor: remove globals & add type annotations
1 parent abd6982 commit 1e508d2

File tree

7 files changed

+195
-93
lines changed

7 files changed

+195
-93
lines changed

.luacheckrc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
-- luacheck: ignore 131
1+
---@diagnostic disable: lowercase-global
22

33
std = 'luajit'
44

55
read_globals = {'mp'}
66

7-
allow_defined_top = true
7+
allow_defined_top = false
88

99
max_line_length = 80
1010

1111
max_comment_line_length = false
1212

1313
include_files = {
1414
'clipshot.lua',
15+
'discord.lua',
1516
'misc.lua',
1617
'open-dialog/*.lua'
1718
}
18-
19-
files['open-dialog/zenity.lua'] = {
20-
globals = {'table.merge'}
21-
}
22-
23-
-- vim: ft=lua

clipshot.lua

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
1-
NAME = 'mpv-screenshot.jpeg'
1+
---@class ClipshotOptions
2+
---@field name string
3+
---@field type string
4+
local o = {
5+
name = 'mpv-screenshot.jpeg',
6+
type = '' -- defaults to jpeg
7+
}
8+
require('mp.options').read_options(o, 'clipshot')
29

3-
if package.config:sub(1, 1) == '\\' then -- Windows
4-
SHOT = os.getenv('TEMP')..'\\'..NAME
5-
CMD = {
10+
local file, cmd
11+
12+
local lib = package.cpath:match('%p[\\|/]?%p(%a+)')
13+
if lib == 'so' then -- Linux/BSD
14+
file = '/tmp/'..o.name
15+
if os.getenv('XDG_SESSION_TYPE') == 'wayland' then -- Wayland
16+
cmd = {'sh', '-c', ('wl-copy < %q'):format(file)}
17+
else -- Xorg
18+
local type = o.type ~= '' and o.type or 'image/jpeg'
19+
cmd = {'xclip', '-sel', 'c', '-t', type, '-i', file}
20+
end
21+
elseif lib == 'dll' then -- Windows
22+
file = os.getenv('TEMP')..'\\'..o.name
23+
cmd = {
624
'powershell', '-NoProfile', '-Command', ([[& {
725
Add-Type -Assembly System.Windows.Forms;
826
Add-Type -Assembly System.Drawing;
927
$shot = [Drawing.Image]::FromFile(%q);
1028
[Windows.Forms.Clipboard]::SetImage($shot);
11-
}]]):format(SHOT)
29+
}]]):format(file)
30+
}
31+
else -- MacOS
32+
file = os.getenv('TMPDIR')..'/'..o.name
33+
-- png: «class PNGf»
34+
local type = o.type ~= '' and o.type or 'JPEG picture'
35+
cmd = {
36+
'osascript', '-e', ([[¬
37+
set the clipboard to ( ¬
38+
read (POSIX file %q) as %s ¬
39+
) ¬
40+
]]):format(file, type)
1241
}
13-
else -- Unix
14-
SHOT = '/tmp/'..NAME
15-
-- os.getenv('OSTYPE') doesn't work
16-
local ostype = io.popen('printf "$OSTYPE"', 'r'):read()
17-
if ostype:sub(1, 6) == 'darwin' then -- MacOS
18-
CMD = {
19-
'osascript', '-e', ([[¬
20-
set the clipboard to ( ¬
21-
read (POSIX file %q) as JPEG picture ¬
22-
) ¬
23-
]]):format(SHOT)
24-
}
25-
else -- Linux/BSD
26-
if os.getenv('XDG_SESSION_TYPE') == 'wayland' then -- Wayland
27-
CMD = {'sh', '-c', ('wl-copy < %q'):format(SHOT)}
28-
else -- Xorg
29-
CMD = {'xclip', '-sel', 'c', '-t', 'image/jpeg', '-i', SHOT}
30-
end
31-
end
3242
end
3343

34-
function clipshot(arg)
44+
---@param arg string
45+
---@return fun()
46+
local function clipshot(arg)
3547
return function()
36-
mp.commandv('screenshot-to-file', SHOT, arg)
37-
mp.command_native_async({'run', unpack(CMD)}, function(suc, _, err)
38-
mp.osd_message(suc and 'Copied screenshot to clipboard' or err)
48+
mp.commandv('screenshot-to-file', file, arg)
49+
mp.command_native_async({'run', unpack(cmd)}, function(suc, _, err)
50+
mp.osd_message(suc and 'Copied screenshot to clipboard' or err, 1)
3951
end)
4052
end
4153
end

0 commit comments

Comments
 (0)