|
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') |
2 | 9 |
|
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 = { |
6 | 24 | 'powershell', '-NoProfile', '-Command', ([[& { |
7 | 25 | Add-Type -Assembly System.Windows.Forms; |
8 | 26 | Add-Type -Assembly System.Drawing; |
9 | 27 | $shot = [Drawing.Image]::FromFile(%q); |
10 | 28 | [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) |
12 | 41 | } |
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 |
32 | 42 | end |
33 | 43 |
|
34 | | -function clipshot(arg) |
| 44 | +---@param arg string |
| 45 | +---@return fun() |
| 46 | +local function clipshot(arg) |
35 | 47 | 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) |
39 | 51 | end) |
40 | 52 | end |
41 | 53 | end |
|
0 commit comments