File: cio.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2024 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 # show help message, extracting the info-comment at the start 42 # of this file, and quit 43 awk '/^# +cio/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 44 exit 0 45 ;; 46 esac 47 48 get_clip() { 49 if [ -e /dev/clipboard ]; then 50 cat /dev/clipboard 51 return $? 52 fi 53 if [ -e wl-paste ]; then 54 wl-paste 55 return $? 56 fi 57 if [ -e wclip ]; then 58 wclip -o 59 return $? 60 fi 61 if [ -e xclip ]; then 62 xclip -sel clip -o 63 return $? 64 fi 65 if [ -e pbpaste ]; then 66 pbpaste 67 return $? 68 fi 69 if [ -e powershell.exe ]; then 70 powershell.exe -c Get-Clipboard 71 return $? 72 fi 73 74 # printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr 75 # exit 1 76 powershell.exe -c Get-Clipboard 77 return $? 78 } 79 80 set_clip() { 81 if [ -e /dev/clipboard ]; then 82 cat > /dev/clipboard 83 return $? 84 fi 85 if [ -e wl-copy ]; then 86 wl-copy 87 return $? 88 fi 89 if [ -e wclip ]; then 90 wclip 91 return $? 92 fi 93 if [ -e xclip ]; then 94 xclip -sel clip 95 return $? 96 fi 97 if [ -e pbcopy ]; then 98 pbcopy 99 return $? 100 fi 101 if [ -e clip.exe ]; then 102 clip.exe 103 return $? 104 fi 105 106 # printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr 107 # exit 1 108 clip.exe 109 return $? 110 } 111 112 # handle both `... | cio` and `... | cio | ...` 113 if [ -p /dev/stdin ]; then 114 cat | set_clip 115 # ... | cio | ... 116 if [ -p /dev/stdout ]; then 117 get_clip 118 fi 119 exit $? 120 fi 121 122 # handle `cio | ...` 123 if [ -p /dev/stdout ]; then 124 get_clip 125 exit $? 126 fi 127 128 # handle `cio ...` 129 if [ $# -gt 0 ]; then 130 cat "$@" | set_clip 131 exit $? 132 fi 133 134 # handle `cio` 135 get_clip 136 exit $?