File: cio.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-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 27 # 28 # Clipboard IO automatically figures out whether to copy/paste the text 29 # clipboard, depending on the piping of its standard input/output. 30 # 31 # When both its standard input/output are piped, it first updates the text 32 # clipboard, and then emits its new value to stdout. 33 # 34 # When run on its own (unpiped), it just emits the current text clipboard 35 # value to stdout. 36 37 38 # handle help option(s) 39 case "$1" in 40 -h|--h|-help|--help) 41 awk '/^# +cio/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 42 exit 0 43 ;; 44 esac 45 46 get_clip() { 47 if [ -e /dev/clipboard ]; then 48 cat /dev/clipboard 49 return $? 50 fi 51 if [ "$(which wclip)" ]; then 52 wclip -o 53 return $? 54 fi 55 if [ "$(which xclip)" ]; then 56 xclip -sel clip -o 57 return $? 58 fi 59 if [ "$(which pbpaste)" ]; then 60 pbpaste 61 return $? 62 fi 63 if [ "$(which powershell.exe)" ]; then 64 powershell.exe -NoProfile -c ' 65 chcp 65001 | Out-Null 66 Get-Clipboard 67 ' 68 return $? 69 fi 70 71 printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr 72 exit 1 73 } 74 75 set_clip() { 76 if [ -e /dev/clipboard ]; then 77 cat > /dev/clipboard 78 return $? 79 fi 80 if [ "$(which wclip)" ]; then 81 wclip 82 return $? 83 fi 84 if [ "$(which xclip)" ]; then 85 xclip -sel clip 86 return $? 87 fi 88 if [ "$(which pbcopy)" ]; then 89 pbcopy 90 return $? 91 fi 92 93 if [ "$(which powershell.exe)" ]; then 94 powershell.exe -NoProfile -c ' 95 chcp 65001 | Out-Null 96 clip.exe 97 ' 98 return $? 99 fi 100 101 printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr 102 exit 1 103 } 104 105 # handle both `... | cio` and `... | cio | ...` 106 if [ -p /dev/stdin ]; then 107 cat | set_clip 108 # ... | cio | ... 109 if [ -p /dev/stdout ]; then 110 get_clip 111 fi 112 exit $? 113 fi 114 115 # handle `cio | ...` 116 if [ -p /dev/stdout ]; then 117 get_clip 118 exit $? 119 fi 120 121 # handle `cio ...` 122 if [ $# -gt 0 ]; then 123 cat "$@" | set_clip 124 exit $? 125 fi 126 127 # handle `cio` 128 get_clip 129 exit $?