diff cmd2.py @ 421:fc63f0aad022

Additional support for native pbcopy for clipboard redirect (">") on MacOSX.
author Jason Ledbetter <jasonbrent@gmail.com
date Wed, 22 Jun 2011 21:56:20 -0400
parents b6beae2a2f46
children 6ffa49335dcb
line wrap: on
line diff
--- a/cmd2.py	Wed Feb 02 05:26:37 2011 -0800
+++ b/cmd2.py	Wed Jun 22 21:56:20 2011 -0400
@@ -152,6 +152,9 @@
         errmsg = """Redirecting to or from paste buffer requires pywin32
 to be installed on operating system.
 Download from http://sourceforge.net/projects/pywin32/"""
+    elif sys.platform[:3] == 'dar':
+        # Use built in pbcopy on Mac OSX
+        pass
     else:
         errmsg = """Redirecting to or from paste buffer requires xclip 
 to be installed on operating system.
@@ -183,6 +186,25 @@
         def get_paste_buffer(*args):
             raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/')
         write_to_paste_buffer = get_paste_buffer
+elif sys.platform == 'darwin':
+    can_clip = False
+    try:
+        # test for pbcopy - AFAIK, should always be installed on MacOS
+        subprocess.check_call('pbcopy -help', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
+        can_clip = True
+    except (subprocess.CalledProcessError, OSError, IOError):
+        pass
+    if can_clip:
+        def get_paste_buffer():
+            pbcopyproc = subprocess.Popen('pbcopy -help', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
+            return pbcopyproc.stdout.read()
+        def write_to_paste_buffer(txt):
+            pbcopyproc = subprocess.Popen('pbcopy', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
+            pbcopyproc.communicate(txt.encode())
+    else:
+        def get_paste_buffer(*args):
+            raise OSError, pastebufferr % ('pbcopy', 'On MacOS X - error should not occur - part of the default installation')
+        write_to_paste_buffer = get_paste_buffer
 else:
     can_clip = False
     try: