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

42
sh/.local/bin/clip Executable file
View file

@ -0,0 +1,42 @@
#!/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

View file

@ -225,16 +225,17 @@ nnoremap e :!nvim %f<cr>
nnoremap gb :file &<cr>l nnoremap gb :file &<cr>l
" yank current directory path into the clipboard " yank current directory path into the clipboard
nnoremap yd :!echo -n %d | xclip -selection "clipboard" %i<cr> " clip is universal clipper from `sh` module
nnoremap yd :!echo -n %d | clip %i<cr>
" yank current file path into the clipboard " yank current file path into the clipboard
nnoremap yf :!echo -n %c:p | xclip -selection "clipboard" %i<cr> nnoremap yf :!echo -n %c:p | clip %i<cr>
" yank current filename without path into the clipboard " yank current filename without path into the clipboard
nnoremap yt :!echo -n %c | xclip -selection "clipboard" %i<cr> nnoremap yt :!echo -n %c | clip %i<cr>
" yank root of current file's name into the clipboard " yank root of current file's name into the clipboard
nnoremap yr :!echo -n %c:r | xclip -selection "clipboard" %i<cr> nnoremap yr :!echo -n %c:r | clip %i<cr>
" Mappings for faster renaming " Mappings for faster renaming
nnoremap I cw<c-a> nnoremap I cw<c-a>