addet better logging and error display function
This commit is contained in:
parent
b4220d657a
commit
1af3a239f8
1 changed files with 63 additions and 5 deletions
68
.zshrc
68
.zshrc
|
@ -1,7 +1,64 @@
|
|||
# ─< Helper functions >─────────────────────────────────────────────────────────────────
|
||||
function echo_error() { echo -e "\033[0;1;31merror:\033[0;31m\t${*}\033[0m"; }
|
||||
function echo_binfo() { echo -e "\033[0;1;34mInfo:\033[0;34m\t${*}\033[0m"; }
|
||||
function echo_info() { echo -e "\033[0;35m${*}\033[0m"; }
|
||||
# ANSI color codes
|
||||
RED='\033[0;31m'
|
||||
CYAN='\033[0;36m'
|
||||
YELLOW='\033[0;33m'
|
||||
LIGHT_GREEN='\033[0;92m'
|
||||
BOLD='\033[1m'
|
||||
NC='\033[0m' # No Color
|
||||
_STORED_ERRORS=""
|
||||
_STORED_WARNINGS=""
|
||||
_STORED_INFOS=""
|
||||
|
||||
# Modified echo functions that both store and display messages
|
||||
function echo_error() {
|
||||
local message="${RED}$1${NC}\n"
|
||||
printf "$message" >&2
|
||||
_STORED_ERRORS="${_STORED_ERRORS}${message}"
|
||||
}
|
||||
|
||||
function echo_warning() {
|
||||
local message="${YELLOW}$1${NC}\n"
|
||||
printf "$message"
|
||||
_STORED_WARNINGS="${_STORED_WARNINGS}${message}"
|
||||
}
|
||||
|
||||
function echo_info() {
|
||||
local message="${CYAN}$1${NC}\n"
|
||||
printf "$message"
|
||||
_STORED_INFOS="${_STORED_INFOS}${message}"
|
||||
}
|
||||
|
||||
|
||||
# Function to display all stored messages
|
||||
function error_log() {
|
||||
local has_messages=0
|
||||
|
||||
# First check if we have any messages at all
|
||||
if [ -z "$_STORED_ERRORS" ] && [ -z "$_STORED_WARNINGS" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Only display sections that have content
|
||||
if [ -n "$_STORED_ERRORS" ]; then
|
||||
printf "\n${BOLD}${RED}=== Errors ===${NC}\n"
|
||||
printf "$_STORED_ERRORS"
|
||||
has_messages=1
|
||||
fi
|
||||
|
||||
if [ -n "$_STORED_WARNINGS" ]; then
|
||||
printf "\n${BOLD}${YELLOW}=== Warnings ===${NC}\n"
|
||||
printf "$_STORED_WARNINGS"
|
||||
has_messages=1
|
||||
fi
|
||||
|
||||
if [ -n "$_STORED_INFOS" ]; then
|
||||
[ "$has_messages" -eq 1 ] && printf "\n"
|
||||
printf "${BOLD}${CYAN}=== Info ===${NC}\n"
|
||||
printf "$_STORED_INFOS"
|
||||
has_messages=1
|
||||
fi
|
||||
}
|
||||
|
||||
# ─< Check if the given command exists silently >─────────────────────────────────────────
|
||||
command_exists() {
|
||||
|
@ -12,14 +69,14 @@ command_exists() {
|
|||
check_root() {
|
||||
if [[ "${EUID}" -ne 0 ]]; then
|
||||
if command_exists sudo; then
|
||||
echo_binfo "User is not root. Using sudo for privileged operations."
|
||||
echo_info "User <$(whoami)> is not root. Using sudo for privileged operations."
|
||||
_sudo="sudo"
|
||||
else
|
||||
echo_error "No sudo found and you're not root! Can't install packages."
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo_binfo "Root access confirmed."
|
||||
echo_info "Root access confirmed."
|
||||
_sudo=""
|
||||
fi
|
||||
}
|
||||
|
@ -554,5 +611,6 @@ main(){
|
|||
}
|
||||
if check_root; then
|
||||
main
|
||||
error_log
|
||||
fi
|
||||
# echo "$bat"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue