#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # cio [options...] [filepath...] # # # Clipboard IO automatically figures out whether to copy/paste from/to the # clipboard, depending on the piping of its standard input/output. # # When both its standard input/output are piped, it first updates the text # clipboard, and then emits its new value to the standard output. # # When run unpiped on its own, it just emits the current clipboard value to # the standard output. # # The options are, available both in single and double-dash versions # # -c clear/empty the clipboard # -clear clear/empty the clipboard # -e clear/empty the clipboard # -empty clear/empty the clipboard # # -h show this help message # -help show this help message case "$1" in -h|--h|-help|--help) awk '/^# +cio /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac if [ -e /dev/clipboard ]; then get_clip='cat /dev/clipboard' set_clip='cat > /dev/clipboard' elif [ -e /usr/bin/wl-paste ]; then get_clip='wl-paste --no-newline' elif [ -e /usr/bin/wclip ]; then get_clip='wclip -o' set_clip='wclip' elif [ -e /usr/bin/xclip ]; then get_clip='xclip -o' set_clip='xclip -sel clip' elif [ -e /usr/bin/pbpaste ]; then get_clip='pbpaste' fi if [ -z "${set_clip}" ]; then if [ -e /usr/bin/wl-copy ]; then set_clip='wl-copy' elif [ -e /usr/bin/pbcopy ]; then set_clip='pbcopy' fi fi case "$1" in -c|--c|-clear|--clear|-e|--e|-empty|--empty) ${set_clip} < /dev/null return $? ;; esac [ "$1" = '--' ] && shift emit_clipboard() { if [ -z "${get_clip}" ]; then printf "can't read the clipboard\n" > /dev/stderr exit 1 fi ${get_clip} || exit $? } change_clipboard() { if [ -z "${set_clip}" ]; then printf "can't change the clipboard\n" > /dev/stderr exit 1 fi cat "$@" | ${set_clip} || exit $? } if [ $# -gt 0 ]; then # handle `cio ...` or `cio ... | ...` change_clipboard "$@" if [ -p /dev/stdout ]; then emit_clipboard fi exit 0 elif [ -p /dev/stdin ]; then # handle `... | cio` or `... | cio | ...` change_clipboard /dev/stdin if [ -p /dev/stdout ]; then emit_clipboard fi exit 0 else # handle `cio` or `cio | ...` emit_clipboard exit 0 fi