Skip to content

Commit

Permalink
New TextPrefix function (rear#3160)
Browse files Browse the repository at this point in the history
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 rear#3160
  • Loading branch information
jsmeix authored and lzaoral committed Mar 5, 2024
1 parent f82dd7c commit 8670882
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions usr/share/rear/lib/_input-output-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8670882

Please sign in to comment.