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