From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id DC78A158003 for ; Mon, 6 Feb 2023 03:59:07 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id F32C6E07A5; Mon, 6 Feb 2023 03:59:06 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id D565DE07A5 for ; Mon, 6 Feb 2023 03:59:06 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id B30F3340C53 for ; Mon, 6 Feb 2023 03:59:05 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 4B8DC899 for ; Mon, 6 Feb 2023 03:59:04 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1675652944.a1b7172e3c094c2a0db29e4e346bbe4a8c824200.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: a1b7172e3c094c2a0db29e4e346bbe4a8c824200 X-VCS-Branch: master Date: Mon, 6 Feb 2023 03:59:04 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 9d35b769-624b-4a09-9ab4-05f404f8fc80 X-Archives-Hash: 9b03bac0d9a9d118658bb6a5aea3e3d1 commit: a1b7172e3c094c2a0db29e4e346bbe4a8c824200 Author: Kerin Millar plushkava net> AuthorDate: Sun Feb 5 23:40:41 2023 +0000 Commit: Sam James gentoo 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 plushkava.net> Signed-off-by: Sam James 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