Improved Readability

Added section headers and made some rearrangements to improve readability.
Removed discouraged '$?' usage.
Added install_program function.
This commit is contained in:
The-Plottwist 2022-01-15 16:13:15 +03:00
parent f1e03b4949
commit 2824010b86

View File

@ -1,7 +1,33 @@
#! /usr/bin/env bash
# Grub2 Themes
set -o errexit
# Exit Immediately if a command fails
set -o errexit
#
# ──────────────────────────────────────────────────── I ──────────
# :::::: C O L O R S : : : : : : : :
# ──────────────────────────────────────────────────────────────
#
CDEF=" \033[0m" # default color
CCIN=" \033[0;36m" # info color
CGSC=" \033[0;32m" # success color
CRER=" \033[0;31m" # error color
CWAR=" \033[0;33m" # waring color
b_CDEF=" \033[1;37m" # bold default color
b_CCIN=" \033[1;36m" # bold info color
b_CGSC=" \033[1;32m" # bold success color
b_CRER=" \033[1;31m" # bold error color
b_CWAR=" \033[1;33m" # bold warning color
#
# ────────────────────────────────────────────────────── II ──────────
# :::::: G L O B A L S : : : : : : : :
# ────────────────────────────────────────────────────────────────
#
[ GLOBAL::CONF ]
{
@ -18,17 +44,12 @@ THEME_VARIANTS=('tela' 'vimix' 'stylish' 'whitesur')
ICON_VARIANTS=('color' 'white' 'whitesur')
SCREEN_VARIANTS=('1080p' '2k' '4k' 'ultrawide' 'ultrawide2k')
#COLORS
CDEF=" \033[0m" # default color
CCIN=" \033[0;36m" # info color
CGSC=" \033[0;32m" # success color
CRER=" \033[0;31m" # error color
CWAR=" \033[0;33m" # waring color
b_CDEF=" \033[1;37m" # bold default color
b_CCIN=" \033[1;36m" # bold info color
b_CGSC=" \033[1;32m" # bold success color
b_CRER=" \033[1;31m" # bold error color
b_CWAR=" \033[1;33m" # bold warning color
#
# ────────────────────────────────────────────────────────── III ──────────
# :::::: F U N C T I O N S : : : : : : : :
# ────────────────────────────────────────────────────────────────────
#
# echo like ... with flag type and display message colors
prompt () {
@ -49,7 +70,7 @@ prompt () {
# Check command availability
function has_command() {
command -v $1 > /dev/null
command -v $1 &> /dev/null #with "&>", all output will be redirected.
}
usage() {
@ -72,7 +93,7 @@ install() {
if [[ "$UID" -eq "$ROOT_UID" ]]; then
clear
# Create themes directory if it didn't exist
# Make a themes directory if it doesn't exist
prompt -s "\n Checking for the existence of themes directory..."
[[ -d "${THEME_DIR}/${theme}" ]] && rm -rf "${THEME_DIR}/${theme}"
@ -187,46 +208,45 @@ install() {
updating_grub
prompt -w "\n * At the next restart of your computer you will see your new Grub theme: '$theme' "
#Check if password is cached (if cache timestamp has not expired yet)
elif sudo -n true 2> /dev/null && echo; then #No need for "$?" ==> https://github.com/koalaman/shellcheck/wiki/SC2181
sudo "$0" -t ${theme} -i ${icon} -s ${screen}
else
#Check if password is cached (if cache timestamp not expired yet)
sudo -n true 2> /dev/null && echo
if [[ $? == 0 ]]; then
#No need to ask for password
sudo "$0" -t ${theme} -i ${icon} -s ${screen}
#Ask for password
if [[ -n ${tui_root_login} ]] ; then
if [[ -n "${theme}" && -n "${screen}" ]]; then
sudo -S $0 -t ${theme} -i ${icon} -s ${screen} <<< ${tui_root_login}
fi
else
#Ask for password
if [[ -n ${tui_root_login} ]] ; then
if [[ -n "${theme}" && -n "${screen}" ]]; then
sudo -S $0 -t ${theme} -i ${icon} -s ${screen} <<< ${tui_root_login}
fi
prompt -e "\n [ Error! ] -> Run me as root! "
read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s
if sudo -S echo <<< $REPLY 2> /dev/null && echo; then
#Correct password, use with sudo's stdin
sudo -S "$0" -t ${theme} -i ${icon} -s ${screen} <<< ${REPLY}
else
prompt -e "\n [ Error! ] -> Run me as root! "
read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s
sudo -S echo <<< $REPLY 2> /dev/null && echo
if [[ $? == 0 ]]; then
#Correct password, use with sudo's stdin
sudo -S "$0" -t ${theme} -i ${icon} -s ${screen} <<< ${REPLY}
else
#block for 3 seconds before allowing another attempt
sleep 3
prompt -e "\n [ Error! ] -> Incorrect password!\n"
exit 1
fi
#block for 3 seconds before allowing another attempt
sleep 3
prompt -e "\n [ Error! ] -> Incorrect password!\n"
exit 1
fi
fi
fi
fi
}
run_dialog() {
if [[ -x /usr/bin/dialog ]]; then
if [[ "$UID" -ne "$ROOT_UID" ]]; then
#Check if password is cached (if cache timestamp not expired yet)
sudo -n true 2> /dev/null && echo
if [[ $? == 0 ]]; then
if sudo -n true 2> /dev/null && echo; then
#No need to ask for password
sudo $0
else
@ -237,9 +257,7 @@ run_dialog() {
--passwordbox "require root permission" 8 50 \
--output-fd 1 )
sudo -S echo <<< $tui_root_login 2> /dev/null && echo
if [[ $? == 0 ]]; then
if sudo -S echo <<< $tui_root_login 2> /dev/null && echo; then
#Correct password, use with sudo's stdin
sudo -S "$0" <<< $tui_root_login
else
@ -324,20 +342,30 @@ updating_grub() {
prompt -s "\n * All done!"
}
function install_program () {
if has_command zypper; then
zypper in "$@"
elif has_command apt-get; then
apt-get install "$@"
elif has_command dnf; then
dnf install -y "$@"
elif has_command yum; then
yum install "$@"
elif has_command pacman; then
pacman -S --noconfirm "$@"
fi
}
install_dialog() {
if [ ! "$(which dialog 2> /dev/null)" ]; then
prompt -w "\n 'dialog' need to be installed for this shell"
if has_command zypper; then
sudo zypper in dialog
elif has_command apt-get; then
sudo apt-get install dialog
elif has_command dnf; then
sudo dnf install -y dialog
elif has_command yum; then
sudo yum install dialog
elif has_command pacman; then
sudo pacman -S --noconfirm dialog
fi
install_program "dialog"
fi
}
@ -374,9 +402,7 @@ remove() {
else
#Check if password is cached (if cache timestamp not expired yet)
sudo -n true 2> /dev/null && echo
if [[ $? == 0 ]]; then
if sudo -n true 2> /dev/null && echo; then
#No need to ask for password
sudo "$0" "${PROG_ARGS[@]}"
else
@ -384,9 +410,7 @@ remove() {
prompt -e "\n [ Error! ] -> Run me as root! "
read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s
sudo -S echo <<< $REPLY 2> /dev/null && echo
if [[ $? == 0 ]]; then
if sudo -S echo <<< $REPLY 2> /dev/null && echo; then
#Correct password, use with sudo's stdin
sudo -S "$0" "${PROG_ARGS[@]}" <<< $REPLY
else
@ -404,9 +428,8 @@ dialog_installer() {
if [[ ! -x /usr/bin/dialog ]]; then
if [[ $UID -ne $ROOT_UID ]]; then
#Check if password is cached (if cache timestamp not expired yet)
sudo -n true 2> /dev/null && echo
if [[ $? == 0 ]]; then
if sudo -n true 2> /dev/null && echo; then
#No need to ask for password
exec sudo $0
else
@ -414,9 +437,7 @@ dialog_installer() {
prompt -e "\n [ Error! ] -> Run me as root! "
read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s
sudo -S echo <<< $REPLY 2> /dev/null && echo
if [[ $? == 0 ]]; then
if sudo -S echo <<< $REPLY 2> /dev/null && echo; then
#Correct password, use with sudo's stdin
sudo $0 <<< $REPLY
else
@ -433,6 +454,13 @@ dialog_installer() {
install "${theme}" "${icon}" "${screen}"
}
#
# ────────────────────────────────────────────────────────────────────────── IV ──────────
# :::::: A R G U M E N T H A N D L I N G : : : : : : : :
# ────────────────────────────────────────────────────────────────────────────────────
#
while [[ $# -gt 0 ]]; do
PROG_ARGS+=("${1}")
dialog='false'
@ -465,7 +493,7 @@ while [[ $# -gt 0 ]]; do
themes+=("${THEME_VARIANTS[3]}")
shift
;;
-*|--*)
-*) # "-*" overrides "--*"
break
;;
*)
@ -492,7 +520,7 @@ while [[ $# -gt 0 ]]; do
icons+=("${ICON_VARIANTS[2]}")
shift
;;
-*|--*)
-*)
break
;;
*)
@ -527,7 +555,7 @@ while [[ $# -gt 0 ]]; do
screens+=("${SCREEN_VARIANTS[4]}")
shift
;;
-*|--*)
-*)
break
;;
*)
@ -550,6 +578,13 @@ while [[ $# -gt 0 ]]; do
esac
done
#
# ──────────────────────────────────────────────── V ──────────
# :::::: M A I N : : : : : : : :
# ──────────────────────────────────────────────────────────
#
# Show terminal user interface for better use
if [[ "${dialog:-}" == 'false' ]]; then
if [[ "${remove:-}" != 'true' ]]; then