commit:     a1b7172e3c094c2a0db29e4e346bbe4a8c824200
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun Feb  5 23:40:41 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Feb  6 03:09:04 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=a1b7172e

Avoid unnecessary subshells and use hash to avoid repeated PATH lookups

Use hash to determine whether logger is available, preventing repeated
PATH searches in the case that esyslog() is called more than once.

Once RC_NOCOLOR is set to "yes", don't waste any further time iterating
over the remaining positional parameters.

Use hash to determine whether tput is available, preventing at least one
repeated PATH lookup.

Don't incur a pointless subshell in the course of checking whether tput
is available on the second occasion.

Don't repeatedly execute tput, incurring a subshell on each occasion, to
get the sequences for "bold" and "sgr0". Obtain both, once.

Use unset -v to indicate that variables, specifically, are to be unset.

Remove the true hack at the end of the script. It's no longer needed.

The overall reduction in the number of subshells amounts to 9 and it can
be trivially proven that the code is executed more quickly.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 functions.sh | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/functions.sh b/functions.sh
index 0391c90..ae7d151 100644
--- a/functions.sh
+++ b/functions.sh
@@ -71,7 +71,7 @@ esyslog()
        local pri=
        local tag=
 
-       if [ -n "${EINFO_LOG}" ] && command -v logger > /dev/null 2>&1; then
+       if [ -n "${EINFO_LOG}" ] && hash logger 2>/dev/null; then
                pri="$1"
                tag="$2"
 
@@ -434,6 +434,7 @@ for arg in "$@" ; do
                # Lastly check if the user disabled it with --nocolor argument
                --nocolor|--nocolour|-nc|-C)
                        RC_NOCOLOR="yes"
+                       break
                        ;;
        esac
 done
@@ -447,7 +448,7 @@ COLS="${COLUMNS:-0}"            # bash's internal COLUMNS 
variable
 
 if ! yesno "${RC_ENDCOL}"; then
        ENDCOL=''
-elif command -v tput >/dev/null 2>&1; then
+elif hash tput 2>/dev/null; then
        ENDCOL="$(tput cuu1)$(tput cuf $(( COLS - 8 )) )"
 else
        ENDCOL='\033[A\033['$(( COLS - 8 ))'C'
@@ -455,14 +456,16 @@ fi
 
 # Setup the colors so our messages all look pretty
 if yesno "${RC_NOCOLOR}"; then
-       unset BAD BRACKET GOOD HILITE NORMAL WARN
-elif (command -v tput && tput colors) >/dev/null 2>&1; then
-       BAD="$(tput sgr0)$(tput bold)$(tput setaf 1)"
-       BRACKET="$(tput sgr0)$(tput bold)$(tput setaf 4)"
-       GOOD="$(tput sgr0)$(tput bold)$(tput setaf 2)"
-       HILITE="$(tput sgr0)$(tput bold)$(tput setaf 6)"
-       NORMAL="$(tput sgr0)"
-       WARN="$(tput sgr0)$(tput bold)$(tput setaf 3)"
+       unset -v BAD BRACKET GOOD HILITE NORMAL WARN
+elif { hash tput && tput colors >/dev/null; } 2>/dev/null; then
+       genfuncs_bold=$(tput bold) genfuncs_norm=$(tput sgr0)
+       BAD="${genfuncs_norm}${genfuncs_bold}$(tput setaf 1)"
+       BRACKET="${genfuncs_norm}${genfuncs_bold}$(tput setaf 4)"
+       GOOD="${genfuncs_norm}${genfuncs_bold}$(tput setaf 2)"
+       HILITE="${genfuncs_norm}${genfuncs_bold}$(tput setaf 6)"
+       NORMAL="${genfuncs_norm}"
+       WARN="${genfuncs_norm}${genfuncs_bold}$(tput setaf 3)"
+       unset -v genfuncs_bold genfuncs_norm
 else
        BAD=$(printf '\033[31;01m')
        BRACKET=$(printf '\033[34;01m')
@@ -472,8 +475,4 @@ else
        WARN=$(printf '\033[33;01m')
 fi
 
-# If we made it this far, the script succeeded, so don't let failures
-# from earlier commands (like `tput`) screw up the $? value.
-:
-
 # vim:ts=4

Reply via email to