From 86708824ce7ad17912111c357ca1c5aaa0192c10 Mon Sep 17 00:00:00 2001 From: Johannes Meixner Date: Fri, 23 Feb 2024 15:14:36 +0100 Subject: [PATCH] New TextPrefix function (#3160) New TextPrefix function to remove leading space and tab characters from input lines and prefix each line with the argument string if specified. The intent is to be able to properly indent multi-line message strings in the code and output the message without the code indentation but prefixed as needed, see https://github.com/rear/rear/pull/3160 --- usr/share/rear/lib/_input-output-functions.sh | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/usr/share/rear/lib/_input-output-functions.sh b/usr/share/rear/lib/_input-output-functions.sh index 3ff688f1fe..c16a8e75af 100644 --- a/usr/share/rear/lib/_input-output-functions.sh +++ b/usr/share/rear/lib/_input-output-functions.sh @@ -546,6 +546,27 @@ function LogToSyslog () { { logger -t rear -i "${MESSAGE_PREFIX}$*" ; } 2>>/dev/$DISPENSABLE_OUTPUT_DEV } +# Remove leading space and tab characters from input lines +# and prefix each line with the argument string if specified. +# When no argument is specified each line is prefixed/indented with two spaces. +# When an empty argument is specified only leading space and tab characters are removed. +# The intent is to be able to properly indent multi-line message strings in the code and +# output the message without the code indentation but prefixed as needed for example like +# message="first line +# second line +# last line" +# LogPrint "Message text:$LF$( TextPrefix ' | ' <<<"$message" )" +# which results the following output +# Message text: +# | first line +# | second line +# | last line +function TextPrefix () { + { local prefix="${1- }" + sed -e "s/^[ \t]*/$prefix/" + } 2>>/dev/$DISPENSABLE_OUTPUT_DEV +} + # Check if any of the arguments is executable (logical OR condition). # Using plain "type" without any option because has_binary is intended # to know if there is a program that one can call regardless if it is