From a487bd561ade17095a013ef61ca7e5ba245d3dad Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 20 Oct 2023 00:32:29 -0400 Subject: [PATCH] Fix clipboard contents sometimes failing to clear Former-commit-id: 8ecbbb6199864fda973e47605a10d737318f3993 Former-commit-id: eb625eded3f9518967ee9fa06201311e8571e394 --- commit.sh | 2 +- lib/clipclear.py | 27 +++++++++++++++++++++++++++ lib/sshyp.py | 21 +++++++-------------- port-jobs/CLIPBOARD.py | 21 +++++++-------------- port-jobs/SHEBANG.sh | 2 +- 5 files changed, 43 insertions(+), 30 deletions(-) create mode 100755 lib/clipclear.py diff --git a/commit.sh b/commit.sh index 3256f25..0bdc35c 100755 --- a/commit.sh +++ b/commit.sh @@ -1,4 +1,4 @@ #!/bin/sh -git add -f extra lib/sshyp.py lib/sshync.py lib/stweak.py port-jobs share LICENSE README.md package.sh commit.sh .gitignore +git add -f extra lib/sshyp.py lib/sshync.py lib/stweak.py lib/clipclear.py port-jobs share LICENSE README.md package.sh commit.sh .gitignore git commit -m "$1" git push diff --git a/lib/clipclear.py b/lib/clipclear.py new file mode 100755 index 0000000..3ba9f52 --- /dev/null +++ b/lib/clipclear.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +from hashlib import sha512 +from subprocess import PIPE, run +from sys import argv +from time import sleep + +# set period of time to wait before attempting to clear clipboard +sleep(30) + +# store a hash of the current clipboard contents for later comparison +_hash_paste = sha512() +_hash_paste.update(run('wl-paste', stdout=PIPE).stdout.strip()) + +# if the supplied hash matches the hash of the current clipboard contents, clear the clipboard +if argv[1] == _hash_paste.hexdigest(): + if argv[2] == 'wsl': + run(('powershell.exe', '-c', 'Set-Clipboard')) + elif argv[2] == 'wayland': + run(('wl-copy', '-c')) + elif argv[2] == 'haiku': + run(('clipboard', '-r')) + elif argv[2] == 'mac': + run('pbcopy', input=b'') + elif argv[2] == 'termux': + run(("termux-clipboard-set", "''")) + elif argv[2] == 'x11': + run(('xclip', '-i', '/dev/null', '-sel', 'c')) diff --git a/lib/sshyp.py b/lib/sshyp.py index 8a89af4..a8963d2 100755 --- a/lib/sshyp.py +++ b/lib/sshyp.py @@ -547,36 +547,29 @@ def copy_data(): # WSL clipboard detection if 'WSL_DISTRO_NAME' in environ: run(('powershell.exe', '-c', "Set-Clipboard '" + _copy_subject.replace("'", "''") + "'")) - Popen("sleep 30; temp=$(echo \"$(powershell.exe -Command \'Get-FileHash -InputStream $([IO.MemoryStream]" - "::new([byte[]][char[]]\"$(Get-Clipboard)\")) -Algorithm SHA512 | Select-Object -ExpandProperty " - f"Hash\')\" | dos2unix); test \'{_hash.hexdigest().upper()}\' = \"$temp\" && powershell.exe -c " - "Set-Clipboard", shell=True, stdout=DEVNULL, stderr=DEVNULL) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'wsl'), stdout=DEVNULL, + stderr=DEVNULL) # Wayland clipboard detection elif 'WAYLAND_DISPLAY' in environ: run('wl-copy', stdin=Popen(('printf', '%b', _copy_subject.replace('\\', '\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \'{_hash.hexdigest() + 2*' ' + '-'}\' = \"$(printf \"$(wl-paste)\" | sha512sum)\" " - "&& wl-copy -c", shell=True) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'wayland')) # Haiku clipboard detection elif uname()[0] == 'Haiku': run(('clipboard', '-c', _copy_subject)) - Popen(f"sleep 30; test \'{_hash.hexdigest() + 2 * ' ' + '-'}\' = \"$(printf \"$(clipboard -p)\" | sha512sum)\" " - "&& clipboard -r", shell=True) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'haiku')) # MacOS clipboard detection elif uname()[0] == 'Darwin': run('pbcopy', stdin=Popen(('printf', '%b', _copy_subject.replace('\\', '\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \'{_hash.hexdigest() + 2 * ' ' + '-'}\' = \"$(printf \"$(pbpaste)\" | shasum -a 512)\" " - "&& printf '' | pbcopy", shell=True) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'mac')) # Termux (Android) clipboard detection elif isdir("/data/data/com.termux"): run(('termux-clipboard-set', _copy_subject)) - Popen(f"sleep 30; test \'{_hash.hexdigest() + 2 * ' ' + '-'}\' = \"$(printf \"$(termux-clipboard-get)\" | " - f"sha512sum)\" && termux-clipboard-set ''", shell=True) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'termux')) # X11 clipboard detection elif 'DISPLAY' in environ: run(('xclip', '-sel', 'c'), stdin=Popen(('printf', '%b', _copy_subject.replace('\\', '\\\\')), stdout=PIPE) .stdout) - Popen(f"sleep 30; test \'{_hash.hexdigest() + 2*' ' + '-'}\' = \"$(printf \"$(xclip -o -sel c)\" | sha512sum)\"" - " && xclip -i /dev/null -sel c", shell=True) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'x11')) else: print('\n\u001b[38;5;9merror: clipboard tool could not be determined\n\nnote that the clipboard does not ' 'function in a raw tty\u001b[0m\n') diff --git a/port-jobs/CLIPBOARD.py b/port-jobs/CLIPBOARD.py index 6ed5c14..31ae706 100755 --- a/port-jobs/CLIPBOARD.py +++ b/port-jobs/CLIPBOARD.py @@ -9,30 +9,23 @@ arguments, replacement = argv[1:], None if len(arguments) > 0: if arguments[0] == 'WSL': replacement = """run(('powershell.exe', '-c', "Set-Clipboard '" + _copy_subject.replace("'", "''") + "'")) - Popen(f"sleep 30; temp=$(echo \\"$(powershell.exe -Command \\'Get-FileHash -InputStream $([IO.MemoryStream]::new([byte[]][char[]]\\"$(Get-Clipboard)\\")) -Algorithm SHA512 | Select-Object -ExpandProperty Hash\\')\\" | dos2unix); test \\'{_hash.hexdigest().upper()}\\' = \\"$temp\\" && powershell.exe -c Set-Clipboard", shell=True, stdout=DEVNULL, stderr=DEVNULL)""" + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'wsl'), stdout=DEVNULL, stderr=DEVNULL)""" elif arguments[0] == 'MAC': replacement = """run('pbcopy', stdin=Popen(('printf', '%b', _copy_subject.replace('\\\\\\', '\\\\\\\\\\\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \\'{_hash.hexdigest() + 2 * ' ' + '-'}\\' = \\"$(printf \\"$(pbpaste)\\" | shasum -a 512)\\" && printf '' | pbcopy", shell=True)""" + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'mac'))""" elif arguments[0] == 'HAIKU': replacement = """run(('clipboard', '-c', _copy_subject)) - Popen(f"sleep 30; test \\'{_hash.hexdigest() + 2 * ' ' + '-'}\\' = \\"$(printf \\"$(clipboard -p)\\" | sha512sum)\\" && clipboard -r", shell=True)""" + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'haiku'))""" elif arguments[0] == 'TERMUX': replacement = """run(('termux-clipboard-set', _copy_subject)) - Popen(f"sleep 30; test \\'{_hash.hexdigest() + 2 * ' ' + '-'}\\' = \\"$(printf \\"$(termux-clipboard-get)\\" | sha512sum)\\" && termux-clipboard-set ''", shell=True)""" - elif arguments[0] == 'LINUX': + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'termux'))""" + elif arguments[0] in ('LINUX', 'BSD'): replacement = """if 'WAYLAND_DISPLAY' in environ: run('wl-copy', stdin=Popen(('printf', '%b', _copy_subject.replace('\\\\\\', '\\\\\\\\\\\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \\'{_hash.hexdigest() + 2*' ' + '-'}\\' = \\"$(printf \\"$(wl-paste)\\" | sha512sum)\\" && wl-copy -c", shell=True) + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'wayland')) else: run(('xclip', '-sel', 'c'), stdin=Popen(('printf', '%b', _copy_subject.replace('\\\\\\', '\\\\\\\\\\\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \\'{_hash.hexdigest() + 2*' ' + '-'}\\' = \\"$(printf \\"$(xclip -o -sel c)\\" | sha512sum)\\" && xclip -i /dev/null -sel c", shell=True)""" - elif arguments[0] == 'BSD': - replacement = """if 'WAYLAND_DISPLAY' in environ: - run('wl-copy', stdin=Popen(('printf', '%b', _copy_subject.replace('\\\\\\', '\\\\\\\\\\\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \\'{_hash.hexdigest() + 2*' ' + '-'}\\' = \\"$(printf \\"$(wl-paste)\\" | sha512sum)\\" && wl-copy -c", shell=True) - else: - run(('xclip', '-sel', 'c'), stdin=Popen(('printf', '%b', _copy_subject.replace('\\\\\\', '\\\\\\\\\\\\\\')), stdout=PIPE).stdout) - Popen(f"sleep 30; test \\'{_hash.hexdigest()}\\' = \\"$(printf \\"$(xclip -o -sel c)\\" | sha512sum)\\" && xclip -i /dev/null -sel c", shell=True)""" + Popen((realpath(__file__).rsplit('/', 1)[0] + "/clipclear.py", _hash.hexdigest(), 'x11'))""" else: s_exit() diff --git a/port-jobs/SHEBANG.sh b/port-jobs/SHEBANG.sh index 0e687e9..1cbdca9 100755 --- a/port-jobs/SHEBANG.sh +++ b/port-jobs/SHEBANG.sh @@ -2,5 +2,5 @@ if [ "$1" = 'PREP' ]; then sed -i '1 s/.*/#!\/bin\/env\ python3.11/' ./*.py else - sed -i '1 s/.*/#!\/bin\/env\ python3.11/' ./working/sshyp.py + sed -i '1 s/.*/#!\/bin\/env\ python3.11/' ./working/sshyp.py ./working/clipclear.py fi