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 "$@"

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
" 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
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
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
nnoremap yr :!echo -n %c:r | xclip -selection "clipboard" %i<cr>
nnoremap yr :!echo -n %c:r | clip %i<cr>
" Mappings for faster renaming
nnoremap I cw<c-a>