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 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 Get-Clipboard 65 powershell.exe -NoProfile -c Get-Clipboard | python.exe -c ' 66 from sys import stdin 67 for l in stdin: 68 print(l, end="")' 69 return $? 70 fi 71 72 printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr 73 exit 1 74 } 75 76 set_clip() { 77 if [ -e /dev/clipboard ]; then 78 cat > /dev/clipboard 79 return $? 80 fi 81 if [ "$(which wclip)" ]; then 82 wclip 83 return $? 84 fi 85 if [ "$(which xclip)" ]; then 86 xclip -sel clip 87 return $? 88 fi 89 if [ "$(which pbcopy)" ]; then 90 pbcopy 91 return $? 92 fi 93 94 if [ "$(which powershell.exe)" ]; then 95 # powershell.exe -NoProfile -c Set-Clipboard -Value "$(cat)" 96 powershell.exe -NoProfile -c ' 97 [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding 98 $Input | Set-Clipboard' 99 return $? 100 fi 101 102 printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr 103 exit 1 104 } 105 106 # handle both `... | cio` and `... | cio | ...` 107 if [ -p /dev/stdin ]; then 108 cat | set_clip 109 # ... | cio | ... 110 if [ -p /dev/stdout ]; then 111 get_clip 112 fi 113 exit $? 114 fi 115 116 # handle `cio | ...` 117 if [ -p /dev/stdout ]; then 118 get_clip 119 exit $? 120 fi 121 122 # handle `cio ...` 123 if [ $# -gt 0 ]; then 124 cat "$@" | set_clip 125 exit $? 126 fi 127 128 # handle `cio` 129 get_clip 130 exit $?