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 3F5AC15800F for ; Wed, 15 Feb 2023 07:46:32 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8AB87E07A5; Wed, 15 Feb 2023 07:46:31 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6D92BE07A5 for ; Wed, 15 Feb 2023 07:46:31 +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) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 7A6AF335D41 for ; Wed, 15 Feb 2023 07:46:30 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D8EFE8AE for ; Wed, 15 Feb 2023 07:46:28 +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: <1676446355.34abe2307694673a704009d3baf8862008711fab.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: 34abe2307694673a704009d3baf8862008711fab X-VCS-Branch: master Date: Wed, 15 Feb 2023 07:46:28 +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: fd94689a-e2fc-4a08-9f57-be53a41c27f6 X-Archives-Hash: 9ee60cb72ca600d4d36f59a0d9896b9a commit: 34abe2307694673a704009d3baf8862008711fab Author: Kerin Millar plushkava net> AuthorDate: Wed Feb 15 05:03:42 2023 +0000 Commit: Sam James gentoo org> CommitDate: Wed Feb 15 07:32:35 2023 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=34abe230 Implement - and integrate - an _eprint() helper function The _eprint() function encapsulates some of the behaviour that the following functions have in common. - ebegin - einfo - einfon - eerror - eerrorn - ewarnn - ewarn Additionally, have einfo() wrap einfon(), eerror() wrap eerrorn(), and ewarn() wrap ewarnn(). All of this allows for a pleasing degree of code reduction while improving the structure of the functions overall, partly owing to a complementary reduction in the number of return statements. It also plots a course for the eventual implementation of proper TTY detection, in a context-sensitive manner. Bug: https://bugs.gentoo.org/730432 Signed-off-by: Kerin Millar plushkava.net> Signed-off-by: Sam James gentoo.org> functions.sh | 109 ++++++++++++++++++++--------------------------------------- 1 file changed, 37 insertions(+), 72 deletions(-) diff --git a/functions.sh b/functions.sh index bd45c6f..c1ff6a4 100644 --- a/functions.sh +++ b/functions.sh @@ -5,6 +5,20 @@ # shellcheck disable=2034 RC_GOT_FUNCTIONS="yes" +# +# This is a private function, called by ebegin, eerrorn, einfon, and ewarnn. +# +_eprint() { + local color + color=$1 + shift + + if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then + printf '\n' + fi + printf ' %s*%s %s%b' "${color}" "${NORMAL}" "${genfun_indent}" "$*" +} + # # hard set the indent used for e-commands. # num defaults to 0 @@ -102,15 +116,10 @@ esyslog() # einfon() { - if yesno "${EINFO_QUIET}"; then - return 0 + if ! yesno "${EINFO_QUIET}"; then + _eprint "${GOOD}" "$@" + genfun_lastcall="einfon" fi - if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then - printf "\n" - fi - printf " ${GOOD}*${NORMAL} ${genfun_indent}$*" - genfun_lastcall="einfon" - return 0 } # @@ -118,9 +127,8 @@ einfon() # einfo() { - einfon "$*\n" + einfon "$*\\n" genfun_lastcall="einfo" - return 0 } # @@ -128,20 +136,11 @@ einfo() # ewarnn() { - if yesno "${EINFO_QUIET}"; then - return 0 - else - if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${WARN}*${NORMAL} ${genfun_indent}$*" >&2 + if ! yesno "${EINFO_QUIET}"; then + _eprint "${WARN}" "$@" >&2 + esyslog "daemon.warning" "${0##*/}" "$@" + genfun_lastcall="ewarnn" fi - - # Log warnings to system log - esyslog "daemon.warning" "${0##*/}" "$@" - - genfun_lastcall="ewarnn" - return 0 } # @@ -149,20 +148,8 @@ ewarnn() # ewarn() { - if yesno "${EINFO_QUIET}"; then - return 0 - else - if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${WARN}*${NORMAL} ${genfun_indent}$*\n" >&2 - fi - - # Log warnings to system log - esyslog "daemon.warning" "${0##*/}" "$@" - + ewarnn "$*\\n" genfun_lastcall="ewarn" - return 0 } # @@ -170,19 +157,11 @@ ewarn() # eerrorn() { - if yesno "${EERROR_QUIET}"; then - return 1 - else - if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${BAD}*${NORMAL} ${genfun_indent}$*" >&2 + if ! yesno "${EERROR_QUIET}"; then + _eprint "${BAD}" "$@" >&2 + esyslog "daemon.err" "${0##*/}" "$@" + genfun_lastcall="eerrorn" fi - - # Log errors to system log - esyslog "daemon.err" "${0##*/}" "$@" - - genfun_lastcall="eerrorn" return 1 } @@ -191,18 +170,7 @@ eerrorn() # eerror() { - if yesno "${EERROR_QUIET}"; then - return 1 - else - if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then - printf "\n" >&2 - fi - printf " ${BAD}*${NORMAL} ${genfun_indent}$*\n" >&2 - fi - - # Log errors to system log - esyslog "daemon.err" "${0##*/}" "$@" - + eerrorn "$*\\n" genfun_lastcall="eerror" return 1 } @@ -212,20 +180,17 @@ eerror() # ebegin() { - local msg="$*" - if yesno "${EINFO_QUIET}"; then - return 0 - fi + local msg - msg="${msg} ..." - einfon "${msg}" - if [ -n "${genfun_endcol}" ]; then - printf "\n" + if ! yesno "${EINFO_QUIET}"; then + msg="$* ..." + _eprint "${GOOD}" "${msg}" + if [ -n "${genfun_endcol}" ]; then + printf '\n' + fi + genfun_lastbegun_strlen="$(( 3 + ${#genfun_indent} + ${#msg} ))" + genfun_lastcall="ebegin" fi - - genfun_lastbegun_strlen="$(( 3 + ${#genfun_indent} + ${#msg} ))" - genfun_lastcall="ebegin" - return 0 } #