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.
This commit is contained in:
Marty Oehme 2022-02-11 21:32:16 +01:00
parent ff78a1f32f
commit 94cd954df9
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 89 additions and 46 deletions

View file

@ -19,66 +19,66 @@ OXO_URL="https://0x0.st"
# use fd if available
if command -v fd >/dev/null 2>&1; then
sharefile_fd_cmd="fd"
sharefile_fd_cmd="fd"
else
sharefile_fd_cmd="find"
sharefile_fd_cmd="find"
fi
main() {
if [ $# -eq 0 ]; then
foldpick=$(picker d "")
exit_check $?
picked=$(picker f "$foldpick")
elif [ "$1" = "-" ]; then
while read -r file; do
picked="$file"
done <"/dev/stdin"
elif [ -f "$1" ]; then
picked="$1"
elif [ -d "$1" ]; then
picked=$(picker f "$1")
else
printf "Please only provide a folder or file as the optional argument to sharefile." >&2
exit 1
fi
exit_check $?
if [ $# -eq 0 ]; then
foldpick=$(picker d "")
exit_check $?
picked=$(picker f "$foldpick")
elif [ "$1" = "-" ]; then
while read -r file; do
picked="$file"
done <"/dev/stdin"
elif [ -f "$1" ]; then
picked="$1"
elif [ -d "$1" ]; then
picked=$(picker f "$1")
else
printf "Please only provide a folder or file as the optional argument to sharefile." >&2
exit 1
fi
exit_check $?
url=$(file_to_oxo "$picked")
echo "$url"
url_to_clipboard "$url"
if command -v notify-send >/dev/null 2>&1; then
notify-send "Upload finished" "URL: $url"
fi
exit
url=$(file_to_oxo "$picked")
echo "$url"
url_to_clipboard "$url"
if command -v notify-send >/dev/null 2>&1; then
notify-send "Upload finished" "URL: $url"
fi
exit
}
picker() {
if [ "$sharefile_fd_cmd" = "fd" ]; then
selected=$(fd "$SHAREFILE_FD_OPTS" --type "${1:-f}" . "${2:-$HOME}" | fzf)
elif [ "$sharefile_fd_cmd" = "find" ]; then
selected=$(find "$SHAREFILE_FD_OPTS" "${2:-$HOME}" -type "$1" | fzf)
fi
[ "$?" -eq 130 ] && exit 130
echo "$selected"
if [ "$sharefile_fd_cmd" = "fd" ]; then
selected=$(fd "$SHAREFILE_FD_OPTS" --type "${1:-f}" . "${2:-$HOME}" | fzf)
elif [ "$sharefile_fd_cmd" = "find" ]; then
selected=$(find "$SHAREFILE_FD_OPTS" "${2:-$HOME}" -type "$1" | fzf)
fi
[ "$?" -eq 130 ] && exit 130
echo "$selected"
}
url_to_clipboard() {
if command -v wl-copy >/dev/null 2>&1; then
printf "%s" "$@" | wl-copy
elif command -v xsel >/dev/null 2>&1; then
printf "%s" "$@" | xsel --clipboard
elif command -v xclip >/dev/null 2>&1; then
printf "%s" "$@" | xclip -selection clipboard >/dev/null 2>&1
fi
if command -v wl-copy >/dev/null 2>&1; then
printf "%s" "$@" | wl-copy
elif command -v xsel >/dev/null 2>&1; then
printf "%s" "$@" | xsel --clipboard
elif command -v xclip >/dev/null 2>&1; then
printf "%s" "$@" | xclip -selection clipboard >/dev/null 2>&1
fi
}
file_to_oxo() {
curl -F"file=@$1" "$OXO_URL"
curl -F"file=@$1" "$OXO_URL"
}
# exit on escape pressed
exit_check() {
[ "$1" -eq 130 ] && exit 130
[ "$1" -eq 130 ] && exit 130
}
main "$@"