Skip to content

Commit

Permalink
Update installUpgradeFunctions.sh: Move color-removal code to remove_…
Browse files Browse the repository at this point in the history
…colors()

This allows other scripts to call remove_colors().
  • Loading branch information
EricClaeys authored Feb 5, 2025
1 parent a993adc commit 206d091
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions scripts/installUpgradeFunctions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,43 @@ function get_variable()
}


#####
# Strip out all color escape sequences.
# The message may have an actual escape character or may have the
# four characters "\033" which represent an escape character.

# I don't know how to replace "\n" with an actual newline in sed,
# and there HAS to be a better way to strip the escape sequences.
# I simply replace actual escape characters in the input with "033" then
# replace "033[" with "033X".
# Feel free to improve...
function remove_colors()
{
local MSG="${1}"

local ESC="$( echo -en '\033' )"

# Ignore any initial "\" in the colors.
# In case a variable isn't defined, set it to a string that won't be found.
local G="${GREEN/\\/}" ; G="${G/033\[/033X}"
local Y="${YELLOW/\\/}" ; Y="${Y:-abcxyz}" ; Y="${Y/033\[/033X}"
local R="${RED/\\/}" ; R="${R:-abcxyz}" ; R="${R/033\[/033X}"
local D="${cDEBUG/\\/}" ; D="${D:-abcxyz}" ; D="${D/033\[/033X}"
local N="${NC/\\/}" ; N="${N:-abcxyz}" ; N="${N/033\[/033X}"

# Outer "echo -e" handles "\n" (2 characters) in input.
# No "-e" needed on inner "echo".
echo -e "$( echo "${MSG}" |
sed -e "s/${ESC}/033/g" \
-e "s/033\[/033X/g" \
-e "s/${G}//g" \
-e "s/${Y}//g" \
-e "s/${R}//g" \
-e "s/${D}//g" \
-e "s/${N}//g" \
)"
}

#####
# Display a message of various types in appropriate colors.
# Used primarily in installation scripts.
Expand Down Expand Up @@ -234,41 +271,12 @@ function display_msg()
touch "${DISPLAY_MSG_LOG}"
fi

# Strip out all color escape sequences before adding to log file.
# The message may have an actual escape character or may have the
# four characters "\033" which represent an escape character.

# I don't know how to replace "\n" with an
# actual newline in sed, and there HAS to be a better way to strip the
# escape sequences.
# I simply replace actual escape characters in the input with "033" then
# replace "033[" with "033X".
# Feel free to improve...

# Assume if GREEN isn't defined then no colors are defined.
local MSG="$(date) ${LOGMSG}${MESSAGE2}"
if [[ -n ${GREEN} ]]; then
local ESC="$( echo -en '\033' )"

# Ignore any initial "\" in the colors.
# In case a variable isn't defined, set it to a string that won't be found.
local G="${GREEN/\\/}" ; G="${G/033\[/033X}"
local Y="${YELLOW/\\/}" ; Y="${Y:-abcxyz}" ; Y="${Y/033\[/033X}"
local R="${RED/\\/}" ; R="${R:-abcxyz}" ; R="${R/033\[/033X}"
local D="${cDEBUG/\\/}" ; D="${D:-abcxyz}" ; D="${D/033\[/033X}"
local N="${NC/\\/}" ; N="${N:-abcxyz}" ; N="${N/033\[/033X}"

# Outer "echo -e" handles "\n" (2 characters) in input.
# No "-e" needed on inner "echo".
echo -e "$( echo "$(date) ${LOGMSG}${MESSAGE2}" |
sed -e "s/${ESC}/033/g" -e "s/033\[/033X/g" \
-e "s/${G}//g" \
-e "s/${Y}//g" \
-e "s/${R}//g" \
-e "s/${D}//g" \
-e "s/${N}//g" \
)"
remove_colors "${MSG}"
else
echo "$(date) ${LOGMSG}${MESSAGE2}"
echo "${MSG}"
fi >> "${DISPLAY_MSG_LOG}"
}

Expand Down

0 comments on commit 206d091

Please sign in to comment.