-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindbuffer.py
More file actions
80 lines (64 loc) · 2.83 KB
/
findbuffer.py
File metadata and controls
80 lines (64 loc) · 2.83 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys, os, subprocess
# Import the Pywin32 package if installed (for clipboard functionality on Windows)
try:
import Pywin32.setup
import win32gui, win32con, win32clipboard
except ImportError:
pass
class PasteBoard:
"""
PasteBoard: A wrapper around pbcopy and pbpaste on OS X, SetClipboardData and GetClipboardData on Windows.
On other platforms it just stores the text locally
"""
buffer = ""
pboard = ""
# General and find buffer supported on OS X, general supported on Windows
general_supported = sys.platform.startswith("darwin") or sys.platform.startswith("linux")
try:
general_supported = general_supported or win32clipboard != None
except:
pass
find_supported = sys.platform.startswith("darwin")
def __init__(self):
if sys.platform.startswith("darwin") or sys.platform.startswith("linux"):
# Set language to UTF-8
self.env = os.environ.copy()
self.env["LANG"] = "en_US.UTF-8"
# Copy the given text to the find pasteboard
def set(self, text):
self.buffer = text
if sys.platform.startswith("darwin"):
proc = subprocess.Popen([ "pbcopy", "-pboard", self.pboard ], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=self.env)
proc.communicate(input=self.buffer.encode("utf-8"))
elif sys.platform.startswith("win"):
if self.general_supported and self.pboard == "general":
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_TEXT, self.buffer)
win32clipboard.CloseClipboard
elif sys.platform.startswith("linux"):
proc = subprocess.Popen([ "xclip", "-i", "-selection", "clipboard" ], shell=False, stdin=subprocess.PIPE, env=self.env)
res = proc.communicate(input=self.buffer.encode("utf-8"))
print("result", res)
# Return the text pasted from the find pasteboard
def get(self):
if sys.platform.startswith("darwin"):
proc = subprocess.Popen([ "pbpaste", "-pboard", self.pboard, "-Prefer", "txt" ], shell=False, stdout=subprocess.PIPE, env=self.env)
self.buffer = proc.communicate()[0].decode("utf-8")
elif sys.platform.startswith("win"):
if self.general_supported and self.pboard == "general":
win32clipboard.OpenClipboard()
self.buffer = win32clipboard.GetClipboardData(win32con.CF_TEXT)
win32clipboard.CloseClipboard
elif sys.platform.startswith("linux"):
proc = subprocess.Popen([ "xclip", "-o", "-selection", "clipboard" ], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=self.env)
self.buffer = proc.communicate()[0].decode("utf-8")
return self.buffer
class CopyBuffer(PasteBoard):
def __init__(self):
self.pboard = "general"
PasteBoard.__init__(self)
class FindBuffer(PasteBoard):
def __init__(self):
self.pboard = "find"
PasteBoard.__init__(self)