1
0
mirror of https://tildegit.org/tomasino/pb.git synced 2024-11-20 03:25:54 +01:00

condensed the pretty printing / color logic

This commit is contained in:
James Tomasino 2020-01-20 11:43:21 +00:00
parent 1e3772a514
commit 8ae82d6255
2 changed files with 21 additions and 62 deletions

81
pb
View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# init variables # init variables
version="v.2018.10.09" version="v.2020.01.20"
ENDPOINT="https://ttm.sh" ENDPOINT="https://ttm.sh"
flag_options="hvcufs::x" flag_options="hvcufs::x"
flag_version=0 flag_version=0
@ -12,11 +12,6 @@ flag_shortlist=0
flag_colors=0 flag_colors=0
data="" data=""
# Colors
SUCCESS=$(tput setaf 190)
ERROR=$(tput setaf 196)
RESET=$(tput sgr0)
# help message available via func # help message available via func
show_help() { show_help() {
cat > /dev/stdout << END cat > /dev/stdout << END
@ -132,6 +127,18 @@ if [ ${flag_shortlist} -gt 0 ]; then
die "${out} ${lsresults}" 0 die "${out} ${lsresults}" 0
fi fi
# Colors
if [ ${flag_colors} -gt 0 ]; then
SUCCESS=$(tput setaf 190)
ERROR=$(tput setaf 196)
RESET=$(tput sgr0)
else
SUCCESS=""
ERROR=""
RESET=""
fi
# URL shortening reference # URL shortening reference
# If URL mode detected, process URL shortener and end processing without # If URL mode detected, process URL shortener and end processing without
@ -140,15 +147,9 @@ if [ ${flag_url} -gt 0 ]; then
if [ -z "${data}" ]; then if [ -z "${data}" ]; then
# if no data # if no data
# print error message # print error message
if [ ${flag_colors} -gt 0 ]; then printf "%sProvide URL to shorten%s\\n" "$ERROR" "$RESET"
printf "%sProvide URL to shorten%s\\n" "$ERROR" "$RESET"
else
printf "Provide URL to shorten\\n"
fi
else else
# shorten URL and print results # shorten URL and print results
curl -F"shorten=${data}" "${ENDPOINT}" curl -F"shorten=${data}" "${ENDPOINT}"
fi fi
@ -157,93 +158,51 @@ fi
if [ ${flag_file} -gt 0 ]; then if [ ${flag_file} -gt 0 ]; then
# file mode # file mode
if [ -z "${data}" ]; then if [ -z "${data}" ]; then
# if no data # if no data
# print error message # print error message
if [ ${flag_colors} -gt 0 ]; then printf "%sProvide data to upload%s\\n" "$ERROR" "$RESET"
printf "%sProvide data to upload%s\\n" "$ERROR" "$RESET"
else
printf "Provide data to upload\\n"
fi
elif [ ! -f "${data}" ]; then elif [ ! -f "${data}" ]; then
# file not found with name provided # file not found with name provided
# print error messagse # print error messagse
if [ ${flag_colors} -gt 0 ]; then printf "%s%s%s\\tFile not found.%s\\n" "$RESET" "${data}" "$ERROR" "$RESET"
printf "%s%s%s\\tFile not found.%s\\n" "$RESET" "${data}" "$ERROR" "$RESET"
else
printf "%s\\tFile not found.\\n" "${data}"
fi
# attempt to split data string (multi-line?) and upload each string as file # attempt to split data string (multi-line?) and upload each string as file
for f in ${data}; do for f in ${data}; do
# if there's nothing to parse, skip this loop # if there's nothing to parse, skip this loop
if [ "$f" = "$data" ]; then if [ "$f" = "$data" ]; then
break; break;
fi fi
# print name of file parsed, but not yet status of success or failure # print name of file parsed, but not yet status of success or failure
if [ ${flag_colors} -gt 0 ]; then if [ ${flag_colors} -gt 0 ]; then
printf "%s%s\\t%s" "$RESET" "${f}" "$SUCCESS" printf "%s%s\\t%s" "$RESET" "${f}" "$SUCCESS"
fi fi
# check if file exists # check if file exists
if [ -f "${f}" ]; then if [ -f "${f}" ]; then
# send file to endpoint # send file to endpoint
curl -F"file=@${f}" "${ENDPOINT}" curl -F"file=@${f}" "${ENDPOINT}"
# print result short url # print result short url
if [ ${flag_colors} -gt 0 ]; then if [ ${flag_colors} -gt 0 ]; then
printf "%s" "$RESET" printf "%s" "$RESET"
fi fi
else else
# print error message # print error message
if [ ${flag_colors} -gt 0 ]; then printf "%sFile not found.%s\\n" "$ERROR" "$RESET"
printf "%sFile not found.%s\\n" "$ERROR" "$RESET"
else
printf "File not found.\\n"
fi
fi fi
done done
else else
# data available in file # data available in file
# send file to endpoint # send file to endpoint
if [ ${flag_colors} -gt 0 ]; then curl -F"file=@${data}" "${ENDPOINT}"
printf "%s${data}\\t%s" "$RESET" "$SUCCESS"
curl -F"file=@${data}" "${ENDPOINT}"
printf "%s" "$RESET"
else
curl -F"file=@${data}" "${ENDPOINT}"
fi
fi fi
else else
# non-file mode # non-file mode
if [ -z "${data}" ]; then if [ -z "${data}" ]; then
# if no data # if no data
# print error message # print error message
if [ ${flag_colors} -gt 0 ]; then printf "%sNo data found for upload. Please try again.%s\\n" "$ERROR" "$RESET"
printf "%sNo data found for upload. Please try again.%s\\n" "$ERROR" "$RESET"
else
printf "No data found for upload. Please try again.\\n"
fi
else else
# data available # data available
# send data to endpoint, print short url # send data to endpoint, print short url
if [ ${flag_colors} -gt 0 ]; then printf "%s" "${data}" | curl -F"file=@-;filename=null.txt" "${ENDPOINT}"
printf "%s" "$SUCCESS"
printf "%s" "${data}" | curl -F"file=@-;filename=null.txt" "${ENDPOINT}"
printf "%s" "$RESET"
else
printf "%s" "${data}" | curl -F"file=@-;filename=null.txt" "${ENDPOINT}"
fi
fi fi
fi fi

2
pb.1
View File

@ -1,4 +1,4 @@
.TH PB 1 "16 August 2018" "v2018.08.16" .TH PB 1 "20 January 2020" "v2020.01.20"
.SH NAME .SH NAME
pb \- a helper utility for using 0x0 pastebin services pb \- a helper utility for using 0x0 pastebin services