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 case "$1" in
  39     -h|--h|-help|--help)
  40         awk '/^# +cio/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  41         exit 0
  42     ;;
  43 esac
  44 
  45 get_clip() {
  46     if [ -e /dev/clipboard ]; then
  47         cat /dev/clipboard
  48         return $?
  49     fi
  50     if [ "$(which wclip)" ]; then
  51         wclip -o
  52         return $?
  53     fi
  54     if [ "$(which xclip)" ]; then
  55         xclip -sel clip -o
  56         return $?
  57     fi
  58     if [ "$(which pbpaste)" ]; then
  59         pbpaste
  60         return $?
  61     fi
  62     if [ "$(which powershell.exe)" ]; then
  63         powershell.exe -NoProfile -c '
  64             chcp 65001 | Out-Null
  65             Get-Clipboard
  66         '
  67         return $?
  68     fi
  69 
  70     printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr
  71     exit 1
  72 }
  73 
  74 set_clip() {
  75     if [ -e /dev/clipboard ]; then
  76         cat > /dev/clipboard
  77         return $?
  78     fi
  79     if [ "$(which wclip)" ]; then
  80         wclip
  81         return $?
  82     fi
  83     if [ "$(which xclip)" ]; then
  84         xclip -sel clip
  85         return $?
  86     fi
  87     if [ "$(which pbcopy)" ]; then
  88         pbcopy
  89         return $?
  90     fi
  91 
  92     if [ "$(which powershell.exe)" ]; then
  93         powershell.exe -NoProfile -c '
  94             chcp 65001 | Out-Null
  95             clip.exe
  96         '
  97         return $?
  98     fi
  99 
 100     printf "\e[31mcan't use the clipboard\e[0m\n" > /dev/stderr
 101     exit 1
 102 }
 103 
 104 # handle both `... | cio` and `... | cio | ...`
 105 if [ -p /dev/stdin ]; then
 106     cat | set_clip
 107     # ... | cio | ...
 108     if [ -p /dev/stdout ]; then
 109         get_clip
 110     fi
 111     exit $?
 112 fi
 113 
 114 # handle `cio | ...`
 115 if [ -p /dev/stdout ]; then
 116     get_clip
 117     exit $?
 118 fi
 119 
 120 # handle `cio ...`
 121 if [ $# -gt 0 ]; then
 122     cat "$@" | set_clip
 123     exit $?
 124 fi
 125 
 126 # handle `cio`
 127 get_clip
 128 exit $?