1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 04:49:03 -08:00
rc/sh/sc
2021-08-02 14:09:27 -07:00

67 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
sc_shorten() {
REPLY="${1:2:2}${1:5:2}${1:8:2}${1:11:12}"
}
# http://stackoverflow.com/a/10797966
uri_encode() {
REPLY="$(echo -En "$@" | curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "")"
REPLY="${REPLY:2}"
}
copy_scr() {
local fn="$1"
local short="$(basename $fn)"
local r='20\d\d-\d\d-\d\d_\d\d\d\d\d\d\d\d.(png|jpg)'
grep -Pq "$r" <<<"$short" || return 1
eaput "$fn" "$short"
sc_shorten "$short"
uri_encode "$REPLY"
REPLY="$_REMOTE_DOMAIN/s/$REPLY"
return 0
}
copy_tmp() {
local fn="$1"
local short="$(basename "$fn")"
eaput "$fn" "$short"
uri_encode "$short"
REPLY="$_REMOTE_DOMAIN/t/$REPLY"
return 0
}
sc() { ### @-
### upload given files to a webserver and return a direct link for sharing them.
### you'll want to tweak this if you use it yourself.
### this contains some extra logic for screenshots created by `scropt`.
###
### **TODO:** consider renaming because sc(1) already exists.
if [ -n "${ZSH_VERSION:-}" ]; then
# syntax is too different to bother tbh
echo "please run with bash"
return 1
fi
which xsel &>/dev/null || {
echo "please install xsel"
return 1
}
local clipboard=""
for f; do
f="$(readlink -f "$f")"
if [ ! -e "$f" ]; then
continue
fi
copy_scr "$f" || copy_tmp "$f"
clipboard="$clipboard"$'\n'"$REPLY"
done
xsel -b <<<"${clipboard:1}"
return 0
}
[ -n "${preload+-}" ] || . ~/sh/preload || exit 2
eval ${preload:-preload} ea
[ -n "${preload+-}" ] || sc "$@"