File: cio.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2025 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/empty the clipboard 41 # -clear clear/empty the clipboard 42 # -e clear/empty the clipboard 43 # -empty clear/empty the clipboard 44 # 45 # -h show this help message 46 # -help show this help message 47 48 49 case "$1" in 50 -h|--h|-help|--help) 51 awk '/^# +cio /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 52 exit 0 53 ;; 54 esac 55 56 if [ -e /dev/clipboard ]; then 57 get_clip='cat /dev/clipboard' 58 set_clip='cat > /dev/clipboard' 59 elif [ -e /usr/bin/wl-paste ]; then 60 get_clip='wl-paste --no-newline' 61 elif [ -e /usr/bin/wclip ]; then 62 get_clip='wclip -o' 63 set_clip='wclip' 64 elif [ -e /usr/bin/xclip ]; then 65 get_clip='xclip -o' 66 set_clip='xclip -sel clip' 67 elif [ -e /usr/bin/pbpaste ]; then 68 get_clip='pbpaste' 69 fi 70 71 if [ -z "${set_clip}" ]; then 72 if [ -e /usr/bin/wl-copy ]; then 73 set_clip='wl-copy' 74 elif [ -e /usr/bin/pbcopy ]; then 75 set_clip='pbcopy' 76 fi 77 fi 78 79 case "$1" in 80 -c|--c|-clear|--clear|-e|--e|-empty|--empty) 81 ${set_clip} < /dev/null 82 return $? 83 ;; 84 esac 85 86 [ "$1" = "--" ] && shift 87 88 emit_clipboard() { 89 if [ -z "${get_clip}" ]; then 90 printf "can't read the clipboard\n" > /dev/stderr 91 exit 1 92 fi 93 ${get_clip} || exit $? 94 } 95 96 change_clipboard() { 97 if [ -z "${set_clip}" ]; then 98 printf "can't change the clipboard\n" > /dev/stderr 99 exit 1 100 fi 101 cat "$@" | ${set_clip} || exit $? 102 } 103 104 if [ $# -gt 0 ]; then 105 # handle `cio ...` or `cio ... | ...` 106 change_clipboard "$@" 107 if [ -p /dev/stdout ]; then 108 emit_clipboard 109 fi 110 exit 0 111 elif [ -p /dev/stdin ]; then 112 # handle `... | cio` or `... | cio | ...` 113 change_clipboard /dev/stdin 114 if [ -p /dev/stdout ]; then 115 emit_clipboard 116 fi 117 exit 0 118 else 119 # handle `cio` or `cio | ...` 120 emit_clipboard 121 exit 0 122 fi