dotfiles/sh/.local/bin/clip
Marty Oehme 94cd954df9
sh: Reintegrate clip script
Brought back an old universal clipping script and updated it to work
better - well, at all. Can now decide between wl-copy, xclip and xsel
and will do so in that order.
Can take clipping material from the following arguments (will clip any
and all following arguments) or from stdin. Stdin has precedence.
Not much more to say really, but makes writing other applications a bit
more universal when they rely on this universal little tool.
2022-02-11 21:32:16 +01:00

42 lines
917 B
Bash
Executable file

#!/usr/bin/env sh
# clip -- easy copying to clipboard manager with
# wl-copy / xclip / xsel
#
# clips the first argument to the clipboard
# or stdin if stdin is passed
# will copy png/jpg as image files
#
# idea ~~stolen~~ creatively borrowed from
# https://github.com/kyazdani42/dotfiles/blob/master/bin/copy
clip() {
if exist wl-copy; then
printf "%s" "$1" | wl-copy
elif exist xclip; then
echo "$1" | xclip -i -selection clipboard
elif exist xsel; then
echo "$1" | xsel -i --clipboard
else
printf "No working clipboard program found. Install wl-copy/xclip/xsel and try again."
exit 1
fi
}
# if we are in a pipe, read from stdin
if [ ! -t 0 ]; then
clip "$(cat /dev/stdin)"
exit 0
fi
# TODO check if $1 is a file, and if it's png or similar, clip that
if [ $# -lt 1 ]; then
printf "No file argument or stdin passed to clip. Exiting."
exit 1
fi
if [ "$#" -ge 1 ]; then
clip "$*"
exit 0
fi