Skip to content

Commit

Permalink
console.lua: use native clipboard property on win32 and wayland
Browse files Browse the repository at this point in the history
Since native clipboard is implemented on win32 and wayland,
use the clipboard property instead.

This fixes the problems with commandline implementations of clipboard:
- On win32, the powershell implementation is complex, and it can take
  several seconds to run for the first time.
- On wayland, it requires wl-paste to be installed, which isn't always
  available. It also works poorly on GNOME.
  • Loading branch information
na-na-hi committed Nov 22, 2024
1 parent d1469ea commit 408bb9e
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1096,38 +1096,18 @@ local function get_clipboard(clip)
return res.stdout
end
elseif platform == 'wayland' then
local res = utils.subprocess({
args = { 'wl-paste', clip and '-n' or '-np' },
playback_only = false,
})
if not res.error then
return res.stdout
if clip and mp.get_property('current-vo') then
return mp.get_property('clipboard/text', '')
end
elseif platform == 'windows' then
local res = utils.subprocess({
args = { 'powershell', '-NoProfile', '-Command', [[& {
Trap {
Write-Error -ErrorRecord $_
Exit 1
}
$clip = ""
if (Get-Command "Get-Clipboard" -errorAction SilentlyContinue) {
$clip = Get-Clipboard -Raw -Format Text -TextFormatType UnicodeText
} else {
Add-Type -AssemblyName PresentationCore
$clip = [Windows.Clipboard]::GetText()
}
$clip = $clip -Replace "`r",""
$u8clip = [System.Text.Encoding]::UTF8.GetBytes($clip)
[Console]::OpenStandardOutput().Write($u8clip, 0, $u8clip.Length)
}]] },
args = { 'wl-paste', '-np' },
playback_only = false,
})
if not res.error then
return res.stdout
end
elseif platform == 'windows' then
return mp.get_property('clipboard/text', '')
elseif platform == 'darwin' then
local res = utils.subprocess({
args = { 'pbpaste' },
Expand Down

0 comments on commit 408bb9e

Please sign in to comment.