Blueno_Termux/myTermux/.local/bin/awesomeshot
2022-04-01 08:36:29 -04:00

433 lines
9.6 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Awesomeshot: A command-line screenshot tool written in bash 5.1.16+.
# https://github.com/mayTermux/awesomeshot
#
# Copyright (c) 2021 - 2022 xShin
version=1.0.1
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-${HOME}/.config}
config_file="${XDG_CONFIG_HOME}/awesomeshot/awesomeshot.conf"
LC_ALL=C
LANG=C
# Color
COLOR_DEFAULT="\e[39m"
COLOR_DANGER="\e[91m"
COLOR_WARNING="\e[93m"
COLOR_SUCCESS="\e[92m"
COLOR_SKY="\e[34m"
# Awesomeshot default config
read -rd '' config <<'EOF'
# Make sure when your phone screenshot the result file image
# PATH is same with this variable. Default my phone result file
# image PATH on "/sdcard/Pictures/Screenshots". If not same,
# you can edit this variable value
screenshot_result_path="/sdcard/Pictures/Screenshots"
# Change this value to "" or whatever for disable function
backup="yes"
# PATH backup original photo or screenshot
path_backup="${HOME}/.config/awesomeshot/backup"
# Array Hex Color
hex_color=(
"#F8F9FA" # white
"#000000" # black
"#333333" # gray
"#A8D7FE" # blue sky
)
# Change this value to "" or whatever for disable convert
convert_rounded="yes"
# Border radius
border_radius=10
# Change this value to "" or whatever for disable convert
convert_border="yes"
# Change this value to "none" for transparent background
border_color="${hex_color[0]}"
border_size=15
# Shadow
# Change this value to "" or whatever for disable convert
convert_shadow="yes"
shadow_color="${hex_color[1]}"
shadow_size="75x30+0+30"
# Footer
# Change this value to "" or whatever for disable convert
convert_footer="yes"
footer_text=" Shot by Awesomeshot"
footer_position="South"
footer_xy="+0+45"
footer_font="JetBrains-Mono-Medium-Nerd-Font-Complete"
footer_font_size=25
footer_foreground="${hex_color[1]}"
footer_background="none"
# Convert to PNG if file not PNG
convert_to_png="yes"
# Open Image when convert done
open_image=""
EOF
# init file_name
file_name=""
function getUserConfig() {
if [ -f "${config_file}" ]; then
source "${config_file}"
elif [ ! -f "${config_file}" ]; then
mkdir -p "${XDG_CONFIG_HOME}/awesomeshot/"
printf '%s\n' "$config" > "$config_file"
source "${config_file}"
fi
}
function generateDefaultConfig() {
if [ ! -f "${config_file}" ]; then
if [ ! -d "~/.config/awesomeshot" ]; then
mkdir ~/.config/awesomeshot
fi
printf '%s\n' "$config" > "$config_file"
stat "SUCCESS" "Success" "Default config has been generated"
exit
fi
if [ -f "${config_file}" ]; then
read -p " Config '~/.config/awesomeshot/awesomeshot.conf' already exists, do you want to generate default config [y/N] " ask
case "${ask}" in
y|Y )
printf '%s\n' "$config" > "$config_file"
stat "SUCCESS" "Success" "Default config has been re-generated"
;;
n|N )
exit
;;
esac
fi
}
function header() {
printf "\n\n %-18s\n\n" "AWESOMESHOT v${version}"
}
function title() {
printf "\n\b\r\033[2K***${1}\n" | sed 's/*/ /g'
}
function subtitle() {
printf "%-45s" "*******${1}" | sed 's/ /./g' | sed 's/*/ /g'
}
function check() {
#  
#   
if [[ $? -eq 0 && ${PIPESTATUS[0]} -eq 0 ]]; then
echo -e " ${COLOR_SUCCESS}${COLOR_DEFAULT} "
else
echo -e " ${COLOR_DANGER}${COLOR_DEFAULT} "
fi
}
function autoRun() {
getUserConfig
count=1
header
title "${COLOR_SKY}[?] WAITING TO TAKE SCREENSHOT${COLOR_DEFAULT} [${COLOR_WARNING}${count}${COLOR_DEFAULT}]"
inotifywait -m -e create ${screenshot_result_path} 2> /dev/null | \
while read get_file_name_result; do
get_file_name=$(echo -e "${get_file_name_result}" | awk '{print $3}')
if [[ "${screenshot_result_path}/${get_file_name}" != "${file_name}" || -z ${file_name} ]]; then
subtitle "[+]*Execute*Program*"
file_name="${screenshot_result_path}/${get_file_name}"
echo -e ""
if [ "${convert_to_png}" == "yes" ]; then
convertToPng "${get_file_name}"
fi
if [ "${backup}" == "yes" ]; then
title "${COLOR_SKY}[+] BACKUP${COLOR_DEFAULT}"
backupOriginalPhoto "${get_file_name}"
fi
title "${COLOR_SKY}[+] EDTING IMAGE${COLOR_DEFAULT}"
if [ "${convert_rounded}" == "yes" ]; then
convertRounded
fi
if [ "${convert_shadow}" == "yes" ]; then
convertShadow
fi
if [ "${convert_border}" == "yes" ]; then
convertBorder
fi
if [ "${convert_footer}" == "yes" ]; then
convertFooter
fi
termux-media-scan "${file_name}" &> /dev/null
termux-toast -b "${hex_color[3]}" -c "${hex_color[1]}" -g middle "${file_name} done"
if [ "${open_image}" == "yes" ]; then
termux-open "${file_name}"
fi
count=$(( ${count} + 1 ))
title "${COLOR_SKY}[?] WAITING TO TAKE SCREENSHOT${COLOR_DEFAULT} [${COLOR_WARNING}${count}${COLOR_DEFAULT}]"
fi
done
}
function manualRun() {
getUserConfig
header
title "${COLOR_SKY}[?] CHECKING FILE${COLOR_DEFAULT}"
subtitle "[+]*Set*Status*File*"
if [ -f ${file_name} ]; then
check
if [ "${convert_to_png}" == "yes" ]; then
convertToPng "${get_file_name}"
fi
if [ "${backup}" == "yes" ]; then
title "${COLOR_SKY}[+] BACKUP${COLOR_DEFAULT}"
backupOriginalPhoto "${get_file_name}"
fi
title "${COLOR_SKY}[+] EDTING IMAGE${COLOR_DEFAULT}"
if [ "${convert_rounded}" == "yes" ]; then
convertRounded
fi
if [ "${convert_shadow}" == "yes" ]; then
convertShadow
fi
if [ "${convert_border}" == "yes" ]; then
convertBorder
fi
if [ "${convert_footer}" == "yes" ]; then
convertFooter
fi
termux-media-scan "${file_name}" &> /dev/null
setterm -cursor on
if [ "${open_image}" == "yes" ]; then
termux-open "${file_name}"
fi
else
echo -e "\n"
stat "ERROR" "Danger" "${COLOR_DANGER}${file_name}${COLOR_DEFAULT}: file not found"
fi
}
function backupOriginalPhoto() {
change_filename_backup=$(echo $1 | sed 's/.png/_backup.png/g')
if [ ! -d ${path_backup} ]; then
mkdir -p "${path_backup}"
fi
cp "${file_name}" "${path_backup}/${change_filename_backup}"
if [ -f ${path_backup}/${change_filename_backup} ]; then
subtitle "[+]*Backup*File*"
check
else
subtitle "[+]*Backup*File*"
check
fi
}
function convertToPng() {
title "${COLOR_SKY}[+] CONVERTING EXTENSION IMAGE${COLOR_DEFAULT}"
extension="${file_name##*.}"
if [ "${extension}" != "png" ]; then
subtitle "[+]*Convert*.${extension}*To*.png*"
convert "${file_name}" "${file_name%.*}.png"
rm "${file_name}"
termux-media-scan "${file_name}" &> /dev/null
file_name=${file_name%.*}.png
check
fi
}
function convertRounded() {
subtitle "[+]*Set*Rounded*Corner*"
convert "$file_name" \
\( +clone -alpha extract -draw \
"fill black polygon 0,0,0,${border_radius} ${border_radius},0 fill white circle ${border_radius},${border_radius},${border_radius},0" \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) \
-alpha off -compose CopyOpacity -composite "$file_name"
check
}
function convertShadow() {
subtitle "[+]*Set*Image*Shadow*"
convert "$file_name" \
\( \
+clone \
-background "${shadow_color}" \
-shadow "${shadow_size}" \
\) \
+swap \
-background none \
-layers merge \
+repage "$file_name"
check
}
function convertBorder() {
subtitle "[+]*Set*Image*Border*Color*"
convert "$file_name" -bordercolor "${border_color}" -border ${border_size} "$file_name"
check
}
function convertFooter() {
subtitle "[+]*Set*Image*Footer"
echo -n "${footer_text}" |
convert "$file_name" \
-gravity ${footer_position} \
-pointsize ${footer_font_size} \
-fill ${footer_foreground} \
-undercolor ${footer_background} \
-font ${footer_font} \
-annotate ${footer_xy} \
@- "$file_name"
check
}
function handleInterruptByUser() {
pkill com.termux
echo -e "
\n [ ${COLOR_WARNING}ERROR${COLOR_DEFAULT} ] > ${COLOR_DANGER}${1}${COLOR_DEFAULT}\n"
setterm -cursor on
read -n 1 -s -r -p " Press any key to exit "
exit 1
}
function stat() {
if [ "${2}" == "Success" ]; then
echo -e " [ ${COLOR_SUCCESS}${1}${COLOR_DEFAULT} ] > ${3}"
elif [ "${2}" == "Warning" ]; then
echo -e " [ ${COLOR_WARNING}${1}${COLOR_DEFAULT} ] > ${3}"
elif [ "${2}" == "Danger" ]; then
echo -e " [ ${COLOR_DANGER}${1}${COLOR_DEFAULT} ] > ${3}"
fi
}
function main() {
trap "handleInterruptByUser 'Interrupt By User'" 2
setterm -cursor off
${1} ${2}
}
function help() {
echo -e "
$(version)
A command-line screenshot tool written in bash 5.1.16+
Usage:
awesomeshot [args] [file]
META OPTIONS:
-h, --help Show list of command-line options
-v, --version Show version of awesomeshot
RUN OPTIONS:
-a, --auto Run awesomeshot with automatic while take screenshot
-m, --manual Run awesomeshot with manual (this option require filename)
-c, --config Generate default config awesomeshot.conf
"
}
function version() {
echo -e "awesomeshot v.${version}"
}
case "${1}" in
-a|--auto )
main autoRun
;;
-c|--config )
generateDefaultConfig
;;
-m|--manual )
if [ ${2} ]; then
file_name="${2}"
main manualRun
else
echo ""
stat "ERROR" "Danger" "This option require filename"
help
exit 1
fi
;;
-h|--help )
help
;;
-v|--version )
version
;;
*)
help
;;
esac