diff options
Diffstat (limited to 'conf.d')
-rw-r--r-- | conf.d/99-pastebin.zsh | 117 |
1 files changed, 100 insertions, 17 deletions
diff --git a/conf.d/99-pastebin.zsh b/conf.d/99-pastebin.zsh index a10c918..7e64c8b 100644 --- a/conf.d/99-pastebin.zsh +++ b/conf.d/99-pastebin.zsh @@ -2,47 +2,130 @@ : ${PB_CURL_ARGS:=""} PASTE_LOG_DIR="$HOME/.local/pb/$PB_INSTANCE" +local LIVE="$PASTE_LOG_DIR/live" +local DEAD="$PASTE_LOG_DIR/dead" -# pb upload -function pbu() { +mkdir -p "$LIVE" "$DEAD" + +function pbupload() { local res if [ "$#" -eq 0 ]; then # paste from stdin - res=$(curl $PB_CURL_ARGS -sF c=@- "https://$PB_INSTANCE") + res=$(curl $PB_CURL_ARGS -sF c=@- "https://$PB_INSTANCE$PB_URL_PATH") else - res=$(curl $PB_CURL_ARGS -sF "c=@$1" "https://$PB_INSTANCE") + res=$(curl $PB_CURL_ARGS -sF "c=@$1" "https://$PB_INSTANCE$PB_URL_PATH") + fi + + local name=$(awk '/url/ { print $2 }' <<<"$res" | tr -d '\n' | sed 's@.*://.*/\(.*\)@\1@') + if [ -z "$name" ]; then + echo "$res" 1>&2 + exit 1 fi - local url=$(awk '/url/ { print $2 }' <<<"$res" | tr -d '\n') - local short=$(awk '/short/ { print $2 }' <<<"$res" | tr -d '\n') + if [ ! -e "$LIVE/$name" ]; then + echo "$res" > "$LIVE/$name" + fi + echo "$name" +} - mkdir -p "$PASTE_LOG_DIR" - echo "$res" | tee "$PASTE_LOG_DIR/$short" +local function pbexists() { + [ -e "$LIVE/$1" ] +} +local function pbget() { + local short="$1" + local attr="$2" + awk "/$attr/ { print \$2 }" < "$LIVE/$short" | tr -d '\n' +} + +local function pbclip() { + local name="$1" + local url=$(sed 's@.*://\(.*\)@\1@' <<<"$2") if [[ -v DISPLAY ]]; then xclip -i <<<"$url" - notify-send "pbu" "Copied to clipboard: $url" + notify-send "$name" "Copied to clipboard: $url" + fi +} + +# pb upload +function pbu() { + local short=$(pbupload "$@") + + local url=$(pbget "$short" "url") + pbclip "pbu" "$url" +} + +# pb change +function pbc() { + local short=$1 + local file=$2 + local uuid=$(pbget "$short" "uuid") + + if [ "$#" -eq 1 ]; then + # update from stdin + curl $PB_CURL_ARGS -X PUT -F c=@- "https://$PB_INSTANCE/$uuid" + else + curl $PB_CURL_ARGS -X PUT -F "c=@$file" "https://$PB_INSTANCE/$uuid" fi } +# pb list +function pbl() { + find "$LIVE" -type f -exec basename {} \; +} + # pb delete, takes short id # someone please fix this function pbd() { - local short=$(grep -C2 $1 $PASTE_LOG_FILE \ - | awk '/uuid/ { print $2 }') - curl $PB_CURL_ARGS -X DELETE https://$PB_INSTANCE/$short + local uuid=$(pbget "$1" "uuid") + curl $PB_CURL_ARGS -X DELETE "https://$PB_INSTANCE/$uuid" + mv "$LIVE/$1" "$DEAD/" } +# pb delete all +function pbdall() { + for p in $(pbl); pbd $p +} + +# pb shorten +function pbs() { + local name=$(PB_URL_PATH="/u" pbupload) + local url=$(pbget "$name" "url") + pbclip "pbs" "$url" +} + +# pb vanity +function pbv() { + local name="$1" + shift + + pbexists "~$1" && pbd "~$1" + local short=$(PB_URL_PATH="/~$1" pbupload "$@") + local url=$(pbget "$short" "url") + pbclip "pbv" "$url" +} + +# pb image function pbi() { - scrot --select --quality 0 /tmp/$$.png - pngcrush -ow /tmp/$$.png - pbu /tmp/$$.png + local tmp=$(mktemp '/tmp/XXXXXXXX') + scrot --select --quality 0 $tmp.png + pngcrush -ow $tmp.png + + # Remove .png suffix, for shorter URL + # Relies on sending the correct mimetype + local short=$(pbupload $tmp.png) + local url=$(pbget "$short" "url") + pbclip "pbi" "$(echo $url | sed 's@\(.*\)\.png@\1@')" } +# pb record function pbr() { - asciinema rec /tmp/$$.json - pbu /tmp/$$.json + local tmp=$(mktemp '/tmp/XXXXXXXX') + asciinema rec $tmp + + local short=$(pbupload $tmp) + pbclip "pbr" "$PB_INSTANCE/t/$short" } function termbin() { |