File: cio.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # cio [options...] [filepath...] 27 # 28 # 29 # Clipboard IO automatically figures out whether to copy/paste from/to the 30 # clipboard, depending on the piping of its standard input/output. 31 # 32 # When both its standard input/output are piped, it first updates the text 33 # clipboard, and then emits its new value to the standard output. 34 # 35 # When run unpiped on its own, it just emits the current clipboard value to 36 # the standard output. 37 # 38 # The options are, available both in single and double-dash versions 39 # 40 # -c, -clear, -e, -empty clear/empty the clipboard 41 # -h, -help show this help message 42 43 44 case "$1" in 45 -h|--h|-help|--help) 46 awk '/^# +cio /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 47 exit 0 48 ;; 49 esac 50 51 if [ -e /dev/clipboard ]; then 52 get_clip='cat /dev/clipboard' 53 set_clip='cat > /dev/clipboard' 54 elif [ -e /usr/bin/wl-paste ]; then 55 # get_clip='wl-paste' 56 get_clip='wl-paste --no-newline' 57 elif [ -e /usr/bin/wclip ]; then 58 get_clip='wclip -o' 59 set_clip='wclip' 60 elif [ -e /usr/bin/xclip ]; then 61 get_clip='xclip -o' 62 set_clip='xclip -sel clip' 63 elif [ -e /usr/bin/pbpaste ]; then 64 get_clip='pbpaste' 65 fi 66 67 if [ -z "${set_clip}" ]; then 68 if [ -e /usr/bin/wl-copy ]; then 69 set_clip='wl-copy' 70 elif [ -e /usr/bin/pbcopy ]; then 71 set_clip='pbcopy' 72 fi 73 fi 74 75 case "$1" in 76 -c|--c|-clear|--clear|-e|--e|-empty|--empty) 77 ${set_clip} < /dev/null 78 return $? 79 ;; 80 esac 81 82 [ "$1" = '--' ] && shift 83 84 emit_clipboard() { 85 if [ -z "${get_clip}" ]; then 86 printf "can't read the clipboard\n" > /dev/stderr 87 exit 1 88 fi 89 ${get_clip} || exit $? 90 } 91 92 change_clipboard() { 93 if [ -z "${set_clip}" ]; then 94 printf "can't change the clipboard\n" > /dev/stderr 95 exit 1 96 fi 97 cat "$@" | ${set_clip} || exit $? 98 } 99 100 if [ $# -gt 0 ]; then 101 # handle `cio ...` or `cio ... | ...` 102 change_clipboard "$@" 103 if [ -p /dev/stdout ]; then 104 emit_clipboard 105 fi 106 exit 0 107 elif [ -p /dev/stdin ]; then 108 # handle `... | cio` or `... | cio | ...` 109 change_clipboard /dev/stdin 110 if [ -p /dev/stdout ]; then 111 emit_clipboard 112 fi 113 exit 0 114 else 115 # handle `cio` or `cio | ...` 116 emit_clipboard 117 exit 0 118 fi