text Untitled 4 views
#!/usr/bin/env bash

FILE_PATH="${1:-}"

# If no file path provided, use fzf to select one
if [[ -z "$FILE_PATH" ]]; then
    FILE_PATH=$(
        find . -maxdepth 1 -type f -printf '%f\n' |
        fzf \
            --prompt="   Select file to upload: " \
            --border \
            --reverse \
            --header="Use / to select, <Enter> to confirm" \
            --pointer="➤" \
            --color="bg+:#1e1e2e,bg:#181825,spinner:#f5c2e7,hl:#f38ba8,fg:#cdd6f4,header:#89b4fa,info:#f9e2af,pointer:#a6e3a1,marker:#f5c2e7,fg+:#cdd6f4,prompt:#89b4fa,hl+:#f38a8a" \
            --preview='bat --style=numbers --color=always {}'
    )

    if [[ -z "$FILE_PATH" ]]; then
        echo -e "\033[33mNo file selected.\033[0m"
        exit 1
    fi
fi

# Validate file
if [[ ! -f "$FILE_PATH" ]]; then
    echo -e "\033[31mFile path does not exist: $FILE_PATH\033[0m"
    exit 1
fi

# Upload
RESPONSE=$(curl -fsS \
    -X POST \
    -H "Content-Type: text/plain" \
    --data-binary @"$FILE_PATH" \
    "https://pste.us/documents") || {
        echo -e "\033[31mFailed to upload the document.\033[0m"
        exit 1
    }

HASTE_KEY=$(printf '%s' "$RESPONSE" | jq -r '.key')

if [[ -z "$HASTE_KEY" || "$HASTE_KEY" == "null" ]]; then
    echo -e "\033[31mFailed to get Hastebin key.\033[0m"
    exit 1
fi

URL="https://pste.us/$HASTE_KEY"

# Copy URL to clipboard
if command -v wl-copy &>/dev/null; then
    printf '%s' "$URL" | wl-copy
elif command -v xclip &>/dev/null; then
    printf '%s' "$URL" | xclip -selection clipboard
elif command -v xsel &>/dev/null; then
    printf '%s' "$URL" | xsel --clipboard --input
elif command -v pbcopy &>/dev/null; then
    printf '%s' "$URL" | pbcopy
else
    echo -e "\033[33mClipboard utility not found; URL was not copied.\033[0m"
fi

echo -e "\033[35m$(basename "$FILE_PATH")\033[0m"
echo -e "\033[36m  Hastebin URL:\033[0m \033[32m$URL\033[0m"
echo -e "\033[33m  (Copied to clipboard)\033[0m"