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 E426A1581C1 for ; Sun, 7 Jul 2024 05:55:42 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E79C3E2A5A; Sun, 7 Jul 2024 05:55:40 +0000 (UTC) Received: from smtp.gentoo.org (smtp.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 CF516E2A58 for ; Sun, 7 Jul 2024 05:55:40 +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 D7157340C8A for ; Sun, 7 Jul 2024 05:55:39 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 1C5561DD2 for ; Sun, 7 Jul 2024 05:55:38 +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: <1719596341.fd79ab30122474ab5207ea001d92ffac677b5380.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: functions/ X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions/experimental.sh X-VCS-Directories: functions/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: fd79ab30122474ab5207ea001d92ffac677b5380 X-VCS-Branch: master Date: Sun, 7 Jul 2024 05:55:38 +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: 35fc19c7-55a3-4835-8e67-965302328512 X-Archives-Hash: 2bb0807d10bc390a5847925174c96d82 commit: fd79ab30122474ab5207ea001d92ffac677b5380 Author: Kerin Millar plushkava net> AuthorDate: Thu Jun 27 16:06:38 2024 +0000 Commit: Sam James gentoo org> CommitDate: Fri Jun 28 17:39:01 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=fd79ab30 Add an experimental module for staging new ideas The idea is to be able to write new functions without having to initially commit to their being in the core library or, indeed, any of the other modules. Experimental functions may be promoted or simply dropped without warning if it is decided that they are insufficiently useful to merit incorporation. This initial commit defines the is_interactive() and prepend_ts() functions. Signed-off-by: Kerin Millar plushkava.net> functions/experimental.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/functions/experimental.sh b/functions/experimental.sh new file mode 100644 index 0000000..f577fa7 --- /dev/null +++ b/functions/experimental.sh @@ -0,0 +1,53 @@ +# Copyright 2024 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 +# shellcheck shell=sh disable=3043 + +# This file contains functions considered experimental in nature. Any functions +# defined here may eventually be promoted to the core library or to a distinct +# module. They may also be dropped without warning, either because they were +# not considered as being sufficiently within the scope of gentoo-functions as +# a project or because they were deemed to be insufficiently useful. As such, it +# serves as a staging ground for new ideas. Note that GENFUN_API_LEVEL must +# never be incremented on account of any changes made to this module. + +warn "sourcing the experimental module from gentoo-functions; no stability guarantee is provided" + +# +# Returns 0 provided that two conditions hold. Firstly, that the standard input +# is connected to a tty. Secondly, that the standard output has not been closed. +# This technique is loosely based on the IO::Interactive::Tiny module from CPAN. +# +is_interactive() +{ + test -t 0 && { true 3>&1; } 2>/dev/null +} + +# +# Continuously reads lines from the standard input, prepending each with a +# timestamp before printing to the standard output. Timestamps shall be in the +# format of "%FT%T%z", per strftime(3). Output buffering shall not be employed. +# +prepend_ts() +{ + if hash gawk 2>/dev/null; then + prepend_ts() + { + gawk '{ print strftime("%FT%T%z"), $0; fflush(); }' + } + elif hash ts 2>/dev/null; then + prepend_ts() + { + ts '%FT%T%z' + } + elif bash -c '(( BASH_VERSINFO >= 4 ))' 2>/dev/null; then + prepend_ts() + { + bash -c 'while read -r; do printf "%(%FT%T%z)T %s\n" -1 "${REPLY}"; done' + } + else + warn "prepend_ts: this function requires that either bash, gawk or moreutils be installed" + return 1 + fi + + prepend_ts +}