Create a debug_message function for readability

This commit is contained in:
Jeremy Gardais 2020-03-12 07:19:43 +01:00
parent e6a1c8c62a
commit db6efb1530
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 14 additions and 6 deletions

View File

@ -9,7 +9,7 @@
## Or put it in the clipboard if no argument was given
# Vars [[[
debug="0"
debug=true
flag_clipboard=false
flag_inline=false
@ -23,15 +23,23 @@ c_reset='\033[0m'
# ]]]
# Function to print a debug message [[[
debug_message() {
_message="${1}"
[ "${debug}" = "true" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG ${_message}"
}
# ]]]
# Verify argument [[[
case "$#" in
0 )
flag_clipboard=true
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG Verify arg: No argument was given, try to use clipboard."
debug_message " Verify arg: No argument was given, try to use clipboard."
;;
1 )
flag_inline=true
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG Verify arg: One argument was given, use it."
debug_message " Verify arg: One argument was given, use it."
;;
* )
printf "${c_redb}%b${c_reset}\n" "Error: Expect one argument or a content in clipboard."
@ -50,7 +58,7 @@ if [ "${flag_clipboard}" = "true" ]; then
url_to_short=$(xclip -out -selection clipboard)
fi
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG Get URL: URL to be shortened: ${url_to_short}"
debug_message " Get URL: URL to be shortened: ${url_to_short}"
# ]]]
# Ensure the URL wasn't already shortened [[[
if printf -- '%s' "${url_to_short}" | grep -q -E -- "${null_service_url}"
@ -66,12 +74,12 @@ result=$(curl -sF"shorten=${url_to_short}" "${null_service_url}")
# Manage the result [[[
## If the URL should simply be printed to stdout
if [ "${flag_inline}" = "true" ]; then
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG Manage result: Print the result on stdout:"
debug_message " Manage result: Print the result on stdout:"
printf "%b\n" "${result}"
fi
# If the URL should remplace the previous content of the clipboard
if [ "${flag_clipboard}" = "true" ]; then
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG Manage result: Put the result in clipboard."
debug_message " Manage result: Put the result in clipboard."
echo "${result}" | xclip -rmlastnl -selection clipboard
fi