Commits

ikame committed 969eb7c

Better clipboard access from the terminal

Comments (0)

Files changed (1)

  • +42 -0
    cb
+#!/usr/bin/env python
+"""System clipboard access.
+
+If executed normally send the contents of the clipboard to stdout. If you pipe
+to it, it will put that content in the clipboard.
+"""
+
+import sys
+import argparse
+import subprocess
+import shlex
+
+
+if 'darwin' in sys.platform:
+    copy_cmd = 'pbcopy'
+    paste_cmd = 'pbpaste'
+elif 'linux' in sys.platform:
+    copy_cmd = paste_cmd = 'xclip'
+else:
+    sys.exit('Unsupported platform')
+
+
+copy = shlex.split(copy_cmd)
+paste = shlex.split(paste_cmd)
+
+
+def main(argv=None):
+    if argv is None:
+        argv = sys.argv[1:]
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.parse_args(argv)
+
+    input_data = None if sys.stdin.isatty() else sys.stdin
+
+    if input_data:
+        subprocess.call(copy, stdin=input_data)
+    else:
+        subprocess.call(paste, stdout=sys.stdout)
+
+
+if __name__ == '__main__':
+    sys.exit(main())