Skip to content

Commit

Permalink
Fixed wsl encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
vertliba committed Mar 25, 2024
1 parent d7dc568 commit 6dba64a
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/pyperclip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,10 @@ def paste_windows():
def init_wsl_clipboard():

def copy_wsl(text):
text = _stringifyText(text)
base64_text = base64.b64encode(text.encode('utf-8')).decode('utf-8')
ps_script = f"$text = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('{base64_text}')); " \
f"Set-Clipboard -Value $text"
# '-noprofile' speeds up load time
p = subprocess.Popen(['powershell.exe', '-noprofile', '-command', ps_script],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()

if stderr:
raise Exception(f"Error copying to clipboard: {stderr.decode('utf-8')}")
text = _stringifyText(text) # Converts non-str values to str.
p = subprocess.Popen(['clip.exe'],
stdin=subprocess.PIPE, close_fds=True)
p.communicate(input=text.encode('utf-16le'))

def paste_wsl():
ps_script = '[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((Get-Clipboard -Raw)))'
Expand Down

0 comments on commit 6dba64a

Please sign in to comment.