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/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' 61 get_clip='wl-paste --no-newline' 62 elif [ -e /usr/bin/wclip ]; then 63 get_clip='wclip -o' 64 set_clip='wclip' 65 elif [ -e /usr/bin/xclip ]; then 66 get_clip='xclip -o' 67 set_clip='xclip -sel clip' 68 elif [ -e /usr/bin/pbpaste ]; then 69 get_clip='pbpaste' 70 fi 71 72 if [ -z "${set_clip}" ]; then 73 if [ -e /usr/bin/wl-copy ]; then 74 set_clip='wl-copy' 75 elif [ -e /usr/bin/pbcopy ]; then 76 set_clip='pbcopy' 77 fi 78 fi 79 80 case "$1" in 81 -c|--c|-clear|--clear|-e|--e|-empty|--empty) 82 ${set_clip} < /dev/null 83 return $? 84 ;; 85 esac 86 87 [ "$1" = '--' ] && shift 88 89 emit_clipboard() { 90 if [ -z "${get_clip}" ]; then 91 printf "can't read the clipboard\n" > /dev/stderr 92 exit 1 93 fi 94 ${get_clip} || exit $? 95 } 96 97 change_clipboard() { 98 if [ -z "${set_clip}" ]; then 99 printf "can't change the clipboard\n" > /dev/stderr 100 exit 1 101 fi 102 cat "$@" | ${set_clip} || exit $? 103 } 104 105 if [ $# -gt 0 ]; then 106 # handle `cio ...` or `cio ... | ...` 107 change_clipboard "$@" 108 if [ -p /dev/stdout ]; then 109 emit_clipboard 110 fi 111 exit 0 112 elif [ -p /dev/stdin ]; then 113 # handle `... | cio` or `... | cio | ...` 114 change_clipboard /dev/stdin 115 if [ -p /dev/stdout ]; then 116 emit_clipboard 117 fi 118 exit 0 119 else 120 # handle `cio` or `cio | ...` 121 emit_clipboard 122 exit 0 123 fi