-
Notifications
You must be signed in to change notification settings - Fork 4
/
clipboard-print
executable file
·133 lines (113 loc) · 3.8 KB
/
clipboard-print
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python2
import sys
import os
import subprocess
import optparse
import tempfile
try:
import pygtk
except ImportError:
# Fedora 23+
#if not os.path.isdir('/usr/lib64/pygtk/2.0'):
# raise
pygtklibdir = os.path.join('usr', 'lib64', 'pygtk', '2.0')
sys.path.insert(0, pygtklibdir)
#print('pre 2nd import')
try:
import pygtk
except ImportError:
pygtk = None
gtk = None
if pygtk is not None:
pygtk.require('2.0')
if gtk is not None:
import gtk
def program_name():
return os.path.basename(sys.argv[0])
def mac_clipboard_get_text():
with tempfile.TemporaryFile(mode='w+') as tf:
subprocess.check_call(['pbpaste'], stdout=tf)
tf.seek(0)
return tf.read()
def mac_clipboard_set_text(text):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
p.stdin.write(text)
p.stdin.close()
r = p.wait()
if r != 0:
raise RuntimeError('pbcopy returned ' + str(r))
def gtk_clipboard_get_text(selection):
'return textual data from the X11 clipboard. @selection should be "PRIMARY" or "CLIPBOARD"'
clipboard = gtk.Clipboard(selection=selection)
return clipboard.wait_for_text()
def gtk_clipboard_set_text(text, selection):
clipboard = gtk.Clipboard(selection=selection)
clipboard.set_text(text)
# make the data stick around after this process exits
clipboard.store()
def clipboard_get_text(selection):
if gtk is None:
return mac_clipboard_get_text()
else:
return gtk_clipboard_get_text(selection)
def clipboard_set_text(text, selection):
if gtk is None:
return mac_clipboard_set_text(text)
else:
return gtk_clipboard_set_text(text, selection)
main_function_list = []
def main_function(func):
global main_function_list
main_function_list.append(func)
return func
@main_function
def clipboard_print(args):
op = optparse.OptionParser(option_list=[
optparse.Option('-p', '--primary', action='store_true',
help=('use PRIMARY instead of CLIPBOARD as the X11 selection.\n'
'PRIMARY usually contains the currently selected text.')),
optparse.Option('-n', '--no-newline', action='store_true', help="don't add newline"),
])
(options, args) = op.parse_args(args)
if options.primary:
s = 'PRIMARY'
else:
s = 'CLIPBOARD'
sys.stdout.write(clipboard_get_text(s))
if not options.no_newline:
sys.stdout.write('\n')
@main_function
def clipboard_copy(args):
'copy file to clipboard'
op = optparse.OptionParser(usage='usage: %prog FILE', option_list=[
optparse.Option('-p', '--primary', action='store_true',
help='use PRIMARY instead of CLIPBOARD as the X11 selection'),
])
(options, args) = op.parse_args(args)
try:
(filename,) = args
except ValueError:
op.print_usage(sys.stderr)
sys.exit(2)
if options.primary:
s = 'PRIMARY'
else:
s = 'CLIPBOARD'
clipboard_set_text(open(filename).read(), selection=s)
@main_function
def clipboard_open_file_line(args):
'assume input in "filename:line" (ie. grep) format, open in editor'
(filename, line) = clipboard_get_text('PRIMARY').strip().split(':')
editor_cmd = os.environ.get('VISUAL')
if editor_cmd is None:
editor_cmd = os.environ.get('EDITOR', 'vim')
# FIXME: not all editors handle "filename +line" cmd line
return subprocess.call([ editor_cmd, filename, '+%s' % line ])
def main(args):
def to_command_name(s):
return s.replace('_', '-')
name_to_function = dict( (to_command_name(x.__name__), x)
for x in main_function_list )
return name_to_function[program_name()](args)
if __name__ == '__main__':
main(sys.argv[1:])