1
0
mirror of https://github.com/eoli3n/dotfiles synced 2024-11-22 23:12:32 +01:00

updated up

This commit is contained in:
eoli3n 2020-11-15 20:50:39 +01:00
parent 9ac86921cf
commit de3f43bf4d

@ -13,18 +13,21 @@
# Vars
autocopy=1
AUTOCOPY=1
# Functions
function usage(){
echo -e "\nBash file uploader\n"
echo -e "Usage :"
echo -e "\t$0 [OPTIONS] file"
echo -e "\tcat file.ext | $0 [OPTIONS]"
echo -e "Options :"
echo -e "\t-h\t Print this usage"
echo -e "\t-a\t Disable autocopy in clipboard"
filename=$(basename "$0")
cat <<- EOF
Bash file uploader
Usage :
"$filename" [OPTIONS] file
cat file.ext | "$filename" [OPTIONS]
Options :
-h Print this usage
-a Disable autocopy in clipboard
EOF
}
function x0(){
@ -48,71 +51,97 @@ function ix(){
# Arguments
while getopts "ha" arg
do
case $arg in
a)
autocopy=0
;;
h)
usage
exit
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
function arguments(){
while getopts "ha" arg
do
case $arg in
a)
AUTOCOPY=0
;;
h)
usage
exit
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
}
# Main
# Test if first argument is an existing file
if [ -n "$1" ] && [ ! -f "$1" ]
then
echo "File \"$1\" not found"
exit 1
fi
# If no piped data
if [ -t 0 ]
then
# If no argument, read stdin and save url
if [ -z "$1" ]
then
echo -e "Enter text, then upload with twice CTRL+d:\n---"
url=$(x0)
echo -e "---"
# Else upload argument to x0.at and save url
else
url=$(x0 "$1")
fi
# Else if piped data
else
# Upload to ix.io and save url
url=$(ix)
fi
# Print url
echo "$url"
# If autocopy is set
if [ "$autocopy" == "1" ]
then
function autocopy(){
# If xorg or wayland is running
if env | grep 'DISPLAY' &> /dev/null
if grep 'DISPLAY' <(env) &> /dev/null
then
# If xclip is installed
if which xclip &> /dev/null
then
xclip "$url"
xclip "$1"
# Else if wl-clipboard is installed
elif which wl-copy &> /dev/null
then
wl-copy "$url"
wl-copy "$1"
fi
fi
fi
}
function is_file(){
# Test if first argument is an existing file
if [ ! -f "$1" ]
then
echo "File \"$1\" not found"
exit 1
fi
}
function main(){
# Vars
local url
# Getopts
arguments
# Tests
if [ -n "$1" ]
then
is_file "$1"
fi
# If no piped data
if [ -t 0 ]
then
# If no argument, read stdin and save url
if [ -z "$1" ]
then
echo -e "Enter text, then upload with twice CTRL+d:\n---"
url=$(x0)
echo -e "---"
# Else upload argument to x0.at and save url
else
url=$(x0 "$1")
fi
# Else if piped data
else
# Upload to ix.io and save url
url=$(ix)
fi
# Print url
echo "$url"
# If autocopy is set
if [ "$AUTOCOPY" == "1" ]
then
# Autocopy url
autocopy "$url"
fi
}
# Main
main "$1"