forked from 2d6/talon_german
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclipscanner.py
44 lines (38 loc) · 1.03 KB
/
clipscanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sys
import subprocess
import time
from talon import actions
USE_XCLIP = False
if (
sys.platform.startswith('freebsd') or
sys.platform.startswith('linux') or
sys.platform.startswith('darwin')
):
try:
subprocess.run(['xclip', '-version'])
USE_XCLIP = True
except:
print("installing xclip might make insertion way more snappy")
print("sudo apt install xclip")
class ClipScanner:
def __enter__(self):
if USE_XCLIP:
try:
self.buffer = subprocess.check_output(['xclip', '-o'])
except subprocess.CalledProcessError as e:
print("'xclip -o' returned error: ", e)
self.buffer = u''
self.clear()
return self
def __exit__(self, type, value, tb):
if USE_XCLIP:
with subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE) as proc:
proc.stdin.write(self.buffer)
def get_selection(self):
if USE_XCLIP:
return subprocess.check_output(['xclip', '-o'], universal_newlines=True)
else:
return actions.edit.selected_text()
def clear(self):
if USE_XCLIP:
subprocess.run(['xclip', '-i', '/dev/null'])